Commit 492279e8 authored by BRENO RILHO LEMOS's avatar BRENO RILHO LEMOS

Inheritance

parent 002018aa
#include <iostream>
using namespace std;
class area_cl { // This is called the BASE CLASS.
public:
double height;
double width;
};
class rectangle : public area_cl { // This class inherits every public information from class area_cl. It is called the DERIVED CLASS.
public:
rectangle(double h, double w); // This function is called CONSTRUCTOR. It is called when object is created.
double area();
};
rectangle::rectangle(double h, double w) { // This is how a constructor is defined. A constructor has no type!
height = h;
width = w;
}
double rectangle::area() { // Here's an example of a function doing business on variables from its base class.
return height * width;
}
class isosceles : public area_cl { // Another DERIVED CLASS.
public:
isosceles(double h, double w);
double area();
};
isosceles::isosceles(double h, double w) {
height = h;
width = w;
}
double isosceles::area() {
return 0.5 * height * width;
}
int main() {
double rec_w, rec_h, iso_w, iso_h;
cout << "Rectangle's width: ";
cin >> rec_w;
cout << "Rectangle's height: ";
cin >> rec_h;
cout << "Isosceles triangle's width: ";
cin >> iso_w;
cout << "Isosceles triangle's height: ";
cin >> iso_h;
rectangle r(rec_h,rec_w);
isosceles t(iso_h,iso_w);
cout << "\nThe rectangle's area is " << r.area() << " and the triangle's area is " << t.area();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment