33
Concepts and Basics of C++ Programming CSE 202

c++ lecture 1

Embed Size (px)

Citation preview

Page 1: c++ lecture 1

Concepts and Basics of C++ Programming

CSE 202

Page 2: c++ lecture 1

Object-Orientation

• A thinking methodology– Everything is an object.– Any system is composed of objects (a system is

also an object).– The evolution and development of a system is

caused by the interactions of the objects inside/outside a system.

Page 3: c++ lecture 1

Everything is an object

• A student, a professor• A desk, a chair, a classroom, a building• A university, a city, a country• The world, the universe• A subject such as CS, IS, Math, History, …

Page 4: c++ lecture 1

The development of a system is caused by interactions

• LPU is defined by the interactions among:– students– professors– staff– Board governance– State governance–… ...

Inside LPU

Outside LPU

Page 5: c++ lecture 1

Reading and Writing Data

Page 6: c++ lecture 1

Reading and Writing Data

• Two operators are introduced in c++ i.e. cout and cin.

• Cout is a predefined object and represents the standard output stream and this output stream represents the screen. Cout, equires iostream file

E.g. cout<<“ I love india”; cout will display this string as such on screen.

Page 7: c++ lecture 1

• << is called insertion or put to operator.• It is also called bit-wise left -shift operator• if string is variable then cout can be used to

display the contents of string.E.g. cout<< string;• cin is used to read the data.• cin>>a;• >> is called extraction operator.

Page 8: c++ lecture 1

The cout Object

• Displays output on the computer screen

• You use the stream insertion operator << to send output to cout:

cout << "Programming is fun!";

Page 9: c++ lecture 1

The cout Object• Can be used to send more than one item to

cout:

cout << "Hello " << "there!";Or:

cout << "Hello ";cout << "there!";

Page 10: c++ lecture 1

The cout Object

• This produces one line of output:

cout << "Programming is ";cout << "fun!";

Page 11: c++ lecture 1

The cin Object

• Standard input object• Like cout, requires iostream file• Used to read input from keyboard• Information retrieved from cin with >>• Input is stored in one or more variables

Page 12: c++ lecture 1

The cin Object

• cin converts data to the type that matches the variable:

int height;cout << "How tall is the room? ";cin >> height;

Page 13: c++ lecture 1

The cin Object• Can be used to input more than one value:

cin >> height >> width;

• Multiple values from keyboard must be separated by spaces

• Order is important: first value entered goes to first variable, etc.

Page 14: c++ lecture 1

Reading Strings with cin• Can be used to read in a string• Must first declare an array to hold characters in

string:char myName[21];

• nyName is name of array, 21 is the number of characters that can be stored (the size of the array), including the NULL character at the end

• Can be used with cin to assign a value:cin >> myName;

Page 15: c++ lecture 1

Class

• A class is a user define data type which holds both data and function.

• The data included in the class i.e the internal data is called data member and the functions included is called the member function.

• These member functions can manipulate the internal data of the class

Page 16: c++ lecture 1

Object

• Is an instant of a class.

• In terms of variables, class would be the type and an object would be a variable.

Page 17: c++ lecture 1

Creating Classes in C++

• A class definition begins with the keyword class.

• The body of the class is contained within a set of braces, { } ; (notice the semi-colon).

class class_name{

….….….};

Class body (data member + methodsmethods)

Any valid identifier

Page 18: c++ lecture 1

class classname{ private:variable declarations; function declarations; public: variable declarations; function declarations; protected: variable declarations; function declarations;} obj1, obj2,…..objN;

Page 19: c++ lecture 1

Class name

• Name given to a particular class (any user define name). It can also be called as tag name of the class that act as the type specifier for class using which we can create objects.

• The class is specified by keyword “class”

Page 20: c++ lecture 1

Data Members

• Data type properties that describe the characteristics of a class.

• We can declare any number of data members of any type in a class.

E.g. int x;

Page 21: c++ lecture 1

Member functions

• Various operations that can be performed to data members of that class.

• We can declare any number of member functions of any type in a class.

E.g. void read();

Page 22: c++ lecture 1

Access Specifiers• Used to specify access rights for the data

members and member functions of the class.• Depending upon the access level of a class

member, access to it is allowed or denied.• Within the body, the keywords private: and public: specify the access level of the members of the class.– the default is private.

• Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.

Page 23: c++ lecture 1

class class_name{ private:

………

public:………

};Public members or methods

private members or methods

Page 24: c++ lecture 1

Private: only members of that class have accessibility can be accessed only through member

functions of that class i.e by the functions declared inside the class only.

Private members and methods are for internal use only.

Page 25: c++ lecture 1

Public:• Accessible from both inside and outside the class also i.e

by the functions declared in the main() program also.

Protected:• Stage between private and public access.• They can be accessed by the member function or friend

functions of the class. They are similar to private members in the sense that they cannot be accessed by the non- member functions of the class.

Page 26: c++ lecture 1

Class Example

• This class example shows how we can encapsulate (gather) a circle information into one package (unit or class)

class Circle{ private:

double radius; public:

void setRadius(double r); double getDiameter();

double getArea();double getCircumference();

};

No need for others classes to access and retrieve its value directly. Theclass methods are responsible forthat only.

They are accessible from outsidethe class, and they can access themember (radius)

Page 27: c++ lecture 1

Methods definition• The member function of the class can be defined in two

different ways:1) Inside the class definition:- The member functions are simple

defined inside the class only i.e the body of the function resides inside the range of class only.

2) Outside the class definition: by using scope resolution operator, which specifies that the scope of the function is restricted to the class class_name.

Syntax:- class_name:: function_name

Page 28: c++ lecture 1

Inside the class definitionEg: class abc{private:int rollno;char name[20];public:void getdata(){cout<<“name=“;cin>>name;

cout<<“rollno=“;cin>>rollno;}void display(){cout<<“name=“<<name;cout<<“rollno=“<<rollno;}};

Page 29: c++ lecture 1

Outside the class definitionEg: class abc{private:int rollno;char name[20];public:void getdata();void display();};void abc :: getdata(){cout<<“name=“;

cin>>name;cout<<“rollno=“;cin>>rollno;}void abc :: display(){cout<<“name and rollno=“;cout<<name<<rollno;}

Page 30: c++ lecture 1

INLINE AND NON INLINE MEMBER FUNCTION

• A function defined inside the class is by default inline function

• A function defined outside the class using scope resolution operation is non-inline function. It can be made inline by using keyword inline before the function definition.

Eg. inline void abc::getdata()

Page 31: c++ lecture 1

Declaring objects

• Defining objects of class data type is known as class instantiation(instances of class).

• When we create objects during that moment , memory is allocated to them.

Ex- class Circle c;

Page 32: c++ lecture 1

class book{private:int p; char n[40];public:void getdata(){cout<<“enter book price”;cin>>p;cout<<“enter book name”;cin>>n; }void display(); };

void book ::display(){cout<<“book name=“<<n;cout<<“book price=“<<p;}void main(){class book obj;obj.getdata();obj.display();}

Page 33: c++ lecture 1

Accessing class members • Public members of class can be accessed using

dot(.) operator with the object name.

• Private members of the class are accessed inside the public member functions of class.

Eg: obj.getdata();