Inheritance zThe mechanism by which one class can inherit the properties of another. zIt allows a hierarchy of classes to be built, moving from the most.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
M The University Of Michigan Andrew M. Morgan EECS Lecture 24 Savitch Ch. 14 Inheritance.
Operator overloading redefine the operations of operators
For(int i = 1; i
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Inheritance (2).
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Inheritance - Case Study TCP1201: 2013/2014. Case Study – Guessing Game Listed below is the code to play a guessing game using procedural programming.
1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
1 CSC241: Object Oriented Programming Lecture No 21.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
I NTRODUCING O PERATOR O VERLOADING Chapter 6 Department of CSE, BUET 1.
1 Chapter 11 Introducing the Class Pages ( )
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
C++ Classes & Data Abstraction
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Pointer. Kegunaan pointer yang utama adalah untuk menyimpan memory address dari sebuah variable (data type atau object dari class). Selain menyimpan address.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
Learners Support Publications Inheritance: Extending Classes.
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.
Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
Inheritance using Java
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.
1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
1 CSC241: Object Oriented Programming Lecture No 12.
Chapter 10 Inheritance and Polymorphism
Inheritance - Multilevel MEENA RAWAT PGT (COMP) KV ONGC Chandkheda 1.
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
Order of Constructor Call. Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Inheritance and Composition Reusing the code and functionality Unit - 04.
Topics Inheritance introduction
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
NESTED CLASS. Apa itu nested class ? Nested class is a class defined inside a class, that can be used within the scope of the class in which it is defined.
Inheritance Concept of Inheritance . Types of Inheritance .
Class A { public : Int x; A()
Inheritance II CMSC 202.
Ambiguity Resolution in Inheritance
HYBRID INHERITANCE : AMBIGUITY REMOVAL
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Sampath Kumar.S Assistant Professor/IT, SECE
By Muhammad Waris Zargar
Object-Oriented Programming (OOP) Lecture No. 25
class PrintOnetoTen { public static void main(String args[]) {
Inheritance: Polymorphism and Virtual Functions
Multiple Base Classes Inheritance
Inheritance:Concept of Re-usability
B.FathimaMary Dept. of Commerce CA SJC Trichy-02
Object-Oriented Programming (OOP) Lecture No. 27
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

Inheritance zThe mechanism by which one class can inherit the properties of another. zIt allows a hierarchy of classes to be built, moving from the most general to the most specific.

Base Class, Derived Class zBase Class yDefines all qualities common to any derived classes. zDerived Class yInherits those general properties and adds new properties that are specific to that class.

Example: Base Class class base { int x; public: void setx(int n) { x = n; } void showx() { cout << x << ‘\n’ } };

Example: Derived Class // Inherit as public class derived : public base { int y; public: void sety(int n) { y = n; } void showy() { cout << y << ‘\n’;} };

Access Specifier: public zThe keyword public tells the compiler that base will be inherited such that: yall public members of the base class will also be public members of derived. zHowever, all private elements of base will remain private to it and are not directly accessible by derived.

Example: main() int main() { derived ob; ob.setx(10); ob.sety(20); ob.showx(); ob.showy(); }

An incorrect example class derived : public base { int y; public: void sety(int n) { y = n; } /* Error ! Cannot access x, which is private member of base. */ void show_sum() {cout << x+y; } };

Access Specifier: private zIf the access specifier is private: ypublic members of base become private members of derived. ythese members are still accessible by member functions of derived.

Example: Derived Class // Inherit as private class derived : private base { int y; public: void sety(int n) { y = n; } void showy() { cout << y << ‘\n’;} };

Example: main() int main() { derived ob; ob.setx(10);// Error! setx() is private. ob.sety(20);// OK! ob.showx();// Error! showx() is private. ob.showy();// OK! }

Example: Derived Class class derived : private base { int y; public: // setx is accessible from within derived void setxy(int n, int m) { setx(n); y = m; } // showx is also accessible void showxy() { showx(); cout<<y<< ‘\n’;} };

Protected Members zSometimes you want to do the following: ykeep a member of a base class private yallow a derived class access to it zUse protected members! zIf no derived class, protected members is the same as private members.

Protected Members The full general form of a class declaration: class class-name { // private members protected: // protected members public: // public members };

3 Types of Access Specifiers zType 1: inherit as private BaseDerived private membersinaccessible protected membersprivate members public membersprivate members

3 Types of Access Specifiers zType 2: inherit as protected BaseDerived private membersinaccessible protected members public membersprotected members

3 Types of Access Specifiers zType 3: inherit as public BaseDerived private membersinaccessible protected members public members

Constructor and Destructor zIt is possible for both the base class and the derived class to have constructor and/or destructor functions. zThe constructor functions are executed in order of derivation. yi.e.the base class constructor is executed first. zThe destructor functions are executed in reverse order.

Passing arguments zWhat if the constructor functions of both the base class and derived class take arguments? 1.Pass all necessary arguments to the derived class’s constructor. 2.Then pass the appropriate arguments along to the base class.

Example: Constructor of base class base { int i; public: base(int n) { cout << “constructing base \n”; i = n; } ~base() { cout << “destructing base \n”; } };

Example: Constructor of derived class derived : public base { int j; public: derived (int n, int m) : base (m) { cout << “constructing derived\n”; j = n; } ~derived() { cout << “destructing derived\n”;} };

Example: main() int main() { derived o(10,20); return 0; } constructing base constructing derived destructing derived destructing base

Multiple Inheritance zType 1: base 1 derived 1 derived 2

Multiple Inheritance Type 2: base 1base 2 derived

Example: Type 2 // Create first base class class B1 { int a; public: B1(int x) { a = x; } int geta() { return a; } };

Example: Type 2 // Create second base class class B2 { int b; public: B2(int x) { b = x; } int getb() { return b; } };

// Directly inherit two base classes. class D : public B1, public B2 { int c; public: D(int x, int y, int z) : B1(z), B2(y) { c = x; } void show() { cout << geta() << getb() << c;} } ; Example: Type 2

Potential Problem zBase is inherited twice by Derived 3! Base Derived 1 Derived 2 Derived 3

Virtual Base Class zTo resolve this problem, virtual base class can be used. class base { public: int i; };

Virtual Base Class // Inherit base as virtual class D1 : virtual public base { public: int j; }; class D2 : virtual public base { public: int k; };

Virtual Base Class /* Here, D3 inherits both D1 and D2. However, only one copy of base is present */ class D3 : public D1, public D2 { public: int product () { return i * j * k; } };

Pointers to Derived Classes zA pointer declared as a pointer to base class can also be used to point to any class derived from that base. zHowever, only those members of the derived object that were inherited from the base can be accessed.

Example base *p; // base class pointer base B_obj; derived D_obj; p = &B_obj; // p can point to base object p = &D_obj; // p can also point to derived // object

Virtual Function zA virtual function is a member function ydeclared within a base class yredefined by a derived class (i.e. overriding) zIt can be used to support run-time polymorphism.

Example class base { public: int i; base (int x) { i = x; } virtual void func() {cout << i; } };

Example class derived : public base { public: derived (int x) : base (x) {} // The keyword virtual is not needed. void func() {cout << i * i; } };

Example int main() { base ob(10), *p; derived d_ob(10); p = &ob; p->func(); // use base’s func() p = &d_ob; p->func();// use derived’s func() }

Pure Virtual Functions zA pure virtual function has no definition relative to the base class. zOnly the function’s prototype is included. zGeneral form: virtual type func-name(paremeter-list) = 0

Example: area class area { public: double dim1, dim2; area(double x, double y) {dim1 = x; dim2 = y;} // pure virtual function virtual double getarea() = 0; };

Example: rectangle class rectangle : public area { public: // function overriding double getarea() { return dim1 * dim2; } };

Example: triangle class triangle : public area { public: // function overriding double getarea() { return 0.5 * dim1 * dim2; } };