C++ program to demonstrate Constructor Overloading

//class Shape with dim1 and dim2 as private members
//create two constructors of the class
//one with one argument and the other with two arguments
//object rectangle with two dimensions
//object square with only one dimension
//calculate their area

#include <iostream>
using namespace std;
class Shape
{
    int dim1,dim2;
    public:
    Shape(int d1,int d2)
    {
        dim1=d1;
        dim2=d2;
    }
    Shape(int d)
    {
        dim1=d;
        dim2=d;
    }
    int getArea()
    {
        return (dim1*dim2);
    }
};
int main()
{
    Shape rectangle(5,6);
    cout<<"The area of rectangle is "<<rectangle.getArea()<<endl;
    Shape square(10);
    cout<<"The area of square is "<<square.getArea();
    return 0;
}

No comments:

Post a Comment