OOP unit II inheritance.pptx object oriented programming
Srishti951154
21 views
33 slides
Sep 02, 2024
Slide 1 of 33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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...
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 properties and methods of an existing class (called the **parent class** or **superclass**).
### Key Points About Inheritance:
1. **Code Reusability**: Inheritance promotes code reuse by allowing the child class to use or modify methods and attributes of the parent class without having to rewrite them.
2. **Hierarchical Classification**: It helps in creating a hierarchical classification, making it easier to manage and organize code. For instance, if you have a general class like `Vehicle`, you can have more specific subclasses like `Car`, `Bike`, or `Truck`, which inherit common attributes (like `speed`, `color`) and methods (like `accelerate()`, `brake()`) from `Vehicle`.
3. **Types of Inheritance**:
- **Single Inheritance**: A class inherits from only one parent class.
- **Multiple Inheritance**: A class can inherit from multiple parent classes (not supported directly in some languages like Java).
- **Multilevel Inheritance**: A class inherits from a parent class, which in turn inherits from another parent class.
- **Hierarchical Inheritance**: Multiple classes inherit from a**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 properties and methods of an existing class (called the **parent class** or **superclass**).
### Key Points About Inheritance:
1. **Code Reusability**: Inheritance promotes code reuse by allowing the child class to use or modify methods and attributes of the parent class without having to rewrite them.
2. **Hierarchical Classification**: It helps in creating a hierarchical classification, making it easier to manage and organize code. For instance, if you have a general class like `Vehicle`, you can have more specific subclasses like `Car`, `Bike`, or `Truck`, which inherit common attributes (like `speed`, `color`) and methods (like `accelerate()`, `brake()`) from `Vehicle`.
3. **Types of Inheritance**:
- **Single Inheritance**: A class inherits from only one parent class.
- **Multiple Inheritance**: A class can inherit from multiple parent classes (not supported directly in some languages like Java).
- **Multilevel Inheritance**: A class inherits from a parent class, which in turn inherits from another parent class.
- **Hierarchical Inheritance**: Multiple classes inherit from a single parent class.
- **Hybrid Inheritance**: A combination of two or more types of inheritance.
4. **Overriding**: The child class can provide its own implementation of methods defined in the parent class. This is known as **method overriding**.
### Example in Python:
```python
class Animal:
def
Size: 1.06 MB
Language: en
Added: Sep 02, 2024
Slides: 33 pages
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; }