0% found this document useful (0 votes)
22 views11 pages

Chap 0205

Uploaded by

udyadav430
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)
22 views11 pages

Chap 0205

Uploaded by

udyadav430
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/ 11

Lecture – 10

• Constructor
• Destructor
Constructor
The process of initializing and starting up a class is called construction.

A constructor is a special member function of the class which has the same name as that of the
class. It is automatically invoked when we declare/create new objects of the class. It is called
constructor, because it constructs the value of data members of the class.

Special characteristics of constructor as follows –

1. They should be declared in the public section.


2. They are called automatically when the objects are created.
3. They do not have return types, not even void and they cannot return values.
4. They cannot be inherited, though a derived class can call the base class constructor.
5. Like other C++ functions, they can have default arguments.
6. Constructors cannot be virtual.
7. They cannot be referred by their addresses.
Functions of constructor :
1. calculate the size of an object
2. allocating memory space
3. calling an initializing code of super class
4. calling an initializing code of class being created

Construction Rules :
• The rule for constructing an object of a simple class is:
1. Call the constructor/initializer for each data member, in sequence.
2. Call the constructor for the class.

• The rule for constructing an object of a derived class is:

1. Call the constructor for the base class (which recursively calls the constructors needed to
Completely initialize the base class object.)
2. Call the constructor/initializer for each data member of the derived class, in sequence.
3. Call the constructor for the derived class
Syntax :

class class_name
{
public: class_name()
{
// Constructor code
}
//... other Variables & Functions
};

Types Of Constructor

 Default Constructor
 Parameterized Constructor
 Copy Constructor
1. Default Constructor
A default constructor does not have any parameters or if it has parameters, all
the parameters have default values.

Syntax:
class class_name
{
public:
// Default constructor with no arguments
class_name();
// Other Members Declaration
};

2. Parameterized Constructor
If a Constructor has parameters, it is called a Parameterized Constructor.
Parameterized Constructors assist in initializing values when an object is
created.
Example Program for Parameterized Constructor In C++
#include<iostream>
using namespace std;
class Employee
{
public:
int id; string name;
Output :
Employee(int i, string n) { 101 Sonoo
id = i; 102 Nakul
name = n;
}
void display() {
cout<<id<<" "<<name<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo");
e2=Employee(102, "Nakul");
e1.display();
e2.display();
return 0;
}
Copy Constructor
• A copy constructor is a like a normal parameterized Constructor, but which parameter is the
same class object.
• Copy constructor uses to initialize an object using another object of the same class.

The copy constructor is used to −


 Initialize one object from another of the same type.
 Copy an object to pass it as an argument to a function. Copy an object to return it from a
function.

A copy constructor is a member function which initializes an object using another object of the
same class.

A copy constructor has the following general function prototype:

ClassName (constClassName&old_obj);
Following is a simple example of copy constructor.
#include<iostream>
using namespace std;
class Point {
int x, y;
public:
Point(int x1, int y1) {
x = x1; y = y1;
} Output :
P1.x=10, p1.y=15
Point(const Point &p2) {x = p2.x; y = p2.y; } P2.x=10, px.y=15
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
cout<< "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout<< "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}
Destructor
Destructor is a member function which destructs or deletes an object. The destructor function has the
same as the constructor, but it is preceded by a (tilde sign).
1. Destructors are special member functions of the class required to free the memory of the
object whenever it goes out of scope.
2. Destructors are parameter less functions.
3. Name of the Destructor should be exactly same as that of name of the class. But preceded by ‘~’ (tilde).
4. Destructor does not have any return type. Not even void.
5. The Destructor of class is automatically called when object goes out of scope.

Destruction Rules:
When an object is deleted, the destructors are called in the opposite order.
The rule for an object of a derived class is:
1. Call the destructor for the derived class.
2. Call the destructor for each data member object of the derived class in reverse sequence.
3. Call the destructor for the base class.
Example
};
#include<iostream>
usingnamespace std; int main( )
class Marks {
{ Marks m1;
public: Marks m2;
int maths , science; return 0;
}
Marks() //constructor
{
Output
cout<< "Inside Constructor"<<endl; Inside Constructor
cout<< "C++ Object created"<<endl; C++ Object created
} Inside Constructor
C++ Object created
~Marks() //destructor
{ Inside Destructor
cout<< "Inside Destructor"<<endl; C++ Object destructed
cout<< "C++ Object destructed"<<endl; Inside Destructor
} C++ Object destructed
Do Subscribe

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy