C++ Program showing constructor overloading
Constructor is a member function with the same name as of it’s class.
This program is used to find the perimeter of square using constructor overloading.
#include<iostream.h>
#include<conio.h>
Class perimeter
{
Int l, b, p;
Public:
Perimeter ()
{
Cout<<”\n Enter the value of l and b”;
Cin>>l>>b;
}
Perimeter (int a)
{
L=b=a;
}
Perimeter (int l1, int b1)
{
l=l1;
b=b1;
}
Perimeter (perimeter and peri)
{
l= peri.l;
b= peri.b;
}
Void calculate ()
{
P = 2* (l +b)
Cout <<p;
}
};
Void main ()
{
Perimeter obj, obj1 (2), obj2 (2, 3);
Cout<<”\n perimeter of rectangle is”;
Obj. Calculate ();
Cout<<”\n perimeter of square”;
Obj1.calculate ();
Cout<<”\n perimeter of rectangle”;
Obj2.calculate ();
Cout<<”\n perimeter of rectangle”;
Perimeter obj3 (obj2);
obj3.calculate ();
getch ();
}
Related posts:
- Overloading Binary Operator Overloading binary operator An operator requiring two operand is called...
- C++ Program Using Constructor and Destructor This is an object oriented programming in c++ to create...
- Constructors Constructors Types of constructors Default constructor A constructor without any...
- Constructor in C++ Constructor in C++ Constructor is a member function with the...
- Default Argument Default argument C++ allows programmers to specify default values for...