Object Oriented Programming (OOP) Lecture No. 8. Review ► Class Concept Definition ► Data...

38
Object Oriented Object Oriented Programming Programming (OOP) (OOP) Lecture No. 8 Lecture No. 8

Transcript of Object Oriented Programming (OOP) Lecture No. 8. Review ► Class Concept Definition ► Data...

Page 1: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Object Oriented Object Oriented ProgrammingProgramming

(OOP)(OOP)Lecture No. 8Lecture No. 8

Page 2: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ReviewReview

►ClassClass ConceptConcept DefinitionDefinition

►Data membersData members►Member FunctionsMember Functions►Access specifierAccess specifier

Page 3: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Member FunctionsMember Functions

►Member functions are the functions Member functions are the functions that operate on the data encapsulated that operate on the data encapsulated in the classin the class

►Public member functions are the Public member functions are the interface to the classinterface to the class

Page 4: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Member Functions (contd.)Member Functions (contd.)

►Define member function inside the Define member function inside the class definitionclass definition

OROR►Define member function outside the Define member function outside the

class definitionclass definition But they must be declared inside class But they must be declared inside class

definitiondefinition

Page 5: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Function Inside Class BodyFunction Inside Class Body

class class ClassName ClassName {{……public:public:ReturnType ReturnType FunctionNameFunctionName() {() {

……}}

};};

Page 6: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample►Define a class of student Define a class of student

that has a roll number. that has a roll number. This class should have a This class should have a function that can be used function that can be used to set the roll numberto set the roll number

Page 7: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

class Student{class Student{

int rollNo;int rollNo;

public:public:

void setRollNo(int aRollNo){void setRollNo(int aRollNo){

rollNo = aRollNo;rollNo = aRollNo;

}}

};};

Page 8: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Function Outside Class BodyFunction Outside Class Body

class class ClassNameClassName{{……public:public:ReturnTypeReturnType FunctionName FunctionName();();

};};ReturnTypeReturnType ClassName ClassName::::FunctionNameFunctionName()(){{……

}}Scope

resolution operator

Page 9: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

class Student{class Student{……int rollNo;int rollNo;

public:public:void setRollNo(int aRollNo);void setRollNo(int aRollNo);

};};void Student::setRollNo(int aRollNo){void Student::setRollNo(int aRollNo){……rollNo = aRollNo;rollNo = aRollNo;

}}

Page 10: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Inline FunctionsInline Functions

► Instead of calling an inline function Instead of calling an inline function compiler replaces the code at the compiler replaces the code at the function call pointfunction call point

►Keyword ‘inline’ is used to request Keyword ‘inline’ is used to request compiler to make a function inlinecompiler to make a function inline

► It is a request and not a commandIt is a request and not a command

Page 11: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

inline int Area(int len, int hi)inline int Area(int len, int hi){{return len * hi;return len * hi;

}}int main()int main(){{cout << Area(10,20);cout << Area(10,20);

}}

Page 12: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Inline FunctionsInline Functions

► If we define the function inside the If we define the function inside the class body then the function is by class body then the function is by default an inline functiondefault an inline function

► In case function is defined outside the In case function is defined outside the class body then we must use the class body then we must use the keyword ‘inline’ to make a function keyword ‘inline’ to make a function inlineinline

Page 13: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

class Student{class Student{int rollNo;int rollNo;

public:public:void setRollNo(int aRollNo){void setRollNo(int aRollNo){

……rollNo = aRollNo;rollNo = aRollNo;

}}};};

Page 14: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

class Student{class Student{

……

public:public:

inline void setRollNo(int aRollNo);inline void setRollNo(int aRollNo);

};};

void Student::setRollNo(int aRollNo){void Student::setRollNo(int aRollNo){

……

rollNo = aRollNo;rollNo = aRollNo;

}}

Page 15: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

class Student{class Student{……public:public:void setRollNo(int aRollNo);void setRollNo(int aRollNo);

};};inline void Student::setRollNo(int inline void Student::setRollNo(int

aRollNo){aRollNo){……rollNo = aRollNo;rollNo = aRollNo;

}}

Page 16: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

class Student{class Student{……public:public:inline void setRollNo(int aRollNo);inline void setRollNo(int aRollNo);

};};inline void Student::setRollNo(int inline void Student::setRollNo(int

aRollNo){aRollNo){……rollNo = aRollNo;rollNo = aRollNo;

}}

Page 17: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ConstructorConstructor

Page 18: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ConstructorConstructor

►Constructor is used to initialize the Constructor is used to initialize the objects of a classobjects of a class

►Constructor is used to ensure that Constructor is used to ensure that object is in well defined state at the object is in well defined state at the time of creationtime of creation

►Constructor is automatically called Constructor is automatically called when the object is createdwhen the object is created

►Constructor are not usually called Constructor are not usually called explicitlyexplicitly

Page 19: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Constructor (contd.)Constructor (contd.)

►Constructor is a special function Constructor is a special function having same name as the class namehaving same name as the class name

►Constructor does not have return typeConstructor does not have return type►Constructors are commonly public Constructors are commonly public

membersmembers

Page 20: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

class Student{class Student{……

public:public:Student(){Student(){

rollNo = 0;rollNo = 0;……

}}};};

Page 21: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

int main()int main()

{{

Student aStudent;Student aStudent;

/*constructor is implicitly /*constructor is implicitly called at this point*/called at this point*/

}}

Page 22: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Default ConstructorDefault Constructor

►Constructor without any argument is Constructor without any argument is called default constructorcalled default constructor

► If we do not define a default If we do not define a default constructor the compiler will generate constructor the compiler will generate a default constructora default constructor

►This compiler generated default This compiler generated default constructor initialize the data constructor initialize the data members to their default valuesmembers to their default values

Page 23: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

class Studentclass Student{{int rollNo;int rollNo;char *name;char *name;float GPA;float GPA;

public:public:…… //no constructors//no constructors

};};

Page 24: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

Compiler generated default Compiler generated default constructorconstructor

{{

rollNo = 0;rollNo = 0;

GPA = 0.0;GPA = 0.0;

name = NULL;name = NULL;

}}

Page 25: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Constructor OverloadingConstructor Overloading

►Constructors can have parametersConstructors can have parameters►These parameters are used to initialize These parameters are used to initialize

the data members with user supplied the data members with user supplied datadata

Page 26: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

class Student{class Student{……public:public:Student();Student();Student(char * aName);Student(char * aName);Student(char * aName, int aRollNo);Student(char * aName, int aRollNo);Student(int aRollNo, int aRollNo, Student(int aRollNo, int aRollNo, float aGPA);float aGPA);

};};

Page 27: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

Student::Student(Student::Student(int aRollNo, int aRollNo,

char * aName){char * aName){if(aRollNo < 0){if(aRollNo < 0){

rollNo = 0;rollNo = 0;} } else {else {rollNo = aRollNo;rollNo = aRollNo;}}……

}}

Page 28: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

int main()int main()

{{

Student student1;Student student1;

Student student2(“Name”);Student student2(“Name”);

Student student3(”Name”, 1);Student student3(”Name”, 1);

Student student4(”Name”,1,4.0);Student student4(”Name”,1,4.0);

}}

Page 29: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Constructor OverloadingConstructor Overloading

►Use default parameter value to reduce Use default parameter value to reduce the writing effortthe writing effort

Page 30: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

Student::Student(Student::Student( char * aName = NULL, char * aName = NULL, int aRollNo= 0, int aRollNo= 0, float aGPA = 0.0){float aGPA = 0.0){

……}}Is equivalent toIs equivalent toStudent();Student();Student(char * aName);Student(char * aName);Student(char * aName, int aRollNo);Student(char * aName, int aRollNo);Student(char * Name, int aRollNo, float Student(char * Name, int aRollNo, float aGPA);aGPA);

Page 31: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Copy ConstructorCopy Constructor

►Copy constructor are used when:Copy constructor are used when: Initializing an object at the time of Initializing an object at the time of

creationcreation When an object is passed by value to a When an object is passed by value to a

functionfunction

Page 32: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

void func1(Student student){void func1(Student student){……}}int main(){int main(){Student studentA;Student studentA;Student studentB = studentA;Student studentB = studentA;func1(studentA);func1(studentA);

}}

Page 33: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Copy Constructor (Syntax)Copy Constructor (Syntax)

Student::Student(Student::Student(const Student &obj){const Student &obj){

rollNo = obj.rollNo;rollNo = obj.rollNo;name = obj.name;name = obj.name;GPA = obj.GPA;GPA = obj.GPA;

}}

Page 34: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Shallow CopyShallow Copy

►When we initialize one object with When we initialize one object with another then the compiler copies state another then the compiler copies state of one object to the otherof one object to the other

►This kind of copying is called shallow This kind of copying is called shallow copyingcopying

Page 35: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

Student studentA;Student studentB = studentA;

Name

GPARollNo

studentA

Name

GPARollNo

studentBAHMAD…

Memory

Page 36: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Copy Constructor (contd.)Copy Constructor (contd.)

Student::Student( Student::Student( const Student &const Student & obj){obj){

int len = strlen(obj.name);int len = strlen(obj.name);name = new char[len+1]name = new char[len+1]strcpy(name, obj.name);strcpy(name, obj.name);……//copy rest of the data members//copy rest of the data members

}}

Page 37: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Copy Constructor (contd.)Copy Constructor (contd.)

►Copy constructor is normally used to Copy constructor is normally used to perform deep copyperform deep copy

► If we do not make a copy constructor If we do not make a copy constructor then the compiler performs shallow then the compiler performs shallow copycopy

Page 38: Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

ExampleExample

Name

GPARollNo

A

Name

GPARollNo

B

AHMAD

Memory

AHMAD

Student studentA;Student studentB = studentA;