Institute of Petroloeum Technology, Gandhinagar

Slides:



Advertisements
Similar presentations
1 Inheritance Classes and Subclasses Or Extending a Class.
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
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.
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
1 CSC241: Object Oriented Programming Lecture No 27.
1 CSC241: Object Oriented Programming Lecture No 13.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Object Oriented Programming in C++ Chapter 6 Inheritance.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
Chapter -6 Polymorphism
Types of Inheritance in C++. In C++ we have 5 different types of inheritance: – Single Inheritance – Multiple Inheritance – Hierarchical Inheritance –
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Design issues for Object-Oriented Languages
Introduction to Object-oriented Programming
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming with ANSI C ++
Class and Objects UNIT II.
7. Inheritance and Polymorphism
Programming with ANSI C ++
Inheritance and Polymorphism
Inheritance & Polymorphism
Review: Two Programming Paradigms
Table of Contents Class Objects.
Inheritance in Java.
Classes & Objects.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 5 Classes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS212: Object Oriented Analysis and Design
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Object Oriented Programming Language (OOP)
Constructor & Destructor
Chapter 5 Function Basics
FUNCTIONS& FUNCTIONS OVERLOADING
Inheritance Dr. Bhargavi Goswami Department of Computer Science
More Object-Oriented Programming
Dr. Bhargavi Dept of CS CHRIST
Inheritance Cse 3rd year.
C++ Inheritance.
CLASSES AND OBJECTS.
Inheritance: Polymorphism and Virtual Functions
Object-Oriented Programming (OOP) Lecture No. 22
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Inheritance:Concept of Re-usability
9-10 Classes: A Deeper Look.
Overview of C++ Polymorphism
Chapter 9 Inheritance.
B.FathimaMary Dept. of Commerce CA SJC Trichy-02
CPS120: Introduction to Computer Science
Final and Abstract Classes
COP 3330 Object-oriented Programming in C++
9-10 Classes: A Deeper Look.
Corresponds with Chapter 5
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Institute of Petroloeum Technology, Gandhinagar INHERITANCE Object-Oriented Programming Using C++ By Darshit shah

Inheritance… Inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. e.g. The bird “sparrow” is a part of the class “flying bird” which is again a part of the class “bird”.

Inheritance… Each derived class shares common characteristics with the class from which it is derived. It provides reusability. We can add additional features to an existing class without modifying it when we derive a new class from existing one.

Inheritance… The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is referred to as the base class and the new one is called the derived class or subclass.

Inheritance… The new class will have the combined features of both the classes. Allows the programmer To reuse a class To tailor the class in such a way that it does not introduce any undesirable side-effects into the rest of the classes.

Different Types of Inheritance… Single Inheritance Multilevel Inheritance Multiple Inheritance Hierarchical Inheritance Hybrid Inheritance

Single Inheritance… A derived class with only one base class is called Single Inheritance. A B

Multilevel Inheritance… A class is derived from another derived class is known as multilevel inheritance. A B C

Multiple Inheritance… A derived class from several base classes is called multiple inheritance. A B C

Hierarchical Inheritance… Features of one class can be inherited by more than one class is known as hierarchical inheritance. A B C

Hybrid Inheritance… Combination of different types of inheritance is known as hybrid inheritance. A B C D

Defining Derived Classes… General Form… class derived-class name : visibility-mode base-class-name { … members of derived class … } Here, :  derived class name is derived from base-class-name. visibility-mode is optional. It can be either private, public or protected. By default, it is private.

Defining Derived Classes … Features Of Base Class … Visibility mode specifies whether The feature of the base class are privately derived or publicly derived. Example Class ABC: public XYZ { Members of abc };

Defining Derived Classes … Features Of Base Class … class ABC: private XYZ { MEMBERS OF ABC }; class ABC: XYZ // Privately Inherited

Defining Derived Classes … Features Of Base Class … class ABC: protected XYZ // Protected derivation { MEMBERS OF ABC }; This form is used rarely.

Example… Function Main ( )… index1 i; index j; j++; cout << "j."; j.display(); i++; i++; cout << "i."; i.display(); i--;

Example… Defining Class Index… class index // base class { protected: int count; public: index() count = 0; }

Example… Defining Class Index… index (int c) { count = c; } void display() cout << "count = " << count << endl;

Example… Defining Class Index… void operator ++ (int) { count++; } };

Example… Deriving Class Index1… class index1: public index // derived class { public: void operator -- (int) count--; } };

Example… Function Main ( ) & Output … index1 i; index j; j++; cout << "j."; j.display(); i++; i++; cout << "i."; i.display(); i--; OUTPUT j.count = 1 i.count = 2 i.count = 1

protected: instead of private: why? … Can member functions of the derived class access members of the base class? Can operator – – (int) in index1 access count in index?

protected: instead of private: Answer … Members of a derived class can access members of the base class only if the base class members are public or protected. They can’t access private members. We can’t make them public as they can be accessed from anywhere in the program and thus not good for data hiding.

protected: instead of private: Answer … Institute of Petroloeum Technology, Gandhinagar protected: instead of private: Answer … A protected member can be accessed by member functions in its own class or in any class derived from its own class. It can’t be accessed from functions outside these classes, such as main(). They behave just like private members until a new class is derived from the base class that has protected members. protected: making a private member inheritable By Darshit shah

FEW POINTS… We can increase the functionality of the base class (index) without modifying it through the derived class (index1). Inheritance does not work in reverse as the base class & its objects have no knowledge about any classes derived from the base class.

Try the following… In class index, declare variable count as private instead of protected. Change visibility mode specifier to private instead of public (write class index1: private index instead of class index1: public index) Call j--; from main.

EXAMPLE 2 …Function main ( ) … int n; stack1 stk; stk.push(10); stk.push(20); stk.push(30); stk.push(40); stk.push(50); n = stk.pop(); cout << "Value : " << n << " popped.“; getch();

EXAMPLE 2 … Defining class stack… const int MAX = 25; class stack { protected: int s[MAX], top; public: stack() top = -1; }

EXAMPLE 2 … Defining class stack… void push (int num) { top++; s [top] = num; } int pop() { int num; num = s[top]; top --; return num; } }; // end of class stack.

EXAMPLE 2 … Inheriting class stack1 from stack … class stack1: public stack { public: void push ( int num) if ( top == MAX - 1) cout << "Stack is full" << endl; else stack::push(num); }

EXAMPLE 2 … Inheriting class stack1 from stack … int pop() { int n; if (top == -1) cout << "Stack is empty." ; return NULL; }

EXAMPLE 2 … Inheriting class stack1 from stack … else { n = stack::pop(); return n; } };

EXAMPLE 2 … What is happening? … value : 50 popped value : 40 popped now change the value of MAX to 3 and run the program. Create object stk from stack class and run the program.

Few points… Here, both classes stack1 and stack have common functions. Which one would be executed? The function in the derived class gets a priority when the function is called as a member of the derived class object. A program can declare objects of both the base and derived classes. The two objects are independent of one another.

Few Points… Friend function can access private as well as protected data directly, The member functions of a derived class can access only the protected data.

Visibility Of Inherited Members… BASE CLASS VISIBILITY DERIVED CLASS VISIBILITY PUBLIC DERIVATION PRIVATE DERIVATION PROTECTED DERIVATION PRIVATE NOT INHERITED PROTECTED PUBLIC

Multilevel Inheritance… STUDENT BASE CLASS GRAND FATHER TEST INTERMEDIATE BASE CLASS FATHER RESULT DERIVED CLASS CHILD

Multilevel Inheritance… Class Declaration … class student { … }; class test : public student { … }; class result : public test { … };

Multilevel Inheritance… Student Class Declaration … class student { protected: int rn; public: void get_rn(int); void put_rn(void); };

Multilevel Inheritance… student class declaration … void student::get_rn(int rollno) { rn = rollno; } void student::put_rn(void) cout << "Roll No. = " << rn << endl;

Multilevel Inheritance… test class declaration … class test : public student { protected: int english , cp; public: void get_marks (int, int); void put_marks (void); };

Multilevel Inheritance… test class declaration … void test :: get_marks (int e, int c) { english = e; cp = c; }; void test :: put_marks (void) cout << "Marks in English = " << english << endl; cout << "Marks in Computer = " << cp << endl;

Multilevel Inheritance… result class declaration … class RESULT : public test { private: int total; public: void display (void); };

Multilevel Inheritance… result class declaration … void RESULT :: display (void) { total = english + cp; put_rn(); put_marks(); cout << "Total = " << total << endl; };

Multilevel Inheritance… What result class will have … private: int total; protected: int rn, english, cp; public: void get_rn (int); void put_rn (void); void get_marks( int , int); void put_marks (void); void display (void);

Multilevel Inheritance… main() function … void main() { clrscr(); RESULT s10; s10.get_rn(10); s10.get_marks(80,85); s10.display(); getch(); };

Multiple Inheritance… A derived class from several base classes is called multiple inheritance. A B C

Syntax Of Derived Class With Multiple Base Classes… class D : visibility BC-1, visibility BC-2 { … … body of D. } Here, D  Derived Class, BC1 base-class-1 & BC2 base-class-2

Multiple Inheritance… It allows us to combine the features of several existing classes as a starting point for defining new classes. It is like a child inheriting the physical features of one parent and the intelligence of another. The derived class will contain all the members of all base-classes in addition to its own members.

Multiple Inheritance… Example… class father { private: char f_name[50]; char f_blood_grp[5]; int f_height_in_feet; int f_height_in_inch;

Multiple Inheritance… Example… public: void f_init( char*nm, char*bg, int hf, int hi) { strcpy(f_name,nm); strcpy(f_blood_grp,bg); f_height_in_feet = hf; f_height_in_inch = hi; };

Multiple Inheritance… Example… void f_display(void) { cout << "Name of father = " << f_name; cout << "Blood Group = " << f_blood_grp; cout << "Height = " << f_height_in_feet << " Feet & " << f_height_in_inch << " Inches "; } };

Multiple Inheritance… Example… class mother { private: char m_name[50]; char m_blood_grp[5]; int m_height_in_feet; int m_height_in_inch;

Multiple Inheritance… Example… public: void m_init( char*nm, char*bg, int hf, int hi) { strcpy(m_name,nm); strcpy(m_blood_grp,bg); m_height_in_feet = hf; m_height_in_inch = hi; };

Multiple Inheritance… Example… void m_display(void) { cout << "Name of Mother = " << m_name; cout << "Blood Group = " << m_blood_grp; cout << "Height = " << m_height_in_feet << " Feet & " << m_height_in_inch << " Inches "; } };

Multiple Inheritance… Example… class child : public father, public mother { private: char c_name[50]; char c_blood_grp[5]; int c_height_in_feet; int c_height_in_inch;

Multiple Inheritance… Example… public: void c_init( char*nm, char*bg, int hf, int hi) { strcpy(c_name,nm); strcpy(c_blood_grp,bg); c_height_in_feet = hf; c_height_in_inch = hi; };

Multiple Inheritance… Example… void c_display(void) { f_display(); m_display(); cout << "Name of child = " << c_name; cout << "Blood Group = " << c_blood_grp; cout << "Height = " << c_height_in_feet << " Feet & " << c_height_in_inch << " Inches "; } };

Multiple Inheritance… Example… void main() { child c1; c1.f_init("Darshit","O+",5,11); c1.m_init("Ragi","A+",5,7); c1.c_init("Aashna",“O+",5,7); c1.c_display(); };

Ambiguity Resolution In Inheritance … Institute of Petroloeum Technology, Gandhinagar Ambiguity Resolution In Inheritance … What happens, if same function name appears in different base classes from where we derive another class? Answer: Use class resolution operator to invoke function of specific class. Explain this slide with the help of program inherit6.cpp. By Darshit shah

Institute of Petroloeum Technology, Gandhinagar Virtual Base Classes… Suppose, we have multilevel, multiple and hierarchical inheritance , all together in one derived class, there will be duplication of members in derived class. See the example. Explain this slide with the help of program inherit6.cpp. By Darshit shah

Institute of Petroloeum Technology, Gandhinagar EXAMPLE… GRAND PARENT PARENT 1 PARENT 2 Explain this slide with the help of program inherit6.cpp. CHILD By Darshit shah

EXAMPLE… The child class is derived from two base classes called parent 1 and parent 2. Parent 1 and parent 2 are derived from common base class grand parent. Thus child inherits the traits of grand parent via two separate path. i.e. child will have duplicate set of members inherited from grand parent.

Virtual Base Class … This duplication can be avoided by making the common base class as virtual base class while declaring. When a class is made a virtual base class, C++ ensures that only one copy of class is inherited, regardless of how many inheritance paths exist between the virtual base class and a derived class.

Declaration of Virtual Base Class… class gp { … }; class p1: virtual public gp { … }; class p2 : public virtual gp

Declaration of Virtual Base Class… class child: public p1, public p2 { … // Only one copy of gp will be inherited. … }; The keywords virtual and public may be used in either order.

Constructors In Derived Classes… We know that we use constructors for initializing the objects. There may or may not be constructors in base class or derived classes. These constructors may or may not have arguments.

Constructors… Mandatory Or Not… No. of Argument Base Class Derived Class 0 Or >0 Absent May Or May Not Have Constructors Present >0 Must Have Constructors

Order of Execution of Constructor Functions… When both classes contains constructors, the base constructor is executed first and then the derived class constructor will be executed. In case of multiple inheritance, the base classes are constructed in the order in which they appear in the declaration of the derived class. In case of multilevel inheritance, the constructors will be executed in the order of inheritance.

Order Of Execution Of Constructor Functions… Virtual base class constructors are always invoked first. class B : public A { };  A() , B() class A : public B, public C { };  B( ) , C( ), A( ) class A : public B, virtual public C { };  C( ) , B( ), A( )

How To Pass Arguments To Constructors? … Derived class is responsible for supplying initial values to its base class. When we create objects from derived class, we supply all values to derived class constructor. Which in turn will pass to the base constructors in the order in which they are declared in the derived class.