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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…