// This is "heir2.cpp", a demo of inheritance in C++. // It is the work of Harry Hutchins. #include #include using std::cout; using std::endl; using std::setprecision; using std::fixed; class Bag { protected: char Name; public: Bag(const char & = ' '); virtual void Print() const; }; Bag::Bag(const char & A) { Name = A; } void Bag::Print() const { cout << "My name is " << Name << endl; cout << "I am a Bag" << endl; } class Purse : public Bag { protected: float Money; public: Purse(const char &, const float &); void Print() const; }; Purse::Purse(const char & A, const float & M) : Bag(A) { Money = M; } void Purse::Print() const { Bag::Print(); cout << "I am also a Purse" << endl; cout << "I contain " << fixed << setprecision(2) << Money << " dollars" << endl; } int main() { Bag B('A'); Purse P('B', 12.50); cout << "Output from a Bag:" << endl << endl; B.Print(); cout << endl; cout << "Output from a Purse:" << endl << endl; P.Print(); cout << endl; Bag * Array[2]; Array[0] = &B; Array[1] = &P; cout << "Output from each of a pair of Bag pointers:" << endl << endl; for (int I = 0; I < 2; I++) { cout << "From Array[" << I << "]:" << endl; Array[I]->Print(); cout << endl; } return 0; }