0% found this document useful (0 votes)
15 views5 pages

EXP-5 Constructors and Destructors

The document explains constructors and destructors in C++, detailing their types such as default, parameterized, and copy constructors. It includes example C++ programs demonstrating the use of these constructors and the destructor, illustrating how they are invoked automatically during object creation and destruction. The document emphasizes the role of constructors in initializing object data and destructors in cleaning up before an object is destroyed.

Uploaded by

23shiv.prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

EXP-5 Constructors and Destructors

The document explains constructors and destructors in C++, detailing their types such as default, parameterized, and copy constructors. It includes example C++ programs demonstrating the use of these constructors and the destructor, illustrating how they are invoked automatically during object creation and destruction. The document emphasizes the role of constructors in initializing object data and destructors in cleaning up before an object is destroyed.

Uploaded by

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

5. Study of constructors and destructors.

 A constructor is a special member function that is called automatically when an object is


created.
 In C++, a constructor has the same name as that of the class, and it does not have a return type
 A constructor with no parameters is known as a default constructor.
 In C++, a constructor with parameters is known as a parameterized constructor. This is the
preferred method to initialize member data.
 The copy constructor in C++ is used to copy data from one object to another.
 Destructor is an instance member function that is invoked automatically whenever an object is
going to be destroyed. Meaning, a destructor is the last function that is going to be called before
an object is destroyed.

1. // C++ program to demonstrate the use of default constructor

#include <iostream>
using namespace std;

class Person{

// declaring private class data members


private:
string name;
int age;

public:

// declaring constructor
Person()
{
cout<<"Default constructor is called"<<endl;
name = "student";
age = 12;
}

// display function to print the class data members value


void display()
{
cout<<"Name of current object: "<<name<<endl;
cout<<"Age of current object: "<<age<<endl;
}

};
int main()
{
// creating object of class using default constructor
Person obj;

// printing class data members


obj.display();

return 0;
}
Output
Default constructor is called
Name of current object: student
Age of current object: 12

2. // C++ program to demonstrate the use of parameterized constructor

#include <iostream>
using namespace std;

class Person{

// declaring private class data members


private:
string name;
int age;

public:

// declaring parameterized constructor of three different types


Person(string person_name)
{
cout<<"Constructor to set name is called"<<endl;
name = person_name;
age = 12;
}

Person(int person_age)
{
cout<<"Constructor to set age is called"<<endl;
name = "Student";
age = person_age;
}

Person(string person_name, int person_age)


{
cout<<"Constructor for both name and age is called"<<endl;
name = person_name;
age = person_age;
}
// display function to print the class data members value
void display()
{
cout<<"Name of current object: "<<name<<endl;
cout<<"Age of current object: "<<age<<endl;
cout<<endl;
}

};
int main()
{
// creating objects of class using parameterized constructor
Person obj1("First person");

// printing class data members for first object


obj1.display();

Person obj2(25);

// printing class data members for second object


obj2.display();

Person obj3("Second person",15);

// printing class data members for third object


obj3.display();
return 0;
}
Output
Constructor to set name is called
Name of current object: First person
Age of current object: 12

Constructor to set age is called


Name of current object: Student
Age of current object: 25

Constructor for both name and age is called


Name of current object: Second person
Age of current object: 15

3. // C++ program to illustrate the use of copy constructor


#include <iostream>
#include <string.h>
using namespace std;

// Class definition for 'student'


class student {
int rno;
string name;
double fee;

public:
// Parameterized constructor
student(int, string, double);
// Copy constructor
student(student& t)
{
rno = t.rno;
name = t.name;
fee = t.fee;
cout << "Copy Constructor Called" << endl;
}
// Function to display student details
void display();
};

// Implementation of the parameterized constructor


student::student(int no, string n, double f)
{
rno = no;
name = n;
fee = f;
}

// Implementation of the display function


void student::display()
{
cout << rno << "\t" << name << "\t" << fee << endl;
}

int main()
{
// Create student object with parameterized constructor
student s(1001, "Manjeet", 10000);
s.display();

// Create another student object using the copy


// constructor
student manjeet(s);
manjeet.display();

return 0;
}

Output
1001 Manjeet 10000
Copy Constructor Called
1001 Manjeet 10000

4. // C++ program to demonstrate the execution of destructor

#include <iostream>
using namespace std;

class Test {
public:
// User-Defined Constructor
Test() { cout << "\n Constructor executed"; }

// User-Defined Destructor
~Test() { cout << "\nDestructor executed"; }
};
main()
{
Test t;

return 0;
}

Output
Constructor executed
Destructor executed

You might also like