Constructor and Destructor
Constructor and Destructor
To create a constructor, use the same name as the class, followed by parentheses ():
#include <iostream>
int main() {
MyClass myObj; // Create an object of MyClass (this will call the constructor)
return 0;
}
Constructor Parameters
Constructors can also take parameters (just like regular functions), which can be useful for setting
initial values for attributes.
#include <iostream>
using namespace std;
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
Just like functions, constructors can also be defined outside the class. First, declare the constructor
inside the class, and then define it outside of the class by specifying the name of the class, followed by
the scope resolution :: operator, followed by the name of the constructor (which is the same as the
class):
#include <iostream>
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
// Simple C++ program of Constructor demonstration
#include <iostream>
using namespace std;
class student
{
public:
student() // user defined constructor
{
cout<<"object is initialized"<<endl;
}
};
int main ()
{
student x,y,z;
#include <iostream>
class Overclass
public:
int x;
int y;
Overclass() { x = y = 0; }
Overclass(int a) { x = y = a; }
Overclass(int a, int b) { x = a; y = b; }
};
int main()
Overclass A;
Overclass A1(4);
cout << "Overclass A's x, y value:: " << A.x << " , "<< A.y << "\n";
cout << "Overclass A1's x,y value:: "<< A1.x << " ,"<< A1.y << "\n";
cout << "Overclass A2's x,y value:; "<< A2.x << " , "<< A2.y << "\n";
return 0;
}
#include <iostream>
using namespace std;
class student {
int rno;
char name[10];
double fee;
public:
student()
{
cout << "Enter the RollNo:";
cin >> rno;
cout << "Enter the Name:";
cin >> name;
cout << "Enter the Fee:";
cin >> fee;
}
void display()
{
cout << endl << rno << "\t" << name << "\t" << fee;
}
};
int main()
s.display();
return 0;
}
// Simple C++ program demonstrating Constructor defined outside the class
#include <iostream>
class student {
int rno;
char name[50];
double fee;
public:
student();
void display();
};
student::student()
void student::display()
cout << endl << rno << "\t" << name << "\t" << fee;
int main()
student s;
s.display();
return 0;
}
// Simple C++ program of Parameterized Constructor demonstration
//Note: when the parameterized constructor is defined and no default constructor is defined
explicitly, the compiler will not implicitly call the default constructor and hence creating a
simple object as
#include<iostream>
#include<string.h>
class student
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
void display();
};
rno=no;
strcpy(name,n);
fee=f;
void student::display()
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
int main()
student s(1001,"Ram",10000);
s.display();
return 0;
}
// Simple C++ program of copy Constructor demonstration
#include<iostream>
#include<string.h>
class student
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
rno=t.rno;
strcpy(name,t.name);
fee=t.fee;
void display();
};
student::student(int no,char n[],double f)
rno=no;
strcpy(name,n);
fee=f;
void student::display()
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
int main()
student s(1001,"Manjeet",10000);
s.display();
manjeet.display();
return 0;
}
#include <iostream>
class student
int roll;
char name[30];
public:
roll =10;
strcpy(name,"x");
roll =O.roll;
strcpy(name, O.name);
}
void input_data()
void show_data()
};
int main()
s.show_data();
A.show_data();
return 0;
}
// C++ program of Destructor demonstration
#include <iostream>
public :
student ()
int i,j;
i=10;
j=5;
};
int main()