07_objects_and_classes.ppt

26
7/27/2019 07_Objects_and_Classes.ppt............. http://slidepdf.com/reader/full/07objectsandclassesppt 1/26 Programming Concepts in C++ CT025-3-2 Classes and Objects

Upload: kaushal-vaishnav

Post on 02-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 1/26

Programming Concepts in C++CT025-3-2 

Classes and Objects

Page 2: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 2/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 2 (of 9)

Topic and structure of the lesson

• Classes and Objects

 – Specifying a class

 – Defining member variables and functions – Nesting of member functions

 – Arrays within a class

 – Static Data Members and Member Functions

 – Arrays of objects

 – Objects as function arguments

Page 3: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 3/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 3 (of 9)

Learning Outcomes

• By the end of this session, you should be

able to:

 – create, debug and run a simple C++ programsusing functions and applying concepts such

as classes and objects

Page 4: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 4/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 4 (of 9)

Key terms to be used

• If you have mastered this topic, you should

be able to use the following terms correctly

in your assignments and exams: –class

 –objects

Page 5: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 5/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

Definition of class

•  A class is the collection of related data and

function under a single name.

• Class is user defined data type.• Class consists of Data members and

methods(functions).

Page 6: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 6/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

• A Class is a blueprint for objects 

• When a class is defined, no memory is

allocated.• Object doesn't exist until an instance of the

class has been created;

• When the object is physically created,space for that object is allocated in RAM.

• Class is just the specification for objects

and object bears the property of that class.

Page 7: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 7/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

Defining the Class in C++

• Class is defined using keyword class followed by

identifier(name of class).

• Body of class is defined inside curly brackets an

terminated by semicolon at the end in similar way as structure.

class class_name

{access_specifier_1: member1;

access_specifier_2: member2;

...

} object_names;

Page 8: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 8/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

• Where class_name is a valid identifier for 

the class.

• object_names is an optional list of namesfor objects of this class.

• The body of the declaration can contain

members, that can be either data or function declarations,

• and optionally access specifiers.

Page 9: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 9/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

Class Members

• Data Members and methods must be

declared within the class definition.

•  A member cannot be redeclare within aclass.

• No member can be added elsewhere other 

than in the class definition.

•  

Page 10: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 10/26

Page 11: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 11/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

 Access specifiers 

•  Access specifier : is one of the following three

keywords:

• private,

• public

• protected.

• These specifiers modify the access rights that

the members following them acquire:private members: of a class are accessible only

from within other members of the same class or 

from their friends.

Page 12: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 12/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

• protected members are accessible from

members of their same class and from

their friends, but also from members of 

their derived classes.

public members are accessible from

anywhere where the object is visible

Page 13: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 13/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

 Accessing the Data Members

•  Accessing a data member depends solely

on the access control of that data member.

• If its public, then the data member can beeasily accessed using the direct member 

access (.) operator with the object of that

class.

Page 14: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 14/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

• If, the data member is defined as private

or protected, then we cannot access the

data variables directly.

• we create special public member 

functions to access,to use or to initialize

the private and protected data members.

• These member functions are

called getter and setter functions.

Page 15: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 15/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

Accessing Private Data

Members

• The setter function will set the value

passed as argument to the private data

member, and

• getter function will return the value of the

private data member to be used.

• Both getter and setter function must bedefined public.

Page 16: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 16/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

Accessing Protected Data

Members

• Protected data members, can be

accessed directly using dot (.) operator 

inside the subclass of the current class.,

• For non-subclass we follow the steps

same as to access private data member.

Page 17: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 17/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

class Student

{

public: int rollno;

string name;};

int main()

{

Student A;

Student B;

 A.rollno=1;

 A.name=“Raju"; 

B.rollno=2;

B.name=“suresh"; cout <<"Name and Roll no of A is:"<<A.name << A.rollno;

cout <<"Name and Roll no of B is :"<< B.name << B.rollno;

}

Page 18: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 18/26CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

class Student {

private: // private data member 

int rollno;

public: // public accessor and mutator functions

int getRollno(){

return rollno;

}

void setRollno(int i)

{

rollno=i;}

};

int main()

{

Student A;

 A.rollono=1; //Compile time error 

cout<< A.rollno; //Compile time error A.

setRollno(1); //Rollno initialized to 1

cout<< A.getRollno(); //Output will be 1 }

Page 19: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 19/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

• class Box

{

public:

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box

};

int main( )

{

Box Box1; // Declare Box1 of type Box

Box Box2; // Declare Box2 of type Box

double volume = 0.0; // Store the volume of a box here

// box 1 specification

Box1.height = 4.0;Box1.length = 6.0;

Box1.breadth = 3.0;

// box 2 specification

Box2.height = 10.0;

Box2.length = 12.0;

Box2.breadth = 12.0;

// volume of box 1volume = Box1.height * Box1.length * Box1.breadth;

cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2

volume = Box2.height * Box2.length * Box2.breadth;

cout << "Volume of Box2 : " << volume <<endl;

return 0;

}

Page 20: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 20/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

• Volume of Box1 : 72 

Volume of Box2 : 1440 

Page 21: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 21/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

Nesting of member functions

•  A member function of a class can be

called only by an object of that class using

a dot operator.

•  A member function can be called by using

its name inside another member function

of the same class.

• This is known as nesting of member 

functions.

Page 22: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 22/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

• #include

using namespace std;

class set

{

int m,n;

public:

void input(void);void display(void);

void largest(void);

};

int set :: largest(void)

{

if(m >= n)

return(m);

else

return(n);}

void set :: input(void)

{

cout << "Input value of m and n"<<"\n";

cin >> m>>n;

}

void set :: display(void)

{

cout << "largest value=" << largest() <<"\n";}

int main()

{

set A;

 A.input();

 A.display();

• return 0;

}

Page 23: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 23/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects

 Arrays within a class

Page 24: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 24/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 7 (of 9)

Follow up Assignment

1. Write a complete C++ program that uses inline function

sphereVolume to prompt the user for radius of a sphere, and

to calculate and print the volume of that sphere using the

assignment

volume = (4/3)*3.14159*pow(radius, 3);

2. Do question 3.12 (Deitel & Deitel)

3. Do question 3.18 (Deitel & Deitel)

4. Do question 3.19 (Deitel & Deitel)

5. Do question 3.21 (Deitel & Deitel)

Page 25: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 25/26

CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 8 (of 9)

Summary of key points

• Classes and Objects

 – Specifying a class

 – Defining member variables and functions

 – Nesting of member functions

 – Arrays within a class

 – Static Data Members and Member Functions

 – Arrays of objects

 – Objects as function arguments

Page 26: 07_Objects_and_Classes.ppt

7/27/2019 07_Objects_and_Classes.ppt.............

http://slidepdf.com/reader/full/07objectsandclassesppt 26/26

Highlight of next session

• Classes and Objects

 – Specifying a class

 – Defining member variables and functions

 – Nesting of member functions

 – Arrays within a class

 – Static Data Members and Member Functions

 – Arrays of objects

 – Objects as function arguments