Inheritance Programs

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Inheritance Programs as PDF for free.

More details

  • Words: 1,760
  • Pages: 16
INHERITANCE PROGRAMS 1. Single Inheritance Public Derivation class base { int a; public: int b; void getdata(void); void putdata(void); int get_value(void); void put_value(void); }; class derived:public base //Public derivation { int c; public: void sum(void); void display(void); }; void base::getdata(void) { cout<<"Enter a and b"; cin>>a>>b; } void base::putdata(void) { cout<<"a="<>a>>b; } void base::putdata(void) { cout<<"a="<>a>>b; } int base::get_value(void) { return (a); } void base::put_value(void) { cout<<"Base Class Private Member a="<
3

}

/* When a base class and a derived class define a function of same name the derived class function supersedes the base class definition. the base class function will be called only if the derived class does not redefine the function*/ 4. Single Inheritance Same member function in base class derived class class base { int a; public: int b; void getdata(void); void putdata(void); int get_value(void); void put_value(void); }; class derived:public base //Public derivation { int c; public: void sum(void); void putdata(void);// same function name }; void base::getdata(void) { cout<<"Enter a and b"; cin rel="nofollow">>a>>b; } void base::putdata(void) { cout<<"base a="<>a>>b>>c; } void base::putdata(void) { cout<<"a="<>a>>b>>c; } void base::putdata(void) { cout<<"a="<>a>>b>>c; } void base::putdata(void) { cout<<"a="<>a>>b>>c; } void base::putdata(void) { cout<<"a="<
8

int base::get_sum(void) { return(a); } void base::put_value(void) { cout<<"Base Class Private Member a="<>a; } void one::display_a(void){ cout<<"a="<
9

cin rel="nofollow">>b>>c;

}

void display_values(void) }; class three:public two { float d; public: void display(void) { d=c+b; display_a(); display_values(); cout<<"d="<
{ cout<<"b="<>a; } void one::display_a(void){ cout<<"a="<>b>>c; }

10

}

void display_values(void) { display_a(); cout<<"b="<>a; } void one::display_a(void){ cout<<"a="<
11

class two : private one { protected: int b; float c; public: void get_values(void) { get_a(); cout<<"Enter b(int) and c(float) values"; cin rel="nofollow">>b>>c; } void display_values(void) { display_a(); cout<<"b="<>a; } void one::display_a(void){ cout<<"a="<>b>>c; } void display_values(void) { display_a(); cout<<"b="<>a; } class two { protected: int b; public: void get_b(void) { cout<<"Enter b"; cin>>b; }; class three:public one,public two { public: void display(void) { cout<<"a="<>a; } class two { protected: int b; public: void get_b(void) { cout<<"Enter b"; cin>>b; } }; class three:public one,private two { public: void display(void) { get_b(); cout<<"a="<
15

int main() { C obj1; obj1.disp();return(0);} /* Ambiguity occurs (error) display(); - rel="nofollow"> A::display(); or B::display(); */ 16.Ambiguity resolution in Single Inheritance class A { public: void display(void) { cout<<"Class A"<<endl; } }; class B: public A { public: void display(void) { cout<<"Class B"<<endl; } }; int main() { B obj1; obj1.display(); obj1.A::display(); obj1.B::display(); } /* Ambiguity occurs (error) display (); -> obj1.A:: display(); or obj1.B::display(); */

16

Related Documents

Inheritance Programs
November 2019 11
Inheritance
November 2019 34
Inheritance
June 2020 21
Inheritance
October 2019 32
Inheritance Patterns
November 2019 48
Programs
October 2019 61