Programming with ANSI C ++

Slides:



Advertisements
Similar presentations
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advertisements

Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
C++ Inheritance Systems Programming.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism &Virtual Functions
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CS  C++ allows multiple implementation inheritance  Handy for multiple “is-a” situations  Handy for reusing implementation without “is-a”  Leads.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Coming up: Inheritance
CITA 342 Section 1 Object Oriented Programming (OOP)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Inheritance and Run time Polymorphism
Chapter 5 Classes.
Polymorphism & Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Understanding Inheritance
Programming with ANSI C ++
Inheritance Basics Programming with Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Interfaces.
Polymorphism Polymorphism
Constructors and destructors
Constructors and Destructors
Computer Programming with JAVA
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Object-Oriented Programming: Inheritance and Polymorphism
Review of Previous Lesson
CPS120: Introduction to Computer Science
Final and Abstract Classes
COP 3330 Object-oriented Programming in C++
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Creating and Using Classes
Presentation transcript:

Programming with ANSI C ++ A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

Chapter 9 Inheritance

The need Mechanism of creating a new class from already defined class The new class contains all attributes of the old class in addition to some of its own attributes Additionally, it can also override some of the features of the old class. Not reinventing the wheel!

Continue We do not need to start from scratch A class once defined can be used multiple times by other applications with modifications Proper use of inheritance helps in getting better and more robust solutions quickly Class which for creating and processing windows on the screen

The Advantage The generic class may contain caption of the window, specific border, specific corners, specific size, specific look and feel, maybe minimize, maximize and close buttons as well Additional functionalities provided concentrate on our additional part quick Robust

What is Inheritance Mechanism of getting a derived class from base class Common in the real-world Properties of a base class are automatically available in the derived class

Hierarchy is not forced in C++! Java has a root class called Object C++ has no such notion We can program without such a hierarchy (object based programming)

Other advantages Programming using inheritance is known as object oriented programming Approach using object based method provides better performance but less flexible C++ gives you a choice Making Common Attributes Explicit in Base Class

Advantages: Mapping a Real World Hierarchy Help modeling isa relationship Part of relationship is different than isa it requires programming discipline not to inherit when the relationship is not subset-of or isa.

isa and Part of Relationships If we need to use functions that are defined in that class, it is better to rewrite them in our new class or design in such a way that such inheritance does not take place.

Meaning of Inheritance in C++ Which data member is available to derived class and in which form depends on the type of inheritance (private, public or protected) It also depends on the original access specifiers associated with the base class members

Derivation Using Public class Base {//Body of the base} class Derived : public Base { //body of the derived }

Derivation Using Private class Base {//Body of the base} class Derived : private Base { //body of the derived }

Protected Access Specifier When the class containing protected data members is inherited, the protected members are available to derived class member functions as well But they are not available to objects of derived class

Derivation Using Protected class Base {//Body of the base} class Derived : protected Base { //body of the derived }

Resultant access specifier Member type in a base class Type of derivation Member type in derived class Private Not available Protected Public

Effect on further inheritance The further inheritance access specifier The Effective Access Specifier value in further inheritance Public Private Protected Not Available

Implementation of Inheritance in C++ Object Model The compiler embeds the base class completely in a derived class at the time of derivation Consistent performance while accessing derived or base class element This efficiency has come for a price. Implementation using pointer is not efficient in terms of time.

Access Control All member functions of the derived class Public members of the class Private members of the class Protected members of the class All objects of the class as well as derived class All member functions of the derived class All member functions of the class and friends Access Control

Access Declaration What If we want to derive a class and want only a few and not all of the public members to be available ? Using access declaration, we can provide public access to some of the base class members even after deriving them as private The access specifier cannot be modified to raise the status of the original access specifier

Deriving Multiple Classes from a Single Base Class Suppose there are some student classes, say school student, undergraduate student and postgraduate students. Need to find out common elements in them The student class (the base class for all of them) is to be created with those elements Thus the base class contents are decided after derived class contents are determined.

Multiple Inheritance It is possible to derive a single class from more than one class. Ex. SportsStudent class is inherited from the student class; and it also inherits from SportsPerson class

Multiple Inheritance The object of SportsStudent class contains properties like name, address, etc. derived from the student class, and name of sport he/she is playing for, details about national and international sports events taken part in, medals won so far etc. which are derived from the SportsPerson class.

Multiple Inheritance There are some other attributes like sports subjects taken, sports points obtained (from practical exams and participation in various events), which are specific to a sports student

Normal Inheritance case Base class data Derived class personal data A further derived class personal data The contents of the class derived from a derived class To get base class object out of DDerived object this portion is to be taken out The address of the object To get derived class object out of DDerived object this portion is to be taken out

Multiple-Derivation Example To get Base2 class out of DerivedObject this portion is to be taken out Base Class1 personal data Base Class2 Personal data Derived class Personal data The contents of the class derived from a derived class in case of multiple inheritance The address of the object

The code which does not requires compiler intervening Base BaseObject; Derived DerivedObject; DDerived DDerivedObject; Base *BaseObjPtr = &DerivedObject Derived *DerivedObjPtr = &DDerivedObject

The code which does requires compiler intervening Class Derived: public Base1, public Base2 {..} Derived DerivedObject; Base2 *BasePtr2 = &DerivedObject; Above requires compiler to set the address to &DerivedObject + size of Base1 subobject If Base1 *BasePtr1 = &DerivedObject is written, the compiler does not need to intervene

The Designer’s Problem A tangled hierarchy We need to find out the value of an attribute for an object which has inherited from multiple classes If we cannot find that attribute, we need to traverse the hierarchy up to find the value. In a case of multiple-inheritance, it is possible to get multiple values for the same attribute

The tangled hierarchy Birds Can Fly! PetBirds Ostreaches Can not Fly! MyBirds

Deriving a Class from an Already Derived Class The C++ object model implementation is such that the further derived class will contain all ancestors within its body The suboject concept

Virtual Base Class A specific type of further inheritance class Base derived into two different classes Derived1 and Derived2. new class DDerived multiply derived from Derived1 and Derived2 Now there are two copies of Base copied in DDerived; one from Derived1 and the other from Derived2.

Multiple Further Derivation of Base We obviously need the scope resolution operator to say either “I need a base class object derived from Derived1” or “I need a base class object derived from Derived2”. DD.Derived1::BaseInt = 0; DD.Derived2::BaseInt = 10

Virtual to have a single copy qualification with a base class name is a serious problem in some cases. To solve this problem we have to precede the first derivation by keyword virtual We can either write virtual public or public virtual; there is no difference between them Now the BaseInt is a single one and there is no ambiguity

Example Whenever compiler finds virtual keyword with derivation, it would ensure that two instances of the same base class are not inherited into the class derived from the derived class of base. It is a challenge for the compiler to provide single instance of a class from two instances available and still provide compatibility when one type of object is assigned to another;

The Problem

The virtual base class structure Single copy of base class is available now. Compiler, because virtual keyword is found in Derived1 and Derived2 inheritance definitions, cares to include a single copy. DDerived Class Original members Derived1 Class original members Base Class Derived2 Class original members The DDerived class

Abstract Classes The classes without any objects are known as abstract classes. These classes are usually outcome of generalization process which finds out common elements of the classes of interest and stores them in a base class.

Abstract Classes Whenever a designer has to model an abstract concept which cannot have objects, then abstract classes are handy.

Abstract Classes Consider the class shape. It can be inherited to rectangle, ellipse, which in turn can be inherited to parallelograms into squares and circles. Except shape, all other can have objects of them (in real sense). Another example is of class Furniture.

Applications of Constructors and Destructors If there is a base class constructor available, the derived class must define one constructor for itself. In case of a default constructor though the situation is different the compiler will define one default constructor for us when we do not do so in derived

Applications of Constructors and Destructors When we have defined class1:public class2, public class3, public class4 … , classN then the constructor for class2 is called first, then the constructor for class 3 is called The call to base class constructors is to be defined outside the body of the constructor, in the member initialization list. Virtual base class is an exception

Applications of Constructors and Destructors The destructors of the base classes are called exactly in reverse order of their initialization when the derived object is destroyed.

Composite Objects (Container Objects)  Sometimes, the object itself contains some other objects as members. Such objects are known as composite objects This is the part of relationship unlike isa relationship discussed earlier we may not need to call the constructor of the component classes whenever we need to call the constructor of the container class

Containership and Inheritance Nose (a triangle) is part of Face. Left and right eyes (circles) are again part of Face. But all instances of triangles and circles are not instances of Face as well. Both type of members are treated by C++ in a similar way (embedded in the target class) so they seem similar while programming.

Containership and Inheritance