Wednesday, April 3, 2013

overloading constructors

#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public:
CRectangle();
CRectangle(int _x, int _y);
void set_values (int,int);
int area () {return (x*y);}
};
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
CRectangle::CRectangle():x(0),y(0)
{}
CRectangle::CRectangle(int _x, int _y):x(_x),y(_y)
{}
int main () {
CRectangle rect, rectb, rectc(3,4);
rectb.set_values (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
cout << "rectc area: " << rectc.area() << endl;
return 0;
}
view raw gistfile1.cpp hosted with ❤ by GitHub
Output:
1
2
3
rect area: 0
rectb area: 30
rectc area: 12

No comments:

Post a Comment