what is constructor in C++
A constructor is a special member function that is automatically called when an object of the class is created.
characteristics of constructor
1. Name Matching: Constructors must have the same name as their class. This naming convention is essential for the compiler to identify and associate the constructor with the class.
2. No Return Type: Constructors do not specify a return type, unlike other functions. This is because they automatically return the newly created object.
3. Automatic Invocation: Constructors are invoked automatically when an object of the class is created. This eliminates the need for explicit function calls, making the code more intuitive and efficient.
4. Initialization: Constructors are primarily used for initializing the object's member variables, and setting their initial values. This initialization is vital for ensuring that the object is in a consistent and predictable state from the outset.
how to create a constructor
syntax:
class class_name
{
class_name()
{
//code to execute
}
};
example:
#include<iostream>
using namespace std;
class Letsmakestudydown
{
public:
//creating constructor
Letsmakestudydown()
{
cout<<"object is created for letsmakestudydown";
}
};
int main()
{
Letsmakestudydown obj;
return 0;
}
types of constructor
Default Constructor:
Definition: A default constructor is one that doesn't take any arguments.
Purpose: It initializes the object's data members to their default values.
Example:
#include<iostream>
using namespace std;
class MyClass
{
public:
MyClass()
{
cout<<"object is created for myclass";
}
};
int main()
{
MyClass obj;
return 0;
}
Parameterized Constructor:
Definition: A parameterized constructor accepts one or more arguments.
Purpose: It initializes the object's data members based on the values provided as arguments.
Example 1:
#include<iostream>
using namespace std;
class Point
{
public:
int x, y;
Point(int xCoord, int yCoord)
{
x = xCoord;
y = yCoord;
}
};
int main()
{
Point obj;
return 0;
}
Example 2:
#include<iostream>
using namespace std;
class Employee
{
public:
int id ;
string name;
Employee(int i,string n)
{
id = i;
name = n;
}
void showdetails()
{
cout<<"name is "<<name;
cout<<" id is "<<id<<endl;
}
};
int main()
{
Employee obj(101,"sam");
Employee obj2(102,"max");
obj2.showdetails();
obj.showdetails();
return 0;
}
Copy Constructor:
Definition: A copy constructor is used to create a new object as a copy of an existing object.
Purpose: It ensures that a new object has the same values as the original object.
Example:
#include <iostream>
using namespace std;
class HouseWall
{
public:
double length;
double height;
HouseWall(double l, double h)
{
length = l;
height = h;
}
// copy constructor with a HouseWall object as parameter
// copies data of the obj parameter
HouseWall(HouseWall &obj)
{
length = obj.length;
height = obj.height;
}
double calculateAreaOfWall()
{
return length * height;
}
};
int main()
{
HouseWall wall1(8.7, 3.2);
// copy contents of wall1 to wall2
HouseWall wall2 = wall1;
// print areas of wall1 and wall2
cout << "Area of Wall 1: ";
cout<< wall1.calculateAreaOfWall() ;
cout<< endl;
cout << "Area of Wall 2: ";
cout<< wall2.calculateAreaOfWall();
cout<<endl;
return 0;
}
Destructor:
Definition: Although not technically a constructor, a destructor is an essential part of class definition.
destructor name must be the same as the class name.
destructor starts with Tilde: ~
Purpose: It cleans up resources (e.g., memory) allocated by the object, ensuring proper resource management.
Example:
#include<iostream>
using namespace std;
class MyResource
{
public:
MyResource()
{
cout<<"object is created for myresource"<<endl;
}
//creating destructor
~MyResource()
{
cout<<"object is deleted for myresource"<<endl;
}
};
int main()
{
MyResource s;
return 0;
}
Constructor Overloading:
Definition: You can define multiple constructors with different parameter lists in a class.
Purpose: This allows you to create objects in various ways, providing flexibility in object initialization.
Example :
#include<iostream>
using namespace std;
class Student
{
public:
int age;
// Default constructor
Student()
{
cout<<"deafult constructor is called"<<endl;
}
Student(int a)
{
age=a;
cout<<"age is stored"<<endl;
}
};
int main()
{
Student obj;
Student obj1(101);
return 0;
}
0 Comments