Download presentation
Presentation is loading. Please wait.
1
Ambiguity Resolution in Inheritance
2
We May face a problem using the multiple Inheritance , When a function name appear in more then one base class Class M { public : void display() { cout<< “Class M” } }; Class N { Public: Void Display() { cout <<“ Class N “} }; Which Display() function is used by the Derived class when we inherit these two class .
3
The problem can be solved by defining a named Instance within the derived class resolution operator with the function. class P : public M , Public N { void Display() M :: display(); } }; Void main() { P p; P.display(); }
4
The Ambiguity may also Arise in a single Inheritance . Example
Class A { Public : void Display() { cout<<“A”; }}; Class B : public A { cout<<“B”; }}; The Function in the Derived class overrides the inherited function So a simple call to display() function by B type object will invoke function defined in B Only But We may Invoke the function Display () defined in Class A by using the Scope Resolution operator to Specify the class
5
Int main() { B b ; b. display(); // invoke display() in B b
Int main() { B b ; b.display(); // invoke display() in B b.A::display() // invoke display() in A b.B::display() // Invoke display() In B }
6
Nesting of classes Inheritance is the mechanism of deriving certain properties of one class into another . When a class contains objects of another class or its members, this kind of relationship is called containership or nesting . The class which contains objects of another class as its members is called as container class.
7
Class alpha {…… }; Class beta {……}; Class gamma { alpha a; // a is an object of alpha class beta b; // b is an object of beta class }; All the objects of gamma class will contain the object and b . This kind of relationship is called containership or nesting Creation of an object that contains another object is different then the creation of an independent object.
8
#include<iostream.h>
class A { int a; public: void get(); }; class B { int b; A t; // object t of class A is declare in class B void getdata(); }; void A :: get() { cin>>a; cout<<a; } void B :: getdata() { cin>>b; cout<<b; t.get(); } //calling of get() of class A in getdata() of class B
9
void main() { Clrscr(); B ab; ab.getdata(); getch(); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.