// CIS 235 - -- Review for Final finher.cpp // //Inheritance and Virtual Functions #include using std::cout; using std::endl; class one { public: void f1(void)const { cout <<"Hello from one\n";} void f2(void)const { cout <<"Hello from two\n";} virtual void f3(void)const { cout <<"Hello from three\n";} virtual ~one() {} }; class two :public one { public: void f2(void)const { cout <<"Hello from two,class two\n";} virtual void f3(void)const { cout <<"Hello from three,class two\n";} void f4(void) const { cout << "Hello from four\n"; } ~two() {} }; void main() { one a; two b; one & c = b; one * p1; two * p2; cout << "Test base class\n"; a.f1(); a.f2(); a.f3(); cout << "Test alias\n"; c.f1(); c.f2(); c.f3(); cout << "Test of base class pointers pointers\n"; p1 = & b; p1->f1(); p1->f2(); p1->f3(); cout << "Test of derived class pointers\n"; p2 = & b; p2->f1(); p2->f2(); p2->f3(); p2->f4(); // What is wrong with the following; cout << "Test of casting base class pointer to derived class pointer\n"; ( (two *) p1)->f4(); // old style of casting static_cast(p1)->f4(); // new style of casting // The following generate syntax errors - WHY? // p1->f4(); // c.f4(); // ((two) a).f1(); // static_cast(a).f1(); // Use scoping to call base class functions using invoking instance of b // .f1(); // .f2(); // .f3(); cout << "Test of casting derived class pointer\n"; static_cast (p2) ->f1(); static_cast (p2) ->f2(); static_cast (p2) ->f3(); cout << "Test of casting derived class\n"; static_cast(b).f1(); static_cast(b).f2(); static_cast(b).f3(); } /* Test base class Hello from one Hello from two Hello from three Test alias Hello from one Hello from two Hello from three,class two Test of base class pointers pointers Hello from one Hello from two Hello from three,class two Test of derived class pointers Hello from one Hello from two,class two Hello from three,class two Hello from four Test of casting base class pointer to derived class pointer Hello from four Test of casting derived class pointer Hello from one Hello from two Hello from three,class two Test of casting derived class Hello from one Hello from two Hello from three */