Saturday, 10 March 2018

Constructor In C++

Constructor name is same name as that of class name

It does not have any return type

It can be overloaded

It is called when object is created.

By default default constructor is called when object is created

It is used to initialize the instance variable of an object

Types:

There are three types of constructor in java
1) Default constructor (No arguments)
2) Parameterized constructor (Having argument)
3) Copy constructor

1) Default constructor (No arguments)
A constructor which has no argument is known as default constructor. It is invoked at the time of creating object. By default default constructor is called when object is created

Example:
#include <iostream.h> 
#include<conio.h>
class A 
 { 
   public: 
        A()   
        {   
            cout<<"Hi, Welcome to LearningPoint92"<<endl;   
        }   
}; 
void main()  
{ 
clrscr();
A a1;
 getch();
} 
Output:
Hi, Welcome to LearningPoint92

2)Parameterized Constructor
A constructor which has parameters is called  as Parameterized Constructor.
Example:
#include <iostream.h> 
#include<conio.h>
class Add
 { 
   public: 
        Add (int a,int b)   
        {   
            cout<<"sum is:"<<a+b<<endl;   
        }   
}; 
void main()  
{ 
clrscr();
Add  a1(55,45);
getch();
} 
Output:
Sum is :100

3)Copy constructor
Copy constructor
takes an object as argument, and is used to copy values of data members of one object into other object
Copy Constructor is a type of constructor which is used to create a copy of an already existing object of a class type.

Syntax:
class_name(const class_name & obj_name);
Example:
A (const A & s1)

Example:
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a, b; // data members
public:
A(int x, int y)
{
a=x;
b=y;
}
// Copy constructor
A (const A & s1)
{
a = s1.a;
b = s1.b;
}
void display()
{
cout<<a<<" "<<b<<endl;
}
};
void main()
{
clrscr();
A obj1(30, 45); // Default constructor
A obj2 = obj1; // Copy constructor
cout<<"Original constructor  Value: ";
obj1.display();
cout<<"Copied constructor value: ";
obj2.display();
getch();
}
Output:
Original constructor Value: 30 45
Copied constructor value: 30 45

Constructor (constructor is used to initialize instance variable of an object)
Example:
#include<iostream.h>
#include<conio.h>
class A{
   public:
   int x,y;

  A( int a,int b)
    {
        x=a;
        y=b;
  cout<<"sum is:"<<x+y<<endl;
    }

};

void main()
{
clrscr();
 A a1(55,45);
getch();
}
Output:
Sum is:100

Constructor (Using method)
#include<iostream.h>
#include<conio.h>
class A{
public:
int x,y;

A( int a,int b)
    {
        x=a;
        y=b;
           
    }
void show()
    {
             cout<<"sum is:"<<x+y<<endl;
    }
         
};
void main()
{
clrscr();
A a1(55,45);
a1.show();
getch();
}
Output:
Sum is:100

Constructor(Constructor can be overloaded)
#include<iostream.h>
#include<conio.h>
class A
{
public:
 int x,y;

   A(int a)      //constructor having one parameter
  {
     x=a;
  cout<<" Value is: <<x<<endl;
  }

    A( int a,int b)  //constructor having two parameter

    {
        x=a;
        y=b;
   cout<<"sum is:"<<x+y<<endl;
    }
};

void main()
{
clrscr();
A a1(56);
 A a2(55,45);
getch();
}
Output:
Value is:56
Sum is:100

Constructor (using this keyword)
#include<iostream.h>
#include<conio.h>
class A
{
public:
 int x,y;

A( int x,int y)
    {
       this->x=x;
       this-> y=y;
   }
  
   void show()
   {
cout<<"sum is:"<<x+y<<endl;
   }
   };
    
void main()
   {
          clrscr();
          A a2 (55,45);
          a2.show();
         getch();
    }

Output:
sum is:100


Constructor (Taking input from user)
#include<iostream.h>
#include<conio.h>
class A{
   public:
   int x,y;

  A( int a,int b)
    {
        x=a;
        y=b;
  cout<<"sum is:"<<x+y<<endl;
    }

};
void main()
{
int a,b;
clrscr();
cout<<"Enter two number"<<endl;
cin>>a>>b;
A a1(a,b);
getch();
}
Output:
Enter two number
55
45
sum is:100


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...