// basedrvd.cpp #include class Foo{ // base class public: const double PIE = 5.82; protected: int x; int y; public: Foo() { x = 0; y = 0; } Foo(int xx, int yy) { x = xx; y = yy; } Foo(const Foo &); ~Foo() { } int getX() const; // get x value int getY() const; // get y value void setX(int); void setY(int); double area(); void showFoo(); }; Foo::Foo(const Foo &p) { x = p.x; y = p.y; } int Foo::getX() const { return x; } int Foo::getY() const { return y; } void Foo::setX(int xx) { x = xx; } void Foo::setY(int yy) { y = yy; } double Foo::area() { return x*y*PIE; } void Foo::showFoo() { cout << "[" << x << ',' << y << "]"; } class Phooey : public Foo // derived class { private: double rad; public: Phooey(); Phooey(const int xx, const int yy); Phooey(int,int,double); Phooey(const Phooey &); ~Phooey() { } double getRad() const; void setRad(double); void tryit(); double area(); void showPhooey(); }; // default constructor Phooey::Phooey() : Foo() { rad = 0.0; } // might need all values x, y, and rad Phooey::Phooey(int xx,int yy,double rr) : Foo(xx,yy) { rad = rr; } // want just values x and y from Foo Phooey::Phooey(const int xx, const int yy) : Foo(xx,yy) { rad = 0.0; } // copy constructor Phooey::Phooey(const Phooey &c) : Foo(c) { rad = c.rad; } double Phooey::getRad() const { return rad; } void Phooey::setRad(double rr) { rad = rr; } void Phooey::tryit() { cout << "I can use " << x << " and " << y << endl; cout << "because they are protected in Foo" << endl; } double Phooey::area() { return PIE*rad*rad; } void Phooey::showPhooey() { cout << " foo = "; showFoo(); cout << " rad = " << rad << endl << endl; } int main(){ Foo one; Foo two(3,2); cout << endl << "one = "; one.showFoo(); cout << endl << "two = "; two.showFoo(); one.setX(-4); one.setY(-1); cout << endl << "one = "; one.showFoo(); cout << endl; Phooey a; cout << "a:"; a.showPhooey(); a.setRad(1.0); cout << "a:"; a.showPhooey(); Phooey b(8,3); cout << "b:"; b.showPhooey(); b.setRad(3.3); cout << "b:"; b.showPhooey(); Phooey c(5,5,5.5); cout << "c:"; c.showPhooey(); a.setX(9); // Phooey can call member function of Foo a.setY(7); // Phooey can call member function of Foo cout << "a:"; a.showPhooey(); cout << "area of b = " << b.area() << endl; cout << "area of two = " << two.area() << endl; b.tryit(); Phooey d(b); cout << "d:"; d.showPhooey(); return 0; }