Oop Constructor Destructors Constructor Overloading lecture 2

24
Today’s Lecture Today’s Lecture Constructor Constructor Destructors Destructors Constructor Overloading Constructor Overloading Friend function Friend function Inheritance Inheritance Single Inheritance Single Inheritance Multiple Inheritances. Multiple Inheritances. Abbas Ajmal Abbas Ajmal 03149958500 03149958500

Transcript of Oop Constructor Destructors Constructor Overloading lecture 2

Page 1: Oop Constructor  Destructors Constructor Overloading lecture 2

Today’s LectureToday’s Lecture Constructor Constructor Destructors Destructors Constructor OverloadingConstructor Overloading Friend functionFriend function InheritanceInheritance Single InheritanceSingle Inheritance Multiple Inheritances.Multiple Inheritances.

Abbas AjmalAbbas Ajmal0314995850003149958500

Page 2: Oop Constructor  Destructors Constructor Overloading lecture 2

ConstructorsConstructors A constructor is a member function of a class that is called and A constructor is a member function of a class that is called and

executed automatically when an object of that class is created.executed automatically when an object of that class is created. The name of the constructor function is the same as the name The name of the constructor function is the same as the name

of the class itself. of the class itself.

A constructor function may have arguments but it cannot A constructor function may have arguments but it cannot return any value. return any value.

Page 3: Oop Constructor  Destructors Constructor Overloading lecture 2

Example: ConstructorsExample: Constructors

#include<iostream>#include<iostream>using namespace std;using namespace std;class test {class test {

public: public: test(){test(){cout<<"Welcome"<<endl;cout<<"Welcome"<<endl;}}

};};main(){main(){

test a,b,c;test a,b,c;}}

Page 4: Oop Constructor  Destructors Constructor Overloading lecture 2

Example ExplanationExample Explanation

In the above program, the class "test" contains member function In the above program, the class "test" contains member function "test". "test".

This member function is the constructor function because name This member function is the constructor function because name of this function and the name of the class are same. of this function and the name of the class are same.

When the member function "test" is executed, it prints When the member function "test" is executed, it prints "Welcome" on the computer in the program. "Welcome" on the computer in the program.

Page 5: Oop Constructor  Destructors Constructor Overloading lecture 2

Example Explanation (cont..)Example Explanation (cont..) In the above program three objects (a, b c) of the class "test" are created. In the above program three objects (a, b c) of the class "test" are created.

Each time an object of the class "test" is created, the constructor is Each time an object of the class "test" is created, the constructor is executed and the word "Welcome" is printed on the computer screen. executed and the word "Welcome" is printed on the computer screen.

Since three objects are created, the word Welcome is printed three times. Since three objects are created, the word Welcome is printed three times.

Page 6: Oop Constructor  Destructors Constructor Overloading lecture 2

Initializing Data Using Constructors Initializing Data Using Constructors

The constructor functions are normally used to initialize The constructor functions are normally used to initialize values in data members of a class when the program is values in data members of a class when the program is executed. executed.

This type of initialization is called the automatic This type of initialization is called the automatic initialization. initialization.

Page 7: Oop Constructor  Destructors Constructor Overloading lecture 2

Example :Example : Initializing Data Using Constructors Initializing Data Using Constructors

#include<iostream>#include<iostream>using namespace std;using namespace std;class sum class sum {{private: private: int n, m, s; int n, m, s; public:public:sum(int x, int y){sum(int x, int y){n=x;m=y;s=n+m;n=x;m=y;s=n+m;

}void psum(){}void psum(){cout<<"Sum of "<<n<<“cout<<"Sum of "<<n<<“and "<<m<<" isand "<<m<<" is"<<s<<endl;"<<s<<endl;}};}};main ( )main ( ){{sum a(16,10), b(2,3);sum a(16,10), b(2,3);a. psum () ; a. psum () ; b.psum() ; } b.psum() ; }

Page 8: Oop Constructor  Destructors Constructor Overloading lecture 2

In the above program when the object “a” of the class “sum” is In the above program when the object “a” of the class “sum” is created, the control shifts to the constructor function “sum” . created, the control shifts to the constructor function “sum” .

The constructor function “sum” assigns values to variables n The constructor function “sum” assigns values to variables n and m and its also calculates their sum. and m and its also calculates their sum.

Thus when “psum” function is executed by the object “a”, the Thus when “psum” function is executed by the object “a”, the values assigned to the data members of the object by the values assigned to the data members of the object by the constructor are printed.constructor are printed.

Similarly when the object b is executed, the constructor Similarly when the object b is executed, the constructor function is automatically called.function is automatically called.

Example ExplanationExample Explanation

Page 9: Oop Constructor  Destructors Constructor Overloading lecture 2

More than one constructor functions can be defined in one More than one constructor functions can be defined in one class. When more than one constructor functions are defined, class. When more than one constructor functions are defined, each constructor defined with a different set of parameters. each constructor defined with a different set of parameters.

Defining more than one constructor with different set of Defining more than one constructor with different set of parameters is called constructor overloading. parameters is called constructor overloading.

Constructor overloading is used to initialize different values to Constructor overloading is used to initialize different values to class objects. class objects.

Constructor OverloadingConstructor Overloading

Page 10: Oop Constructor  Destructors Constructor Overloading lecture 2

When a program that uses the constructor overloading is When a program that uses the constructor overloading is compiled, C++ compiler checks the number of parameters, compiled, C++ compiler checks the number of parameters, their order and data types and marks them differently.their order and data types and marks them differently.

When an object of the class is created, the corresponding When an object of the class is created, the corresponding constructor that matches the number of parameters of the constructor that matches the number of parameters of the object function is executed. object function is executed.

In the coming example two constructor functions are defined In the coming example two constructor functions are defined in class "sum" as shown on the next slide:in class "sum" as shown on the next slide:

Constructor Overloading (cont..)Constructor Overloading (cont..)

Page 11: Oop Constructor  Destructors Constructor Overloading lecture 2

Example: Constructor OverloadingExample: Constructor Overloading

#include<iostream>#include<iostream>using namespace std;using namespace std;class sum {class sum {public:public:sum(int l, int m, int n){sum(int l, int m, int n){cout<<"sum of three values is cout<<"sum of three values is "<<(l+m+n)<<endl;}"<<(l+m+n)<<endl;}sum (int l, int m){sum (int l, int m){cout<<"sum of two values iscout<<"sum of two values is"<<(l+m)<<endl;"<<(l+m)<<endl;

}};}};

main(){main(){sum (2,3), sum (1,2,3);sum (2,3), sum (1,2,3);

}}

Page 12: Oop Constructor  Destructors Constructor Overloading lecture 2

When the program is executed, the object x is created first and When the program is executed, the object x is created first and then the sum constructor function that has only two integer then the sum constructor function that has only two integer type parameters is executed. type parameters is executed.

Then the y object is created. It has three parameters of integer Then the y object is created. It has three parameters of integer type.type.

So the constructor function that has three arguments of integer So the constructor function that has three arguments of integer type is executed. type is executed.

Example ExplanationExample Explanation

Page 13: Oop Constructor  Destructors Constructor Overloading lecture 2

Write a program to define two constructors to find outWrite a program to define two constructors to find outthe maximum values.the maximum values.

Due Date: 6/4/2015Due Date: 6/4/2015

AssignmentAssignment

Page 14: Oop Constructor  Destructors Constructor Overloading lecture 2

When an object is destroyed a special member function of that When an object is destroyed a special member function of that class is executed automatically. class is executed automatically.

This member function is known as the destructor function or This member function is known as the destructor function or destructor.destructor.

The destructor function has the same name as the name or a The destructor function has the same name as the name or a class but a tilde sign (~) is written before its name. class but a tilde sign (~) is written before its name.

It is executed automatically when an object comes to the end It is executed automatically when an object comes to the end of its life.of its life.

Like constructors, destructors do not return any value. They Like constructors, destructors do not return any value. They also do not take any arguments. also do not take any arguments.

DestructorsDestructors

Page 15: Oop Constructor  Destructors Constructor Overloading lecture 2

For example, a local object is destroyed when all the For example, a local object is destroyed when all the statements of the function in which it is declared are executed. statements of the function in which it is declared are executed.

So at the end of the function, the destructor function is So at the end of the function, the destructor function is executed.executed.

Similarly, global objects (objects that are declared before Similarly, global objects (objects that are declared before main function) or static objects are destroyed at the end of main function) or static objects are destroyed at the end of main function.main function.

The lifetime of these objects end when the program execution The lifetime of these objects end when the program execution ends. So at the end of program the destructor function is ends. So at the end of program the destructor function is executed. executed.

DestructorsDestructors (cont..)(cont..)

Page 16: Oop Constructor  Destructors Constructor Overloading lecture 2

The destructor functions are commonly used to free the The destructor functions are commonly used to free the memory that was allocated for objects. memory that was allocated for objects.

The example on the next slide explains the concept of The example on the next slide explains the concept of constructors and destructor.constructors and destructor.

DestructorsDestructors (cont..)(cont..)

Page 17: Oop Constructor  Destructors Constructor Overloading lecture 2

Example: Constructor and DestructorExample: Constructor and Destructor

#include<iostream>#include<iostream>using namespace std;using namespace std;class prg class prg {{public: public: prg () {prg () {cout<<"This is constructor cout<<"This is constructor

function"<<endl;}function"<<endl;}~prg (){ ~prg (){ cout <<"This is destructor cout <<"This is destructor

function"<<endl; } };function"<<endl; } };int main()int main()

{{prg x; prg x; int a, b;int a, b;a = 10; b = 20;a = 10; b = 20;cout<<"Sum of two numbers cout<<"Sum of two numbers

is"<<(a+b)<<endl; is"<<(a+b)<<endl; }}

Page 18: Oop Constructor  Destructors Constructor Overloading lecture 2

Object can be passed as arguments to member functions. WhenObject can be passed as arguments to member functions. Whenan object is passed as an argument to a member function:an object is passed as an argument to a member function:

Only the name of the object is written in the argument. Only the name of the object is written in the argument.

The number of parameters and their types must be defined in The number of parameters and their types must be defined in the member function to which the object to be passed. The the member function to which the object to be passed. The objects that are passed are treated local for the member objects that are passed are treated local for the member function and are destroyed when the control returns to the function and are destroyed when the control returns to the calling function. calling function.

Passing Objects as an argument to a functionPassing Objects as an argument to a function

Page 19: Oop Constructor  Destructors Constructor Overloading lecture 2

Example: Passing Objects as an argument to a functionExample: Passing Objects as an argument to a function

#include<iostream>#include<iostream>using namespace std;using namespace std;class test { class test { private:private:char name [15];char name [15];public:public:void get(void){ void get(void){ cout<< "enter your name? ";cout<< "enter your name? ";cin >>name;};cin >>name;};

void print (test s)void print (test s){ { cout <<"Name cout <<"Name

is:"<<s.name<<endl;}}; is:"<<s.name<<endl;}}; main()main(){{test temp, abc;test temp, abc; temp.get(); temp.get(); abc.print(temp);}abc.print(temp);}

Page 20: Oop Constructor  Destructors Constructor Overloading lecture 2

In the above program, the class "test" has one data member In the above program, the class "test" has one data member "name" of string type and two member functions "get" and "name" of string type and two member functions "get" and "print". The "print" function has an argument of class "test" "print". The "print" function has an argument of class "test" type.type.

The objects "temp" and "abc" are declared of class "test". The The objects "temp" and "abc" are declared of class "test". The member function gets the name in object "temp" and store it member function gets the name in object "temp" and store it into the data member "name". into the data member "name".

The member function "print" for object "abc” is called by The member function "print" for object "abc” is called by passing argument of object "temp". When the control shifts to passing argument of object "temp". When the control shifts to the member function "print", a copy of "temp" is created as a the member function "print", a copy of "temp" is created as a local object in the print function with name "s". local object in the print function with name "s".

Example ExplanationExample Explanation

Page 21: Oop Constructor  Destructors Constructor Overloading lecture 2

A friend function can access all members of a class in which it is declared.A friend function can access all members of a class in which it is declared. It can also access private and protected members of all other classes even It can also access private and protected members of all other classes even

though it is not a member function of those classes.though it is not a member function of those classes. It is a non-member function. It is normally used when a function has to It is a non-member function. It is normally used when a function has to

access private data of other classes. access private data of other classes. A friend function is defined in a class.A friend function is defined in a class. It is possible to declare the friend function as either private or public.It is possible to declare the friend function as either private or public. The function can be invoked without the use of an object. The friend The function can be invoked without the use of an object. The friend

function has its argument as objectsfunction has its argument as objects

Friend FunctionFriend Function

Page 22: Oop Constructor  Destructors Constructor Overloading lecture 2

It is defined by using the keyword “friend” before the name of It is defined by using the keyword “friend” before the name of the function in the function declarator.the function in the function declarator.

It can be defined anywhere in the class.It can be defined anywhere in the class. When a friend function is defined outside the class, it is When a friend function is defined outside the class, it is

defined without the scope resolution parameters (::).defined without the scope resolution parameters (::). A program example is given on next slide that uses the friend A program example is given on next slide that uses the friend

function.function.

Friend FunctionFriend Function (cont..)(cont..)

Page 23: Oop Constructor  Destructors Constructor Overloading lecture 2

Example: Friend FunctionExample: Friend Function

#include<iostream>#include<iostream>using namespace std;using namespace std;class exforsysclass exforsys{{private:private:

int a,b;int a,b;public:public:

void test()void test(){{a=100;a=100;b=200;b=200;}}friend int compute(exforsys e1);friend int compute(exforsys e1);

//Friend Function Declaration with keyword //Friend Function Declaration with keyword friend and with the object of class exforsys to friend and with the object of class exforsys to which it is friend passed to itwhich it is friend passed to it

};};

int compute(exforsys e1)int compute(exforsys e1){{

//Friend Function Definition which has //Friend Function Definition which has access to private dataaccess to private datareturn int(e1.a+e1.b)-5;return int(e1.a+e1.b)-5;

}} main()main(){{

exforsys e;exforsys e;e.test();e.test();cout << "The result is:" << compute(e);cout << "The result is:" << compute(e);

//Calling of Friend Function //Calling of Friend Function

with object as argument.with object as argument.}}

Page 24: Oop Constructor  Destructors Constructor Overloading lecture 2

The function compute() is a non-member function of the class exforsys. In The function compute() is a non-member function of the class exforsys. In order to make this function have access to the private data a and b of class order to make this function have access to the private data a and b of class exforsys , it is created as a friend function for the class exforsys. As a first exforsys , it is created as a friend function for the class exforsys. As a first step, the function compute() is declared as friend in the class exforsys as:step, the function compute() is declared as friend in the class exforsys as:

friend int compute (exforsys e1)friend int compute (exforsys e1)

The keyword friend is placed before the function. The function definition isThe keyword friend is placed before the function. The function definition iswritten as a normal function and thus, the function has access to the privatewritten as a normal function and thus, the function has access to the privatedata a and b of the class exforsys. It is declared as friend inside the class, thedata a and b of the class exforsys. It is declared as friend inside the class, theprivate data values a and b are added, 5 is subtracted from the result, givingprivate data values a and b are added, 5 is subtracted from the result, giving295 as the result. This is returned by the function and thus the output is 295.295 as the result. This is returned by the function and thus the output is 295.

Example ExplanationExample Explanation