class class_name
{
//Statements
public
class_name ([parameter list])
{
//Constructor's Body
}
};
There are four types of constructors in c++
#include <iostream>
using namespace std;
class Student{
// declaring private class data members
private:
string name;
int age;
public:
// declaring constructor
Student()
{
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
Student obj;
// printing class data members
obj.display();
return 0;
}
Default constructor is called
Name of current object: student
Age of current object: 12
#include <iostream>
using namespace std;
class Student{
// declaring private class data members
private:
string name;
int age;
public:
// declaring constructor
Student(string n, int a)
{
cout<<"Default constructor is called"<<endl;
name = n;
age = a;
}
// 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
Student obj("Rabecca",15);
// printing class data members
obj.display();
return 0;
}
Default constructor is called
Name of current object: Rabecca
Age of current object: 15
#include <iostream>
using namespace std;
class Student{
// declaring private class data members
private:
string name;
int age;
public:
// declaring constructor
Student(string n, int a)
{
cout<<"Default constructor is called"<<endl;
name = n;
age = a;
}
Student(const Student& obj)
{
cout<<"Copy constructor is called"<<endl;
name = obj.name;
age = obj.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;
}
};
int main()
{
// creating object of class using default constructor
Student obj("Rabecca",15);
// printing class data members
obj.display();
Student obj2(obj);
// printing class data members
obj.display();
return 0;
}
Default constructor is called
Name of current object: Rabecca
Age of current object: 15
Copy constructor is called
Name of current object: Rabecca
Age of current object: 15
#include <iostream>
using namespace std;
class Student{
// declaring private class data members
private:
int* age;
public:
Student(int* Student_age)
{
cout<<"Constructor for age is called"<<endl;
// allocating memory
age = new int;
age = Student_age;
}
// display function to print the class data members value
void display()
{
cout<<"Age of current object: "<<*age<<endl;
cout<<endl;
}
};
int main()
{
// creating objects of class using parameterized constructor
int age = 25;
Student obj1(&age);
// printing class data members for first object
obj1.display();
return 0;
}
Constructor for age is called
Age of current object: 25
class class_name
{
//Statements
public
~class_name ([parameter list])
{
//Destructor's Body
}
};
#include <iostream>
using namespace std;
class Student{
// declaring private class data members
private:
string name;
int age;
public:
// declaring constructor
Student()
{
cout<<"Default constructor is called"<<endl;
name = "student";
age = 12;
}
~Student()
{
cout<<"Destructor 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
Student obj;
// printing class data members
obj.display();
return 0;
}
Default constructor is called
Name of current object: student
Age of current object: 12
Destructor is called
Note: also read about Function Overloading in C++
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…