0% found this document useful (0 votes)
69 views16 pages

Constructor and Destructor

Constructors are special methods in C++ classes that are automatically called when an object is created. They can be defined with or without parameters to initialize attributes. Constructors can be defined inside or outside the class. Copy constructors are used to initialize one object using another object of the same class. Destructors are called when an object is destroyed in order to perform cleanup tasks.

Uploaded by

amolia2004
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)
69 views16 pages

Constructor and Destructor

Constructors are special methods in C++ classes that are automatically called when an object is created. They can be defined with or without parameters to initialize attributes. Constructors can be defined inside or outside the class. Copy constructors are used to initialize one object using another object of the same class. Destructors are called when an object is destroyed in order to perform cleanup tasks.

Uploaded by

amolia2004
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/ 16

Constructors

A constructor in C++ is a special method that is automatically called when an object of a class is


created.

To create a constructor, use the same name as the class, followed by parentheses ():

//Simple constructor codein c++

#include <iostream>

using namespace std;

class MyClass { // The class


public: // Access specifier
MyClass() { // Constructor
cout << "Hello World!";
}
};

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.

// Simple Constructor code of constructor Parameters

#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):

//Constructor defined outside the class using :: (scope resolution operator)

#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 declaration
};

// Constructor definition outside the class


Car::Car(string x, string y, int z) {
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;
}
// 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;

// Simple C++ program of parameterized Constructor demonstration and


overloaded trice

#include <iostream>

using namespace std;

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);

Overclass A2(8, 12);

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;
}

// Simple C++ program demonstrating Constructor defined inside the class

#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()

student s; // constructor gets called automatically when

// we create the object of the class

s.display();

return 0;
}
// Simple C++ program demonstrating Constructor defined outside the class

#include <iostream>

using namespace std;

class student {

int rno;

char name[50];

double fee;

public:

student();

void display();

};

student::student()

cout << "Enter the RollNo:";

cin >> rno;

cout << "Enter the Name:";

cin >> name;

cout << "Enter the Fee:";


cin >> fee;

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

// Parameterized Constructors: It is possible to pass arguments to constructors. Typically,


these arguments help initialize an object when it is created. To create a parameterized
constructor, simply add parameters to it the way you would to any other function. When
you define the constructor’s body, use the parameters to initialize the object.

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

using namespace std;

class student

int rno;

char name[50];

double fee;

public:

student(int,char[],double);

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,"Ram",10000);

s.display();

return 0;
}
// Simple C++ program of copy Constructor demonstration

#include<iostream>

#include<string.h>

using namespace std;

class student

int rno;

char name[50];

double fee;

public:

student(int,char[],double);

student(student &t) //copy constructor

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();

student manjeet(s); //copy constructor called

manjeet.display();
return 0;
}

// C++ program of Copy Constructor demonstration

#include <iostream>

#include <cstring> // OR You can also use string.h in other compilers

using namespace std;

class student

int roll;

char name[30];

public:

student() // default constructor

roll =10;

strcpy(name,"x");

student( student &O) // copy constrcutor

roll =O.roll;

strcpy(name, O.name);

}
void input_data()

cout<<"\n Enter roll no :"; cin>>roll;

cout<<"\n Enter name :"; cin>>name;

void show_data()

cout<<"\n Roll no :"<<roll;

cout<<"\n Name :"<<name;

};

int main()

student s; // default construtor

s.show_data();

student A(s); // copy constructor

A.show_data();

return 0;
}
// C++ program of Destructor demonstration

#include <iostream>

using namespace std;

class student // student is a class

public :

student ()

int i,j;

i=10;

j=5;

cout<<"The Value of I = "<<i<<" & J= "<<j;

~ student() // destructor declaration

cout <<"\n Thanx for using this program" <<endl;


}

};

int main()

student s1; // s1 is an object

You might also like