Constructor And Destructor in c++
WHAT IS CONSTRUCTOR:-
- constructor is a special type of method whose name is same as class name.
- a constructor in c++ is a special method that is automatically called when an object a class is created.
- constructor does not have a return value hence they do not have a return type. constructor must placed in public section of class.
syntax:-
class class_name
{
class_name() // this is a constructor
{
//body of program
}
};
syntax for defining the constructor outside the class.
classs_name : : class_name()
=> TYPE OF CONSTRUCTOR
- Default constructor
- parameterised constructor
- copy constructor
- Default constructor:-
A constructor with no parameter is know as default constructor
syntax:- class_name()
{
}
Example:-
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2;
return 0;
}
OUTPUT:-
Default Constructor Invoked
Default Constructor Invoked
2. Parameterised constructor:-
A constructor with parameterised is known as parameterised constructor this is the prefered method to initialize data member of a class.
syntax:-
class_name( with parameter )
{
}
class_name obj(with parameter)
Example:-
#include <iostream>
using namespace std;
class Employee {
public:
int id; //data member (also instance variable)
string name; //data member(also instance variable)
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) { Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
Employee e2=Employee(102, "Nakul", 59000);
e1.display();
e2.display();
return 0;
}
OUTPUT:-
101 Sonoo 890000
102 Nakul 59000
3. Copy constructor:-
The copy constructor in c++ is use copy data of one object to another object of the same class.
syntax:-
class_name( class name address of reference variable)
Example:-
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
// initialize variables with parameterized constructor
Wall(double len, double hgt) {
length = len;
height = hgt;
}
// copy constructor with a Wall object as parameter
// copies data of the obj parameter
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
double calculateArea() {
return length * height;
}
};
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);
// copy contents of wall1 to wall2
Wall wall2 = wall1;
// print areas of wall1 and wall2
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
OUTPUT:-
Area of wall 1: 90.3
Area of wall 2 : 90
WHAT IS DISTRUCTOR:-
- Destructors, conversely, are used to deallocate memory and destroy objects when they are no longer needed.
- A destructor is automatically invoked when the program ends or the object is no longer in use.
- A destructor enables an object to perform some actions or execute some code at the time of its destruction
- Destructors are typically invoked in the reverse order of the constructor invocation.
- distructor has the same name as the class name with tilde symbol (~)
Example:-
#include <iostream>
using namespace std;
class test {
public:
int y, z;
test()
{
y = 7;
z = 13;
}
~test(){ }
};
int main()
{
test a;
cout <<"the sum is: "<< a.y+a.z;
return 1;
}
OUTPUT:-
the sum is: 20.
crezy deals in flipkart:-
0 Comments