22-InheritOverloadOverride

22-InheritOverloadOverride - LevelOne one; LevelTwo two;...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> using namespace std; // Case 0 class Level1 { public: void Something() {} }; class Level2 : public Level1 { public: void Anotherthing(int x ) {} }; // Case I class LevelOne { int x; public: void Print() { cout << "Level One Print " << x << endl; } }; class LevelTwo : public LevelOne { int x; public: void Print() { cout << "Level Two Print " << x << endl; } }; // Case II class LevelOneOverride { int x; public: void Print() { cout << "Level One Print " << x << endl; } void Print(int x) { cout << "Level One Print " << x << endl; } }; class LevelTwoOverride : public LevelOneOverride { int x; public: void Print() { cout << "Level One Print " << x << endl; } }; // Case III class LevelOneOverload { int x;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
public: void Print() { cout << "Level One Print " << x << endl; } }; class LevelTwoOverload : /*public*/ LevelOneOverload { int x; public: void Print(int x) { cout << "Level One Print " << x << endl; } }; int main (void) { Level1 one1; Level2 two2; one1.Something(); two2.Anotherthing(4); two2.Something(); // Example 1 // Inheritence only overrides Print
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: LevelOne one; LevelTwo two; one.Print(); two.Print(); // Example 2 // Inheritence overloads and overrides LevelOneOverride oOne; LevelTwoOverride oTwo; oOne.Print(); oTwo.Print(); oOne.Print(1); //oTwo.Print(2); oTwo.LevelOneOverride::Print(2); // Force recognition // Example 3 // Inheritence overloads only LevelOneOverload overloadOne; LevelTwoOverload overloadTwo; overloadOne.Print(); //overloadTwo.Print(); // Cannot force because of private inheritence /*overloadTwo.LevelOneOverload::Print(); //'LevelOneOverload::Print' not accessible because 'LevelTwoOverload' uses 'private' to inherit from 'LevelOneOverload'*/ overloadTwo.Print(35); return 0; } /* Level One Print -858993460 Level Two Print -858993460 Level One Print -858993460 Level One Print -858993460 Level One Print 1 Level One Print 2 Level One Print -858993460 Level One Print 35 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.

Page1 / 3

22-InheritOverloadOverride - LevelOne one; LevelTwo two;...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online