24a-BadCasting - cout << "ThirdClass"...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
class BaseClass { private: int data; int stuff; public: BaseClass(int d = 0) { stuff = data = d; } virtual void Method() {} }; #include <iostream> using namespace std; class DerivedClass : public BaseClass { private: int more; public: DerivedClass() { more = 45; } virtual void Method() { cout << "DerivedClass" << more << endl; } void DoHave() { cout << "DerivedClass" << more << endl;} }; class ThirdClass : public BaseClass { private: float other; public: ThirdClass() { other = 99.25; } virtual void Method() {
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cout << "ThirdClass" << other << endl; } void DontHave() {cout << "ThirdClass" << other << endl;} }; int main (void) { BaseClass* baseRef; DerivedClass dc; baseRef = &dc; ThirdClass tc; baseRef->Method(); baseRef= &tc; baseRef->Method(); static_cast <DerivedClass*> (baseRef)->DoHave(); //static_cast <DerivedClass*> (baseRef)->DontHave(); return 0; } /* DerivedClass45 ThirdClass99.25 DerivedClass1120305152 Press any key to continue . . . */...
View Full Document

This note was uploaded on 10/05/2011 for the course CS 37 taught by Professor Glenncuevas during the Summer '09 term at Irvine Valley College.

Ask a homework question - tutors are online