Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

I NHERITANCE Chapter 7 Department of CSE, BUET 1.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to.
Inheritance, Polymorphism, and Virtual Functions
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
VIRTUAL FUNCTIONS AND DYNAMIC POLYMORPHISM. *Polymorphism refers to the property by which objects belonging to different classes are able to respond to.
Chapter 12: Adding Functionality to Your Classes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
OOP, Virtual Functions and Inheritance
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
Chapter 10 Inheritance and Polymorphism
Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
 A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is same as the class.
Constructor in Inheritance. 2 Constructors are used to initialized object. In inheritance the base class contains default constructor then, the base class.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Chapter -6 Polymorphism
A class may de defined to automatically include the data members and member functions of an already existing class. New data members and member functions.
Overview of C++ Polymorphism
1 Chapter 4: Another way to define a class Inheritance..!!
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
7. Inheritance and Polymorphism
Inheritance and Polymorphism
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance C++ strongly support the reusability, that is one class has been written and tested already it can be adapted or used to create a new class.
Polymorphism Lec
Learning Objectives Inheritance Virtual Function.
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Polymorphism Polymorphism
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
By Muhammad Waris Zargar
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Inheritance:Concept of Re-usability
Overview of C++ Polymorphism
Computer Science II for Majors
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Inheritance: Base and Derived Classes

Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive from a predecessor. In programming language term, inheritance refers to objects inheriting properties(data members & member functions) of another class. Advantage: code reusability. Definition: Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say "A inherits from B". Objects of class A thus have access to attributes and methods of class B without having to redefine them. Definition: If class A inherits from class B then B is called the superclass (or parent class) of A. A is called the subclass (or child class) of B.

Syntax for deriving class Class Classname : access specifier base1 class name, base2 class name { private : private data members; protected: protected data members; public: public data members; }; Access specifier can be - private,protected,public

Example for protected class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a>>b>>c;} void show() { cout<<endl<<"Base Class show()"; cout<<endl<<a<<" "<<b<<" "<<c; } }; int main() { base b_obj; b_obj.a = 10; // error not accessible b_obj.b = 20; error not accessible b_obj.c = 30; // will work since it is public data b_obj.getdata(); //will work -- public data b_obj.show(); // will work -- public data return 0; }

Example with two classes class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a>>b>>c;} void show() { cout<<endl<<"Base Class show()"; cout<<endl<<a<<" "<<b<<" "<<c; } }; int main() { clrscr(); base b_obj; b_obj.getdata(); b_obj.show(); derived d_obj; d_obj.getdata(); d_obj.show(); return 0; } class derived { private: int x; protected: int y; public: int z; void getdata() { cin>>x>>y>>z; } void show() { cout<<endl<<"derived class members"; cout<<endl<<x<<" "<<y<<" "<<z; } };

Types Of Inheritance Single Inheritance Multiple Inheritance Multi-level Inheritance Hierarchical Inheritance Hybrid Inheritance

Example Single Inheritance(public) class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a;} void show() { cout<<endl<<a; } }; int main() { derived d_obj; d_obj.getdata(); d_obj.show(); cout<<d_obj.c; cout<<d_obj.z; return 0; } class derived : public base { private: int x; protected: int y; public: int z; void getdata() { cout<<"\n Enter data for base class"; base::getdata(); cin>>b>>c; cout<<"\n Enter data for derived class"; cin>>x>>y>>z; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; cout<<endl<<x<<" "<<y<<" "<<z; } };

8 The type of inheritance defines the access level for the members of derived class that are inherited from the base class Access Rights of Derived Classes privateprotectedpublic private--- protectedprivateprotected publicprivateprotectedpublic Type of Inheritance Access Controlfor Members

Example Single Inheritance(protected) class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a;} void show() { cout<<endl<<a; } }; int main() { derived d_obj; d_obj.getdata(); d_obj.show(); cout<<d_obj.c; //error cout <<d,obj.z; return 0; } class derived : protected base { private: int x; protected: int y; public: int z; void getdata() { cout<<"\n Enter data for base class"; base::getdata(); cin>>b>>c; cout<<"\n Enter data for derived class"; cin>>x>>y>>z; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; cout<<endl<<x<<" "<<y<<" "<<z; } };

Example Single Inheritance(private) class base { class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a;} void show() { cout<<endl<<a; } }; int main() { derived d_obj; d_obj.getdata(); d_obj.show(); cout<<d_obj.c; //error cout <<d,obj.z; return 0; } class derived : private base { private: int x; protected: int y; public: int z; void getdata() { cout<<"\n Enter data for base class"; base::getdata(); cin>>b>>c; cout<<"\n Enter data for derived class"; cin>>x>>y>>z; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; cout<<endl<<x<<" "<<y<<" "<<z; } };

Access Control If a member is declared in a class C and is private, it can only be used by the member functions in C and by the friends of class C. Class C private: int a; public: void Set_a() Class E: friend Class C private: int num; public: void Set_num() void Set_a() and Class E can access the private data member, a which belongs to Class C.

Access Control If a member is declared in a class C and the member is protected, it can only be used by the member functions in C, friends of C and member functions and friends of classes derived from C. Class C protected: int a; public: void Set_a() Class E: friend Class C private: int num; public: void Set_num() Class F: friend Class D private: int numF; public: void Set_numF() Class D private: int numD; public: void Set_numD() void Set_a(),Class E, Class D, and Class F can access the private data member, a which belongs to Class C.

Access Control If a member is public it can be used everywhere without restrictions. Class C public: int a; public: void Set_a() int a and void Set_a can be accessed everywhere. t a and void Set_a can be accessed everywhere.

Access Control A derived class cannot access directly the private members of its base class. However, the derived class can access the public and protected member of its base class. The derived class can only access private members of the base class only through access functions provided in the base class’s public and protected interfaces.

Types Of Inheritance Class A Class B Class A { }; Class B : public A { }; Single Inheritance

Multi-level Inheritance Class A Class B Class C Public/private

Example for Access control in multi-level inheritance 1.A B C public public X Y Z A B C M N P X Y Z A B, Y C,Z M N, B,Y P, C, Z

Example for Access control in multi-level inheritance 1.A B C public private X Y Z A B C M N P X Y Z A B, Y C,Z M,B,Y,C,Z N P

Example for Access control in multi-level inheritance 1.A B C private public X Y Z A B C M N P X Y Z A,Y,Z B C M N,B P,C

Example for Access control in multi-level inheritance 1.A B C private private X Y Z A B C M N P X Y Z A,Y,Z B C M,B,C N P

Multiple Inheritance Class A Class B Class C Class A { }; Class B { }; Class C:public A, public B { };

Example for Access control in multiple inheritance A B public C Indicates public X Y Z A B C M N P X Y Z A B C M N, B,Y P, C, Z private Protected public

Example for Access control in multiple inheritance A B private C public X Y Z A B C M N P X Y Z A B C M,B,C N,Y P, Z

Hierarchical Inheritance Class A Class B Class CClass D Class GClass F Class E

Hybrid Inheritance Class A Class B Class CClass D Class G Class F Class E

Constructors and Destructors in derived classes When derived class object is constructed,then constructor of base class is called first and then constructor of derived class is called. Destructor works in reverse way. i.e. derived class destructor is called first and then destructor of base class. When class is derived from more than one base classes then calling constructor sequence depends on declaration of base classes. Constructors of base classes are called in left to right sequence whereas destructors are invoked in right to left sequence.

Multiple Inheritance Class A Class B Class C Class A { }; Class B { }; Class C:public A, public B { };

Class A{ public: A() {cout<<“A’s Constructor”;} ~A() { Cout<<“A’s Destructor”;} } }; Class B{ public: B() {cout<<“B’s Constructor”;} ~B() { Cout<<“B’s Destructor”;} } }; Class C:public B, private A { public: C() {cout<<“C’s Constructor”;} ~C() { Cout<<“C’s Destructor”;} } }; int main() { C obj; return 0; } Output B’s Constructor A’s Constructor C’s Constructor C’s Destructor A’s Destructor B’s Destructor

Hybrid Inheritance Class A Class B Class C Class D

Class A{ public: A() {cout<<“A’s Constructor”;} ~A() { Cout<<“A’s Destructor”;} } }; Class B{ public: B() {cout<<“B’s Constructor”;} ~B() { Cout<<“B’s Destructor”;} } }; Class C:public B, private A { public: C() {cout<<“C’s Constructor”;} ~C() { Cout<<“C’s Destructor”;} } }; Class D:public C{ public: D() {cout<<“D’s Constructor”;} ~D() { Cout<<“D’s Destructor”;} } }; Output B’s Constructor A’s Constructor C’s Constructor D’s Constructor D’s DEstructor C’s Destructor A’s Destructor B’s Destructor int main() { D obj; return 0; }

Explicit call to base class Constructor through derived class class base { private: int a; protected: int b; public: int c; base() { a=b=c=0;} base(int n1,int n2,int n3) { a=n1; b=n2; c=n3; } void show() { cout<<endl<<a; } }; class derived : private base { private: int x; protected: int y; public: int z; derived() {x=y=z=0;} derived(int n1,int n2,int n3) { x=n1; y=n2; z=n3; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; / cout<<endl<<x<<" "<<y<<" "<<z; } }; int main() { derived d_obj; d_obj.show(); return 0; }

Explicit call to base class Constructor through derived class class base { private: int a; protected: int b; public: int c; base() { a=b=c=0;} base(int n1,int n2,int n3) { a=n1; b=n2; c=n3; } void show() { cout<<endl<<a; } }; class derived : private base { private: int x; protected: int y; public: int z; derived() {x=y=z=0;} derived(int n1,int n2,int n3) { x=n1; y=n2; z=n3; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; / cout<<endl<<x<<" "<<y<<" "<<z; } }; int main() { derived d_obj(10,20,30); d_obj.show(); return 0; }

Explicit call to base class Constructor through derived class class base { private: int a; protected: int b; public: int c; base() { a=b=c=0;} base(int n1,int n2,int n3) { a=n1; b=n2; c=n3; } void show() { cout<<endl<<a; } }; class derived : private base { private: int x; protected: int y; public: int z; derived() {x=y=z=0;} derived(int n1,int n2,int n3):base(n1,n2,n3) { x=n1; y=n2; z=n3; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; / cout<<endl<<x<<" "<<y<<" "<<z; } }; int main() { derived d_obj(10,20,30); d_obj.show(); return 0; }

Explicit call to base class Constructor through derived class class base { private: int a; protected: int b; public: int c; base() { a=b=c=0;} base(int p1,int p2,int p3) { a=p1; b=p2; c=p3; } void show() { cout<<endl<<a; } }; class derived : private base { private: int x; protected: int y; public: int z; derived() {x=y=z=0;} derived(int n1,int n2,int n3):base(5,6,7) { x=n1; y=n2; z=n3; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; / cout<<endl<<x<<" "<<y<<" "<<z; } }; int main() { derived d_obj(10,20,30); d_obj.show(); return 0; }

Explicit call to base class Constructor through derived class class base { private: int a; protected: int b; public: int c; base() { a=b=c=0;} base(int p1,int p2,int p3) { a=p1; b=p2; c=p3; } void show() { cout<<endl<<a; } }; class derived : private base { private: int x; protected: int y; public: int z; derived() {x=y=z=0;} derived(int n1,int n2,int n3,int n4,int n5,int n6):base(n4,n5,n6) { x=n1; y=n2; z=n3; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; / cout<<endl<<x<<" "<<y<<" "<<z; } }; int main() { derived d_obj(10,20,30,40,50,60); d_obj.show(); return 0; }

Concept Of Overloading and Over-riding More than one Member functions with same name within a class is called as Overloading. e.g. Class A { private: int a; public: int add() { a= a+10;} int add(int x){ a = a + x;} float add(float x) { a = a+x;} }; int main() { A Obj; Obj.add(); Obj.add(45); Obj.add(24.8); return 0; }

Cont…. More than one member functions with same name across the class (inheritance) is called as Over-riding. Class A { private : int da; public: A() { da=0;} A(int x) { da=x;} void show() {cout<<da;} }; Class B :public A { private : int db; public: B() { db=0;} B(int x) { db=x;} void show() {cout<<db;} };

Pointer to Object Class Base { int b; public: Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} }; int main() { Base obj(30); //obj is object of class base obj.show(); //fn invoke thr’ obj with dot Base *pb; // pb is pointer to Base class pb=&obj; //pointing to object of base class pb->show(); // fn invoke thr’ pointer with arrow return 0; }

Derived class Pointer can point base class data member n function Class Base { public: int a; Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} }; Class Derived :public Base { public: int d; Derived() {d=0;} Derived(int x) { d=x;} void show() {cout<<b<<d;} }; int main() { Base obj(30),*pb; pb=&obj; pb->b = 50; pb->show(); //will call base class show() Derived dobj(20),*pd; pd = &dobj; pd->b = 50; pd->d=70; pd->show(); // will call derived class show() return 0; }

Derived class Pointer can not point to base class object Class Base { public: int a; Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} }; Class Derived :public Base { public: int d; Derived() {d=0;} Derived(int x) { d=x;} void show() {cout<<b<<d;} }; int main() { Base obj(30),*pb; pb=&obj; pb->b = 50; pb->show(); //will call base class show() Derived dobj(20),*pd; pd = &obj; // error return 0; }

Base class Pointer can point to derived class object Class Base { public: int a; Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} }; Class Derived :public Base { public: int d; Derived() {d=0;} Derived(int x) { d=x;} void show() {cout<<b<<d;} }; int main() { Base *pb; Derived obj(30); pb=&obj; pb->b = 50; pb->d = 40; //error pb->show(); //will call base class show() return 0; }

Base class Pointer can point to derived class data by type casting Class Base { public: int a; Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} }; Class Derived :public Base { public: int d; Derived() {d=0;} Derived(int x) { d=x;} void show() {cout<<b<<d;} }; int main() { Base *pb; Derived obj(30); pb=&obj; pb->b = 50; ((derived *)pb)->d = 40; ((derived *) pb)->show(); //will call derived class //show() return 0; }

IF Derived class does not have function definition Class base { public: void show() {cout<<“\n Base class show”;} }; Class derived:public base { int a; public: derived() { a = 10;} }; int main() { base b,*bptr; derived d; bptr = &b; bptr->show(); // base bptr = &d; bptr->show(); // base return 0; }

Virtual function Class base { public: void display() {cout<<“\n Base class display”;} virtual void show() {cout<<“\n Base class show”;} }; Class derived:public base { public: void display() {cout<<“\n derived class display”;} void show() {cout<<“\n derived class show”;} }; int main() { base b,*bptr; derived d; bptr = &b; bptr->display(); //base bptr->show(); // base bptr = &d; bptr->display(); //base // checks data type of caller bptr->show(); //derived //checks type of object at runtime return 0; }

Abstract Class An abstract class is one which is not use to create objects but its is designed to act as base class for other classes. e.g. Student Arts Sciencecommerce Shape Draw() Circle Draw() Rectangle Draw() Triangle Draw()

Pure Virtual function It is normal practice to declare a function as virtual in base class and redefine it in derived classes. The function inside the base class serves only as place holder and such functions are called do nothing functions Declaration virtual draw() = 0;

Cont….. A Pure virtual function is a function declared in base class that has no definition(code) relative to the base class. Class containing pure virtual function can not be used to declare any objects of its own. e.g. Abstract Class The main objective of an abstract class and pure virtual function is to achieve runtime polymorphism.

Virtual Class Hybrid Inheritance Class A Class B Class D Class C

Example for virtual class Class A { public: int da; void show(){cout<<da;} }; Class B:public A { public: int db; }; Class C:public A { public: int dc; }; Class D:public B,public C { public: int dd; void show() {cout<<dd,<<dc<<db<<da;}//ambiguity error }; int main() { D obj; return 0; } da db, A::da dc,A::da dd, A::da,A::da

Example for virtual class Class A { public: int da; void show(){cout<<da;} }; Class B:virtual public A { public: int db; }; Class C:virtual public A { public: int dc; }; Class D:public B,public C { public: int dd; void show() {cout<<dd,<<dc<<db<<da;} }; int main() { D obj; return 0; } da db, A::da dc,A::da dd, A::da