Upload
kory-hill
View
213
Download
0
Embed Size (px)
Citation preview
Object Oriented Object Oriented ProgrammingProgramming
(OOP)(OOP)Lecture No. 8Lecture No. 8
ReviewReview
►ClassClass ConceptConcept DefinitionDefinition
►Data membersData members►Member FunctionsMember Functions►Access specifierAccess 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
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
Function Inside Class BodyFunction Inside Class Body
class class ClassName ClassName {{……public:public:ReturnType ReturnType FunctionNameFunctionName() {() {
……}}
};};
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
ExampleExample
class Student{class Student{
int rollNo;int rollNo;
public:public:
void setRollNo(int aRollNo){void setRollNo(int aRollNo){
rollNo = aRollNo;rollNo = aRollNo;
}}
};};
Function Outside Class BodyFunction Outside Class Body
class class ClassNameClassName{{……public:public:ReturnTypeReturnType FunctionName FunctionName();();
};};ReturnTypeReturnType ClassName ClassName::::FunctionNameFunctionName()(){{……
}}Scope
resolution operator
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;
}}
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
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);
}}
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
ExampleExample
class Student{class Student{int rollNo;int rollNo;
public:public:void setRollNo(int aRollNo){void setRollNo(int aRollNo){
……rollNo = aRollNo;rollNo = aRollNo;
}}};};
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;
}}
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;
}}
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;
}}
ConstructorConstructor
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
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
ExampleExample
class Student{class Student{……
public:public:Student(){Student(){
rollNo = 0;rollNo = 0;……
}}};};
ExampleExample
int main()int main()
{{
Student aStudent;Student aStudent;
/*constructor is implicitly /*constructor is implicitly called at this point*/called at this point*/
}}
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
ExampleExample
class Studentclass Student{{int rollNo;int rollNo;char *name;char *name;float GPA;float GPA;
public:public:…… //no constructors//no constructors
};};
ExampleExample
Compiler generated default Compiler generated default constructorconstructor
{{
rollNo = 0;rollNo = 0;
GPA = 0.0;GPA = 0.0;
name = NULL;name = NULL;
}}
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
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);
};};
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;}}……
}}
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);
}}
Constructor OverloadingConstructor Overloading
►Use default parameter value to reduce Use default parameter value to reduce the writing effortthe writing effort
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);
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
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);
}}
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;
}}
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
ExampleExample
Student studentA;Student studentB = studentA;
Name
GPARollNo
studentA
Name
GPARollNo
studentBAHMAD…
Memory
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
}}
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
ExampleExample
Name
GPARollNo
A
Name
GPARollNo
B
AHMAD
Memory
AHMAD
Student studentA;Student studentB = studentA;