The constructor is a special type of member function used to initialize the Object data members with a specific value at the time of its creation and constructor can also be used to allocate the memory.A constructor can be overloaded too.
In C++, a default constructor is a constructor that can be called without any arguments. If a class does not explicitly define any constructors, the compiler provides a default constructor automatically. The default constructor initializes the object's data members to default values or leaves them uninitialized if no initialization is specified.
class ClassName { public: // Default constructor declaration ClassName() { } };
#include<iostream> using namespace std; class student { private: int roll; int marks; public: student() { roll = 1001; marks = 90; } void display() { cout<< "Roll Number is :\t" <<roll << endl; cout<< "Marks is : \t" <<marks << endl; } }; int main() { student s; s.display(); }
Roll Number is : 1001 Marks is : 90
#include<iostream> #include<string.h> using namespace std; class Student { //scope is private, roll and marks are member variables private : int roll; char name[30]; int marks; public : Student() // Unparameterized construcotor { roll=1; strcpy(name,"MR"); marks =100; } void display() { cout<<"Roll Number is :"<<roll<<endl; cout<<"Name is :"<<name<<endl; cout<<"Marks is :"<<marks<<endl; } }; int main() { Student s; // Object Creation with unparameterized constructor s.display(); }
Roll Number is :1 Name is :MR Marks is :100 -------------------------------- Process exited after 0.08815 seconds with return value 0 Press any key to continue . . .
#include<iostream> #include<string.h> using namespace std; class Student { private : int roll; char name[30]; int marks; public : // Parameterized construcotor Student(int r,char n[], int m) { roll=r; strcpy(name,n); marks =m; } void display() { cout<<"Roll Number is :"<<roll<<endl; cout<<"Name is :"<<name<<endl; cout<<"Marks is :"<<marks<<endl; } }; int main() { // Object Creation with parameterized constructoer Student s(101,"Ram",98); s.display(); }
Roll Number is :101 Name is :Ram Marks is :98 -------------------------------- Process exited after 0.1162 seconds with return value 0 Press any key to continue . . .
#include<iostream> #include<string.h> using namespace std; class Student { private : int roll; char name[30]; int marks; public: void display() { cout<<"Roll Number is :"<<roll<<endl; cout<<"Name is :"<<name<<endl; cout<<"Marks is :"<<marks<<endl; } }; main() { Student s; // Object Creation s.display(); Student *s1= new Student();// Dynamic type object creation s1->display(); }
Roll Number is :1 Name is : Marks is :0 Roll Number is :0 Name is : Marks is :0 -------------------------------- Process exited after 0.1099 seconds with return value 0 Press any key to continue . . .
#include<iostream> #include<string.h> using namespace std; class Student { private : int roll=101; char name[30]={'A','n','i','l','\0'}; //accepted //char name[30]="String"; //accepted int marks=90; public: void display() { cout<<"Roll Number is :"<<roll<<endl; cout<<"Name is :"<<name<<endl; cout<<"Marks is :"<<marks<<endl; } }; int main() { Student s; // Object Creation with unparameterized constructoer s.display(); Student *s1= new Student();// Dynamic type object creation s1->display(); }
Roll Number is :101 Name is :Anil Marks is :90 Roll Number is :101 Name is :Anil Marks is :90 -------------------------------- Process exited after 0.1941 seconds with return value 0 Press any key to continue . . .
#include<iostream> #include<string.h> using namespace std; class Student { private : //static int roll=101; //Error because without const static const int roll=101; //acceptable char name[30]="String"; int marks=90; public: void display() { cout<<"Roll Number is :"<< roll <<endl; cout<<"Name is :"<<name<<endl; cout<<"Marks is :"<<marks<<endl; } }; int main() { Student s; // Object Creation with unparameterized constructoer s.display(); Student *s1= new Student();// Dynamic type object creation s1->display(); }
Roll Number is :101 Name is :String Marks is :90 Roll Number is :101 Name is :String Marks is :90 -------------------------------- Process exited after 0.2017 seconds with return value 0 Press any key to continue . . .
#include<iostream> using namespace std; class Code{ int a,b; public : Code() // unparameterizde Constructor { a = 10; b = 20; } Code(Code& i);// Copy Constructor declared void display(void) { cout<<"a = "<<a<<" b ="<<b<<"\n"; } }; Code :: Code(Code& i) // copy Constructor defined { a = i.a; b = i.b; } int main() { Code c1,c2(c1); // Copy constructor calling cout<<endl<<"First object"<<endl; c1.display(); cout<<endl<<"Second object "<<endl; c2.display(); cout<<endl<<"Third Object :"<<endl; Code c3 =c2; // Another way to call the copy constructor c3.display(); }
First object a = 10 b =20 Second object a = 10 b =20 Third Object : a = 10 b =20 -------------------------------- Process exited after 0.1845 seconds with return value 0 Press any key to continue . . .
#include<iostream> using namespace std; class Code { int a,b,c; public : Code() // unparametrized constructor { a=0; b=0; c=0; } Code(int i, int j); // parameterized constructor Code(int i, int j, int k) { a =i; b =j; c =k; } void display(void); }; Code :: Code(int i, int j) //constructor definition { a =i; b =j; } void Code :: display() { cout<<"a= "<<a<<" b="<<b<<" c="<<c<<'\n'; } int main() { Code a=Code();// Unparametrized Constructor Calling a.display(); Code c(5,10); // Two Parameterized Calling c.display(); Code d=Code(3,9,4); // Three Parameterized Calling d.display(); }
a= 0 b=0 c=0 a= 5 b=10 c=21 a= 3 b=9 c=4 -------------------------------- Process exited after 0.1692 seconds with return value 0 Press any key to continue . . .
#include<iostream> #include<string.h> using namespace std; class DC //DC -> Dynamic Constructor { char *str; int length; public : DC (char *s) { length =strlen(s); str = new char[length+1]; //get memory strcpy(str,s);// copy argument to it } void display() { cout<<"String = "<<str<<endl; } }; int main() { DC name1 = "Dynamic Constructors";// Excepted DC name2 ("Dynamic Constructors");// Excepted name1.display(); name2.display(); }
String = Dynamic Constructors String = Dynamic Constructors -------------------------------- Process exited after 0.1861 seconds with return value 0 Press any key to continue . . .
#include <iostream> class MyClass { public: static int count; // Declaration of static data member MyClass() { count++; // Increment count each time an object is created } ~MyClass() { count--; // Decrement count each time an object is destroyed } }; int MyClass::count = 0; // Definition of static data member int main() { // Create objects MyClass obj1; MyClass obj2; MyClass obj3; std::cout << "Number of objects created: " << MyClass::count << std::endl; // Create more objects MyClass obj4; MyClass obj5; std::cout << "Number of objects created: " << MyClass::count << std::endl; return 0; }
#include<iostream> using namespace std; class Destructor { int a,b; public: Destructor(int i, int j); void display(void); Destructor(); ~Destructor(); // ~Destructor() // { // cout<<"\n Destructor Called"; // } }; Destructor :: Destructor() { cout<<"Constructor."<<endl; } Destructor :: Destructor(int i, int j) { a=i; b=j; } void Destructor :: display() { cout<<a<<"\t"<<b<<endl; } Destructor :: ~Destructor() { cout<<"\nDestructor Called"<<endl; } int main() { Destructor(); Destructor c(5,10); c.display(); }
Constructor. Destructor Called 5 10 Destructor Called -------------------------------- Process exited after 0.1698 seconds with return value 0 Press any key to continue . . .
#include<iostream> using namespace std; class A { public: A() { cout << "Constructor called"<<endl; } ~A() { cout << "Destructor called"<<endl; } }; int main() { A obj1; // Constructor Called int x=1; if(x) { A obj2; // Constructor Called } // Destructor Called for obj2 }
Constructor called Constructor called Destructor called Destructor called -------------------------------- Process exited after 0.1736 seconds with return value 0 Press any key to continue . . .