You are on page 1of 10

Constructor

class MyClass
{     
public:           
    MyClass() {     // Constructor
      cout << "Hello World!";
    }
};

int main() {
  MyClass myObj;    // Create an object of MyClass (this will call the constructor)
  return 0;
}
1) Default Constructor
A default constructor doesn’t have any arguments (or parameters)
2) Parameterized Constructor
Constructors with parameters are known as Parameterized constructors.
These type of constructor allows us to pass arguments while object
creation. 

Default constructor:
XYZ() {
} ....
XYZ obj;
....
Parameterized Constructor:
XYZ(int a, int b) {
} ...
XYZ obj(10, 20);
Default Constructor
#include <iostream>
using namespace std;
class Website
{
public:
//Default constructor
Website()
{
cout<<"Welcome to BeginnersBook"<<endl;
}
};
int main()
{
Website obj1;
Website obj2;
return 0;
}

Output:
Welcome to BeginnersBook
Welcome to BeginnersBook
#include <iostream>
using namespace std;
class Add
{ public:
//Parameterized constructor
Add(int num1, int num2)
{
cout<<(num1+num2)<<endl;
}
};
int main(void)
{
Add obj1(10, 20);
}

Output: 30
class Car {        // The class
  public:          // Access specifier
    string brand;  // Attribute
    string model;  // Attribute
    int year;      // Attribute
  
  Car(string x, string y, int z) { // Constructor with parameters
      brand = x;
      model = y;
      year = z;
    }
};

int main() {
  // Create Car objects and call the constructor with different values
  Car carObj1("BMW", "X5", 1999);
  Car carObj2("Ford", "Mustang", 1969);

  // Print values


  cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
  cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
  return 0;
}
Default Copy constructor
class Distance
{
private:
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0) { }
Distance(int ft, float in) : feet(ft), inches(in) { }
void getdist()
{
cout << “\nEnter feet: “;
cin >> feet;
cout << “Enter inches: “;
cin >> inches;
}
void showdist()
{
cout << feet << “\’-” << inches << ‘\”’;
}
};
int main()
{
Distance dist1(11, 6.25);
Distance dist2(dist1);
Distance dist3 = dist1;
cout << “\ndist1 = “;
dist1.showdist();
cout << “\ndist2 = “;
dist2.showdist();
cout << “\ndist3 = “;
dist3.showdist();
cout << endl;
return 0;
}
Destructor is a member function which
destructs or deletes an object.
• A destructor function is called automatically
when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called 
#include<iostream>
using namespace std;
class Demo
{
private:
int num1, num2;
public:
Demo(int n1, int n2) Output
{ Inside Constructor
cout<<"Inside Constructor"<<endl; num1 = 10
num1 = n1; num2 = 20
num2 = n2; Inside Destructor
}
void display()
{
cout<<"num1 = "<< num1 <<endl;
cout<<"num2 = "<< num2 <<endl;
}
~Demo()
{
cout<<"Inside Destructor";
}
};
int main()
{
Demo obj1(10, 20);
obj1.display();
}

You might also like