1234567891011121314151617181920212223242526272829303132333435 |
- // Example showing a base class (Shape) and a derived
- // class Rectangle. Notice how Rectangle inherits Shape's
- // display function.
-
- #include <iostream>
-
- using namespace std;
-
- class Shape {
- protected:
- int width, height;
- public:
- Shape() { width = height = 0; }
- Shape(int w, int h) { width = w; height = h; }
- void display() const;
- };
-
- void Shape::display() const {
- cout << width << " " << height << endl;
- }
-
-
- class Rectangle : public Shape {
- public:
- Rectangle() {}
- Rectangle(int w, int h) : Shape(w, h) {}
- };
-
- int main() {
- Rectangle r;
- Rectangle q(4,5);
- r.display();
- q.display();
- return 0;
- }
|