Inheritance One of the most powerful features of C++

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
1 CSE 303 Lecture 23 Inheritance in C++ slides created by Marty Stepp
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 - Inheritance Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes 19.3Protected.
Inheritance, Polymorphism, and Virtual Functions
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 12: Adding Functionality to Your Classes.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
1 Chapter 8 Destructor & Operator Overloading. 2 Destructor  A destructor is a function that is called when an object is no longer required. A constructor.
Access Control Under Inheritance tMyn1 Access Control Under Inheritance The private data members of a base class are also members of the derived class,
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.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Object Oriented Programming in C++ Chapter 6 Inheritance.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
Understanding Polymorphism tMyn1 Understanding Polymorphism Polymorphism requires the use of derived classes. It always involves the use of a pointer to.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
Constructor Operation tMyn1 Constructor Operation in a Derived Class So far the default base class constructor has been called automatically. We can arrange.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
A First Book of C++ Chapter 12 Extending Your Classes.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Chapter 2 Objects and Classes
Procedural and Object-Oriented Programming
Introduction to C++ (Extensions to C)
Classes C++ representation of an object
Abstract Data Types Programmer-created data types that specify
Object-Oriented Design (OOD) and C++
Inheritance & Polymorphism
Introduction to Classes
Chapter 5 Classes.
group work #hifiTeam
Introduction to Classes
CS1201: Programming Language 2
Chapter 19 - Inheritance Outline 19.1 Introduction
Object-Oriented Programming (OOP) Lecture No. 22
What about multi-dimensional arrays?
Chapter 19 - Inheritance Outline 19.1 Introduction
Chapter 11: Inheritance and Composition
Classes C++ representation of an object
Class rational part2.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Inheritance One of the most powerful features of C++

Derived and base classes A derived class automatically has all the members of the base class –This includes both data members and member functions –It usually also has additional member functions and/or data members Terminology –The base class is often called the parent –The derived class is call the child

Characteristics of derived classes Since the derived class can add data members and member functions, it is often larger than the base class The derived class is more specific than its base class and represents a smaller group of objects The real strength of inheritance comes from the ability to define additions, replacements or refinements to the features inherited from the base class.

Types of inheritance Single inheritance –A class is derived from one base class –Straightforward, relatively easy to use Multiple inheritance –A derived class inherits from multiple (possibly unrelated) base classes –Complex and error prone—but very powerful –We will become proficient with single inheritance before tackling this

Protected members Three types of access control: –If a member of a class is public, it can be accessed by anyone. –If it is private, it can only be accessed by other members and their friends ( but not by derived classes). –If it is protected it can be accessed by derived classes (and their friends) as well as its own members. Data members of classes that might at some point be base classes should be protected, not private. BUT…note that protected data “breaks” encapsulation

Syntax—base class header First write the base class #ifndef BOX_H #define BOX_H class Box { public: Box(double l=1.0, double b=1.0, double h=1.0); double volume() const; protected: double length; double breadth; double height; }; #endif

Base class definition #include "box.h" // Constructor Box::Box(double l, double b, double h) : length(l), breadth(b), height(h) {} // Function to calculate the volume of a Box //object double Box::volume() const { return length*breadth*height; }

Derived class header #ifndef CARTON_H #define CARTON_H #include "Box.h" #include class Carton : public Box { public: Carton(const char* pStr = "Cardboard"); ~Carton(); // Destructor private: char* pMaterial; }; #endif

Derived class definition #include "Carton.h“ #include // Constructor Carton::Carton(const char* pStr) { // Allocate space for the string pMaterial = new char[strlen(pStr)+1]; strcpy( pMaterial, pStr); // Copy it } // Destructor Carton::~Carton() { delete[] pMaterial; }

Driver #include #include "Box.h" #include "Carton.h" using namespace std; int main() { // Create a Box and two Carton objects Box myBox(40.0, 30.0, 20.0); Carton myCarton; Carton candyCarton("Thin cardboard"); cout << endl; cout << "myBox volume is " << myBox.volume() << endl; cout << "myCarton volume is " << myCarton.volume(); cout << "candyCarton volume is " << candyCarton.volume() << endl; return 0; }

Output from driver myBox volume is myCarton volume is 1 candyCarton volume is 1

“is a” and “has a” relationships “is a” is an inheritance relationship –The derived object should be a type of the base object “has a” is a composition relationship –One object contains an instance of another class This is the difference between inheritance and composition

Overriding base-class members in a derived class If the derived class supplies a new version of a function with the same signature as the base class, the base class function is hidden –If the signature is different, the function is overloaded The scope resolution operator may be used to access the base-class version form the derived class

Multiple inheritance A derived class can have as many direct base classes as your application needs Multiple inheritance is sometimes used so that the derived lcass defines an object that is a specialization of two or more different class types (concurrently) It is usually used to add the features of the base classes together to form a composite object containing the capabilities of its base classes One example is the class iostream is derived from both the class istream and the class ostream