OOP unit II inheritance.pptx object oriented programming

Srishti951154 21 views 33 slides Sep 02, 2024
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

It is power point presentation of one of the programming language it includes concept which is very necessary in programming languages. **Inheritance** in Object-Oriented Programming (OOP) is a fundamental concept that allows a new class (known as a **child class** or **subclass**) to acquire the pr...


Slide Content

Unit II Inheritance and Pointers Inheritance

Reusability is yet another feature of OOP. C++ strongly supports the concept of reusability. The mechanism of deriving a new class from an old one is called 'INHERITANCE'. The old class is called 'BASE' class and the new class is called 'DERIEVED‘ class.

Defining Derived Classes A derived class is specified by defining its relationship with the base class in addition to its own details. Syntax: c lass derived-class-name: visibility-mode base-class-name { ….. …..//members of derived class }

class XYZ //base class { members of XYZ }; class ABC : public XYZ //public derivation { members of ABC }; class ABC : XYZ //private derivation (by default) { members of ABC };

In the inheritance, some of the base class data members and member functions are inherited into the derived class. We can add our own data members and member functions and thus extend the functionality of the base class.

When a class inherits from a single base class, it is known as single inheritance. Following program shows the single inheritance using public derivation.

#include< iostream > u sing namespace std ; c lass B{ int a; //private, not inheritable p ublic : //public, ready for inheritance int b; void get_ab(); int get_a(); void show_a(); }; Class D : public B{ int c; p ublic: void mul(); void display(); };

void B :: get_ab() { a=5; b=10; } int B :: get_a() { return a ; } void B :: show_a() { cout<< “a=“ << a << “\n” ; } void D :: mul() { c = b * get_a(); } void D :: display() { cout << “a=“ << get_a() << “\n”; cout << “b=“ << b << “\n ”; cout << “c=“ << c << “\ n\n”; } int main() { D d ; d.get_ab(); d.mul(); d.show_a(); d.display();

d.b=20; d.mul(); d.display(); return 0; }

program for single inheritance using private derivation.

#include< iostream > u sing namespace std ; c lass B{ int a; //private, not inheritable p ublic : //public, ready for inheritance int b; void get_ab(); int get_a(); void show_a(); }; Class D : private B{ int c; p ublic: void mul(); void display(); };

void B :: get_ab() { a=5; b=10; } int B :: get_a() { return a ; } void B :: show_a() { cout<< “a=“ << a << “\n” ; } void D :: mul() { get_ab(); c = b * get_a(); } void D :: display() { show_a(); cout << “b=“ << b << “\n ”; cout << “c=“ << c << “\ n\n”; } int main() { D d ; // d.get_ab (); won’t work d.mul (); // d.show_a (); won’t work d.display ();

// d.b =20; won’t work ,b has become private d.mul(); d.display(); return 0; }

MULTILEVEL INHERITANCE e.g. #include< iostream.h > class student { protected: int rn ; public: v oid getn ( int a) { rn =a ; } void putn ( int a) { cout <<“roll no is”<< rn ; } };

class test : public student //1st level derivation { protected : float s1,s2;   public: v oid getm (float x,float y) { s1=x; s2=y; }   void putm () { cout <<“marks in 2 subject are “<<s1<<s2 ; } };

c lass result : public test { //2nd level derivation float total; public: void display() { total=s1+s2; putn (); putm (); cout <<“total=“<<total; } i nt main (){ r esult stud1; s tud1.getn(101); stud1.getm(75.0,59.5); s tud1.display(); r eturn 0; }

Multiple inheritance class M{ protected: int m; public: void getm(int x){ m=x; } }; class N{ protected: int n; public: void getn(int y){ n=y; } }; class P:public M,public N{ public: void display() { cout<<m<<n<<m*n; } };

int main(){ P p; p.getm(); p.getn(); p.display(); return 0; }

Ambiguity resolution in inheritance class M{ public: void display() { cout<<”class M”; } }; class N{ public: void display() { cout<<”class N”; }}; class P:public M,public N{ public: void display() //overrides display of M and N { M::display(); } };

int main(){ P p; p.display(); return 0; }

virtual base class class A{ ..... }; //grand parent class B1:virtual public A //parent 1 { ...........}; class B2: public virtual A //parent 2 { ...........}; class C: public B1,public B2 //child { ........... //only 1 copy of A will be inherited };

Constructor Special member function to initialize objects of its class. It has same name as that of the class. It constructs the values of data members of the class.

Class with a default Constructor(no parameters) c lass integer { int m,n ; public: integer(); //constructor declared ……. }; Integer:: integer() //constructor defined { m=0; n=0; } In main() integer int1; //object int1 created

Parameterized Constructor(with parameters) c lass integer{ int m, n; public: integer( int x,int y); //parameterized constructor declared ……. }; Integer:: integer() //constructor defined { m=x; n=y; } In main() integer int1(10,20); //object int1 created by implicit call OR Integer int1=integer(10,20); //object int1 created by explicit call

e.g. c lass integer { int m, n; public: integer( int , int );//parameterized constructor declared void display() { cout <<m<<n; } }; integer:: integer( int x , int y) //constructor defined { m=x; n=y; }

int main(){ integer int1(10,20); //constructor called implicitly integer int2=integer(100,200); // constructor called explicitly c out <<“OBJECT1”<< endl ; int1.display(); cout <<“ OBJECT2”<< endl ; int2.display(); r eturn 0; }

constructors in derived class class A{ int x; public : A(int i) { x=i; cout<<”A initialized”; } void showx() { cout<<x; } }; class B{ float y; public: B(float j) { y=j; cout<<”B initialized”; } void showy() { cout<<y; } };

class C:public B,public A { int m,n; public : C(int a,float b,int c,int d):A(a),B(b){ m=c; n=d; cout<<”C initialized”; } void showmn(){ cout<<m<<n; } };

int main() { C obj(5,10.75,20,30); obj.showx(); obj.showy(); obj.showmn(); return 0; }
Tags