OOP Lec 5 (Constructor&Destructor)
OOP Lec 5 (Constructor&Destructor)
OOP Lec 5 (Constructor&Destructor)
};
int main()
{
Hello x,y,z;
return 0;
}
How above program works
• The above program declares a constructor that displays a message on the
screen.
• The program creates three object in main() function .
• The constructor is executed each time an object of the class is created in the
memory.
Write a class that contains two integer data members which are initialized to 100 when an object is created. It
has a member function avg that displays the average of data members.
#include <iostream>
using namespace std;
class Number
{ int main()
private:
int x,y; {
public:
Number()
{
Number
}
x=y=100; n;
void avg()
{ n.avg();
cout<<"x="<<x<<endl;
cout<<"y"<<y<<endl; return 0;
cout<<"Average="<<(x+y)/2<<endl;
} }
};
Passing parameters to Constructors
• The method of passing parameters to the constructor is same as passing
parameters to normal functions.
• The only difference that parameters are passed to the constructor when the
object is declared.
• The parameters are written parenthesis along with the object name in
declaration statement.
Syntax
The syntax of passing parameters to constructor is as follows:
Type object_name(parameters);
Where,
type It is the name of class and indicates the type of object to be
created
Object_name It indicates the name of object to be created.
Parameters It indicates the list of parameters passed to constructor.
Write a class that has marks and grade as data members. A constructor with two parameters
initializes data members with the given values and member functions show displays the
values of data members. Create two objects and displays the values.
void show()
#include <iostream> {
cout<< "Marks="<<marks<<endl;
cout<<"Grades="<<grade<<endl;
using namespace std;
}
class Student
};
{
private:
int main()
int marks; {
char grade; Student s1(730,'A'),s2(621,'B');
public: cout<<"Record of Students 1:"<<endl;
Student (int m, char g) s1.show();
{ cout<<"Record of student 2:"<<endl;
marks=m; s2.show();
grade=g; return 0;
} }
Constructor Overloading
• The process of declaring multiple constructor with same name but different
parameters is known as constructor overloading.
• The constructor with same name must differ in one of the following ways:
1. Number of parameters
2. Types of parameters
3. Sequence of parameters
Write a class that has num and ch as data members. A constructor with no parameter initializes num to 0 and ch to ‘x’.
A constructor with two parameters initializes data members with the given values and member function show displays the values of data members
....
int main()
{
Area A2(2, 1);
# include<iostream>
using namespace std;
class Test
{
private:
int n;
public:
Test()
{
cout<<"object Created..."<<endl;
}
~Test()
{
cout<<"object destroyed..."<<endl;
}
};
int main()
{
Test a,b,c;
}
How Above Program works
• The above program creates a constructor and destructor in the class .
• Both display simple messages on the screen.
• The message “object created ..” will appear when the program is executed.
• The message “object destroyed ..” will appear when the program is terminated
and all objects are destroyed from memory.
End of lecture