SlideShare a Scribd company logo
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:
class 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>
using namespace std;
class B{
int a; //private, not inheritable
public : //public, ready for inheritance
int b;
void get_ab();
int get_a();
void show_a();
};
Class D : public B{
int c;
public:
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 << “nn”;
}
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>
using namespace std;
class B{
int a; //private, not inheritable
public : //public, ready for inheritance
int b;
void get_ab();
int get_a();
void show_a();
};
Class D : private B{
int c;
public:
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 << “nn”; }
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:
void 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:
void getm(float x,float y)
{ s1=x; s2=y; }
void putm()
{ cout<<“marks in 2 subject are “<<s1<<s2; }
};
class result : public test { //2nd level derivation
float total;
public:
void display() {
total=s1+s2;
putn();
putm();
cout<<“total=“<<total; }
int main(){
result stud1;
stud1.getn(101);
stud1.getm(75.0,59.5);
stud1.display();
return 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
};
OOP unit II inheritance.pptx object oriented programming
OOP unit II inheritance.pptx object oriented programming
OOP unit II inheritance.pptx object oriented programming
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)
class 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)
class 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.
class 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
cout<<“OBJECT1”<<endl;
int1.display();
cout<<“OBJECT2”<<endl;
int2.display();
return 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;
}
OOP unit II inheritance.pptx object oriented programming

More Related Content

PPTX
OOPS IN C++
PDF
chapter-10-inheritance.pdf
PPTX
Inheritance and Interfaces
PPTX
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
PPT
Inheritance
PDF
Chapter 6 and inheritance OOP C++ tu ioe
PPTX
[OOP - Lec 20,21] Inheritance
PDF
lecture-2021inheritance-160705095417.pdf
OOPS IN C++
chapter-10-inheritance.pdf
Inheritance and Interfaces
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
Inheritance
Chapter 6 and inheritance OOP C++ tu ioe
[OOP - Lec 20,21] Inheritance
lecture-2021inheritance-160705095417.pdf

Similar to OOP unit II inheritance.pptx object oriented programming (20)

PPTX
OOPS Basics With Example
PPT
Inheritance
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
PPTX
6. Virtual base class.pptx and virtual function
PPTX
Inheritance
PDF
Inheritance
PPT
PPTX
05 Object Oriented Concept Presentation.pptx
PPT
C++ polymorphism
PDF
Inheritance and polymorphism
PPTX
Inheritance.pptx
PPTX
Multiple Inheritance
PDF
Inheritance
PPT
Constructors.16
PDF
C++ prgms 4th unit Inheritance
PPTX
Inheritance
PPTX
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
PDF
C++ prgms 5th unit (inheritance ii)
PDF
Chapter26 inheritance-ii
PPTX
OOPS Basics With Example
Inheritance
MODULE2_INHERITANCE_SESSION1.ppt computer
6. Virtual base class.pptx and virtual function
Inheritance
Inheritance
05 Object Oriented Concept Presentation.pptx
C++ polymorphism
Inheritance and polymorphism
Inheritance.pptx
Multiple Inheritance
Inheritance
Constructors.16
C++ prgms 4th unit Inheritance
Inheritance
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
C++ prgms 5th unit (inheritance ii)
Chapter26 inheritance-ii
Ad

Recently uploaded (20)

PPTX
additive manufacturing of ss316l using mig welding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
DOCX
573137875-Attendance-Management-System-original
PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Well-logging-methods_new................
additive manufacturing of ss316l using mig welding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
CH1 Production IntroductoryConcepts.pptx
OOP with Java - Java Introduction (Basics)
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
573137875-Attendance-Management-System-original
Sustainable Sites - Green Building Construction
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Geodesy 1.pptx...............................................
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Foundation to blockchain - A guide to Blockchain Tech
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Well-logging-methods_new................
Ad

OOP unit II inheritance.pptx object oriented programming

  • 1. Unit II Inheritance and Pointers Inheritance
  • 2. • 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.
  • 3. Defining Derived Classes • A derived class is specified by defining its relationship with the base class in addition to its own details. • Syntax: class derived-class-name: visibility-mode base-class-name { ….. …..//members of derived class }
  • 4. 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 };
  • 5. • 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.
  • 6. • When a class inherits from a single base class, it is known as single inheritance. • Following program shows the single inheritance using public derivation.
  • 7. #include<iostream> using namespace std; class B{ int a; //private, not inheritable public : //public, ready for inheritance int b; void get_ab(); int get_a(); void show_a(); }; Class D : public B{ int c; public: void mul(); void display(); };
  • 8. 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 << “nn”; } int main() { D d; d.get_ab(); d.mul(); d.show_a(); d.display();
  • 10. program for single inheritance using private derivation.
  • 11. #include<iostream> using namespace std; class B{ int a; //private, not inheritable public : //public, ready for inheritance int b; void get_ab(); int get_a(); void show_a(); }; Class D : private B{ int c; public: void mul(); void display(); };
  • 12. 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 << “nn”; } int main() { D d; //d.get_ab(); won’t work d.mul(); //d.show_a(); won’t work d.display();
  • 13. //d.b=20; won’t work ,b has become private d.mul(); d.display(); return 0; }
  • 14. MULTILEVEL INHERITANCE e.g. #include<iostream.h> class student { protected: int rn; public: void getn(int a) { rn=a ; } void putn(int a) { cout<<“roll no is”<<rn; } };
  • 15. class test : public student //1st level derivation { protected: float s1,s2; public: void getm(float x,float y) { s1=x; s2=y; } void putm() { cout<<“marks in 2 subject are “<<s1<<s2; } };
  • 16. class result : public test { //2nd level derivation float total; public: void display() { total=s1+s2; putn(); putm(); cout<<“total=“<<total; } int main(){ result stud1; stud1.getn(101); stud1.getm(75.0,59.5); stud1.display(); return 0;}
  • 17. 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; } };
  • 19. 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(); } };
  • 21. 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 };
  • 25. 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.
  • 26. Class with a default Constructor(no parameters) class integer { int m,n; public: integer(); //constructor declared ……. }; Integer:: integer() //constructor defined { m=0; n=0; } In main() integer int1; //object int1 created
  • 27. Parameterized Constructor(with parameters) class 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
  • 28. e.g. class 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; }
  • 29. int main(){ integer int1(10,20); //constructor called implicitly integer int2=integer(100,200); // constructor called explicitly cout<<“OBJECT1”<<endl; int1.display(); cout<<“OBJECT2”<<endl; int2.display(); return 0; }
  • 30. 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; } };
  • 31. 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; } };