#include <iostream>
using namespace std;
//define class
class Rectangle {
private:
int width;
int height;
public:
Rectangle();
Rectangle(int, int);
public:
int getArea() const {
return width * height ;
}
int getWidth() const {
return width;
}
int getHeight() const {
return height;
}
};
//default constructor
Rectangle::Rectangle() {
width = 10;
height = 5;
}
//constructor 1
Rectangle::Rectangle(int a, int b) {
width = a;
height = b;
}
int main() {
// Create an object Rectangle 'rect1' from constructor 1.
// Value 5 is assigned to the width, the value 4 is assigned to the height.
Rectangle rect1(5, 4);
// Create an object Rectangle 'rect2' from default constructor .
// width, height are assigned default values
Rectangle rect2;
cout << "rect1:" << endl;
// Call method to get with
cout << " width: " << rect1.getWidth() << endl;
// Call method to get heght
cout << " height: " << rect1.getHeight() << endl;
// Call the method to calculate the area.
cout << " area: " << rect1.getArea() << endl;
cout << "rect2:" << endl;
cout << " width: " << rect2.getWidth() << endl;
cout << " height: " << rect2.getHeight() << endl;
cout << " area: " << rect2.getArea() << endl;
return 0;
}
Comments
Leave a comment