0% found this document useful (0 votes)
67 views15 pages

Constructor

Constructor is a special member function that is used to initialize objects. It has the same name as the class and is invoked automatically when an object is created. A copy constructor is used to initialize an object using another object of the same class. It takes a reference to the object as a parameter and copies its values. Destructor is the opposite of constructor and is used to destroy objects and release memory.

Uploaded by

Pushpak Deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views15 pages

Constructor

Constructor is a special member function that is used to initialize objects. It has the same name as the class and is invoked automatically when an object is created. A copy constructor is used to initialize an object using another object of the same class. It takes a reference to the object as a parameter and copies its values. Destructor is the opposite of constructor and is used to destroy objects and release memory.

Uploaded by

Pushpak Deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Constructor

Constructor
• Is a special member function used for
initializing an object.
• doesn’t have return any value.
• name of constructor is same as class name.
• doesn’t need to invoked by using an object.
• they should be declare in the public
section.
• may involve both
1. Constructor with default argument
2. Constructor with argument or
parameterize constructor.
• may define outside or inside of the class.
• Syntax for outside defining
class_name :: constructor_name(arguments)
{
constructor body;
}
E.g………
Class circle void main()
{ {
int r; circle a;
public: a.area();
void setdata(int n) a.setdata(5);
{ a.area();
r = n; }
}
void area()
{
float a = 3.14 * r * r;
cout<<“Area is:”<<a;
}
circle()
{
r = 1;
}
};
E.g…Of parameterize constructor
Class circle void main()
{ {
int r; circle a(5),b(2);
public: a.area();
void area() b.area();
{ }
float a = 3.14 * r * r;
cout<<“Area is:”<<a;
}
circle(int n)
{
r = n;
}
};
E.g…Of constructor with default argument
Class circle void main()
{ {
int r; circle a(5),b(2),c;
public: a.area();
void area() b.area();
c.area();
{ }
float a = 3.14 * r * r;
cout<<“Area is:”<<a;
}
circle(int n = 1)
{
r = n;
}
};
Destructor
• Is a member function, same as constructor.
• name of destructor is same as class name
but preceded by tilde (~) operator.
• Is used to destroy the object of a class.
• Used delete operator when destructor is call.
• doesn’t have return type.
• doesn’t take any argument.
• They are used to release the resources used by
the object.
• Is invoke implicitly.
int count = 0;
class alpha int main()
{ {
public: cout<<“Enter main”;
alpha() alpha a1,a2,a3,a4;
{ {
count++; cout<<“Enter block1”;
cout<<“No. Of object alpha a5;
created”<< count; }
} {
~alpha() cout<<“Enter block2”;
{ alpha a6;
cout<<“No. Of object }
destroyed”<< count; cout<<“Re-enter main”;
count--; return 0;
} }
};
Constructor Overloading
• Defining multiple constructor in a class.
• Number of arguments or types of argument for
each constructor must be different.
How the compiler decide which constructor to
call while creating an object?
E.g……. circle()
class circle {
{ r = 1;
int r; }
public: };
void area() void main()
{ {
float a = 3.14 * r*r; circle a(5),b(2),c;
cout<<“area is”<<a; a.area();
} b.area();
circle(int n) c.area();
{ }
r=n;
}
E.g……
class student student()
{ {
int roll_no; cout<<“Enter roll no. &
char name[10]; name”;
public: cin>>roll_no>>name;
void showinfo() }
{ };
cout<<“Roll no. void main()
is:”<<roll_no; {
cout<<“Name student a(1234,”john”),
is:”<<name; b(2388,”jill”),c;
} a.showinfo();
student(int m,char n) b.showinfo();
{ c.showinfo();
roll_no = m; }
strcpy(name,n);
}
Copy Constructor
Copy Constructor
• Used to declare and initialize an object from
another object.
Syntax :
class_name object_2(object_1);
or class_name object_2 = object_1;
E.g……
student s2(s1);
or student s2 = s1;
• The process of initializing through a copy
constructor is known as copy initialization.
• It takes a reference to an object of the same class as
an argument.
E.g…. int main()
class code {
{ code A(100);
int id; code B(A);
public: code C = A;
code() code D;
{ D = A;
} cout<<“id of A:”;
code(int a) A.display();
{ cout<<“id of B:”;
id = a; B.display();
} cout<<“id of C:”;
code(code &x) C.display();
{ cout<<“id of D:”;
id = x.id; D.display();
} }
void display(void)
{
cout<<id;
}
};

You might also like