Constructor in C++

Constructor:
  • A constructor is one kind of feature in oops.
  • Constructor means, class name the same as the function or method name.
  • It is similar to the function so it’s called special member function and that class name the same as the constructor name.
  • It used to construct and initialize all the data members.
  • Constructor automatically called, when the object is created.
  • It can be defined inside the class and definitions are outside the class definition help of the class name.         

Special characteristics of the constructor:
  • A constructor should be declared in the public section.
  • The name should be equal to the class name followed by the parenthesis.
  • It does not have any return type, not even void.
  • They cannot be inherited through a derived class can call the base class constructor.
  • It cannot be a virtual function.
Types of constructor:
  • Three different types of constructors:
  1. Default constructor
  2. Parameterized constructor
  3. Copy constructor
Default constructor:
  • A constructor with no parameters is known as a default constructor.
Syntax:
  1. class class_name
    {
    Data members;
    Access specifiers:
    class_name ()
    {
              //Statements
    }
    }; 
Example:
  1. #include<iostream.h>
    #include<conio.h>
    class addition
    {
    int a,b;
    public:
    addition()//no parameter with constructor
    {
    cout<<"\n Enter a and b value: ";
    cin>>a>>b;
    cout<<"\n Addition value is:"<<a+b;
    }
    };
    void main()
    {
    clrscr();
    addition obj;//object creation
    getch();
    }
Output:
Parameterized constructor:
  • A constructor with parameters is known as a parameterized constructor.
Syntax:
  1. class class_name
    {
    Data members;
    Access specifiers:
    class_name (parameter list)
    {
              //Statements
    }
    }; 
Example:
  1. #include<iostream.h>
    #include<conio.h>
    class std
    {
    int roll_num,mark;
    char *name[10];
    public:
    std(int x,char *y,int z)
    {
    roll_num=x;
    *name=y;
    mark=z;
    cout<<"\n Roll Number\t Student Name\t Student total mark\n";
    cout<<"\n "<<roll_num<<"\t\t "<<*name<<"\t\t "<<mark<<endl;
    }
    };
    void main()
    {
    clrscr();
    std s1(01,"Ahamed",455);
    std s2(02,"Banu",344);
    getch();
    }
Output:
Copy constructor:
  • It is used when one object of class initializes another object.
  • It takes a reference to an object of the same class as an argument.
Syntax:
  1. Classname(const class_name & object_name
    {
              variable_name=object_name.constructor_variable;
    }
    Object creation:
    class_name object_name1;
    Method 1:
    class_name object_name2=object_name1;
    Method 2:

    class_name object_name2(object_name1);
Example:
  1. #include<iostream.h>
    #include<conio.h>
    class copy_num
    {
    int a, b;
    public:
    copy_num(int x, int y)
    {
    a = x;
    b = y;
    cout << "\n Assigned parameter values to the data member.";
    }
     copy_num(const copy_num& obj)
     {
    a = obj.a;
    b = obj.b;
    cout << "\n Copied the value in constructor.";
    }
    void show()
    {
    cout << "\n Values of a and b value is: \t" << a << "\t" << b;
    }
    };
    void main() {
    clrscr();
    copy_num obj(4,6);
    //Copy Constructor Invoked - Method 1
    copy_num obj2(obj);
    //Copy Constructor Invoked - Method 2
     copy_num obj3 = obj;
     obj.show();
    obj2.show();
    obj3.show();
    getch();
    }
Output:


Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me