Advertisement

Constructor in C++ || Code with Abuzar

    Through this Blog  we are learning about Constructor & Default Constructor ln C++


What is Constructor ?

Constructor is a member function that of a class which initialize the object  . In C++ constructor is automatically called when it's object called .



Constructor is different from normal constructor 

  1. Constructor is automatically called when it's object created
  2. Constructor don't have return type 
  3. Constructor has same name type itself.


       How many types of Constructor in C++

https://abuzartec.blogspot.com/2021/06/constructor-in-c.html

Let's us understand the type of constructor in C++ by taking the real life example .

Suppose you went to a shop for buy a Tv , what are the options ? , First you go to the shop and buy a Tv . but here you can not tell that which type of TV you want ( black & white or Colorful ) so  here this is same as default constructor , Second you went to the shop and says such parameter like brand name or color so the shopkeeper will give you what you want , this is same as parameterized constructor . Third one !  , You go to the shop and says you buy a Tv like this ( a pyhsical Tv in your hand ) So the shopkeeper will see that Tv . Okay , he will give a new Tv like that so this same as copy constructor .


Default Constructor 

Default constructor is a also a type of constructor which doesn't   take any argument or does not pass any parameter.


Example :

// Cpp program to illustrate the

// concept of Constructors
#include <iostream>
using namespace std;
 
class construct
{
public:
    int a, b;
 
    // Default Constructor
    construct()
    {
        a = 10;
        b = 20;
    }
};
 
int main()
{
 /* Default constructor called
automatically when the object is created*/
    construct c;
    cout << "a: " << c.a << endl
         << "b: " << c.b;
    return 1;
}



Output :


    a : 10

    b : 20



Important Note : If we do not define any type of

constructor so compiler will automatically called . 




Parameterized Constructor 

Parametrized constructor are the those type of constructor which passes arguments that help us to help us to call easily when it's object created .


Example :

// CPP program to illustrate
// parameterized constructors

#include <iostream>
using namespace std;
 
class Point
{
private:
    int x, y;
 
public:
    // Parameterized Constructor
    Point(int x1, int y1)
    {
        x = x1;
        y = y1;
    }
 
    int getX()
    {
        return x;
    }
    int getY()
    {
        return y;
    }
};
 
int main()
{
// Constructor called
Point p1(10, 15);
 
//Access values assigned by constructor
cout<<"p1.x = "<< p1.getX()<<",p1.y ="
<< p1.getY();
 
return 0;
}


Output :

    
p1.x = 10, p1.y = 15



When an object is declared it passes a value that helps us to call the constructor .

The normal way of constructor is not working .




Copy Constructor 

Copy constructor is a member function which initialize an object which using another object  .


Example :

#include<iostream>
using namespace std;
 
class Point
{
private:
    int x, y;
public:
Point(intx1, inty1){x =x1; y =y1;}
 
 // Copy constructor
 Point(constPoint &p1){x=p1.x; y=p1.y;}
 
    intgetX()        { return x;}
    intgetY()        { return y;}
};
 
int main()
{
// Normal constructor is called here
 Point p1(10, 15);
// Copy constructor is called here
 Point p2 = p1;
 
// Let us access values assigned by Con.
cout<<"p1.x ="<<p1.getX()<<",p1.y="
<<p1.getY();

cout<<"\np2.x ="<<p2.getX()<<",p2.y ="
<<p2.getY();
 
    return 0;
}


            Output : 

    p1.x = 10, p1.y = 15

     p2.x = 10, p2y = 15 


  Constructor Video : Click Here 

  This is my YouTube channel : Click Here

  Playlist of C++(Full Course) : Click Here

   Playlist of C++(Program ) : Click Here 


Please ! write comment if you find anything Incorrect .

This article is contributed by Mohd Abuzar .If you want to share some information please mail at : abuzar.abuzar.mohd325@gmail.com

Post a Comment

0 Comments