Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.

Slides:



Advertisements
Similar presentations
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
I NHERITANCE Chapter 7 Department of CSE, BUET 1.
Learners Support Publications Inheritance: Extending Classes.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Reconfigurable Computing S. Reda, Brown University Reconfigurable Computing (EN2911X, Fall07) Lecture 13: SystemC (1/3) Prof. Sherief Reda Division of.
Distribution of Marks Internal Sessional Evaluation Assignments – 10 Quizzes – 10 Class Participation Attendence – 5 Mid – Term Test – 25 External Evaluation.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
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.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented Programming Task-Based Programming circle method draw data C.draw()
C++ Classes CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
C++ Review (3) Structs, Classes, Data Abstraction.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
INHERITANCE IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 10 Inheritance and Polymorphism
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.
Chapter -6 Polymorphism
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
CITA 342 Section 1 Object Oriented Programming (OOP)
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Classes, Interfaces and Packages
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
CSIS 123A Lecture 10 Inheritance. Organizing Code Why should programs be organized? –Humans are "complexity challenged" Need simplicity, clarity, efficiency.
1 Chapter 4: Another way to define a class Inheritance..!!
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
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.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes C++ representation of an object
Today’s Objectives 5-Jul-2006 Announcements
7. Inheritance and Polymorphism
Inheritance: hiding methods
Inheritance & Polymorphism
Chapter 5 Classes.
Polymorphism Lec
CS1201: Programming Language 2
By Muhammad Waris Zargar
Chapter 11: Inheritance and Composition
Inheritance -I.
Classes C++ representation of an object
Constructors & Destructors
Presentation transcript:

Chapter 9

Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits all properties of its superclass and can define its own unique properties. Subclass can redefine inherited methods. Generalization: process of forming a superclass Specialization: forming a subclass

Cont’d Classes that are derived from others inherit all the accessible members of the base class. E.g. Base class includes a member A and we derive it to another class with another member called B, the derived class will contain both members A and B. In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format: class derived_class_name: public base_class_name { /*...*/ };

Reusability Once a class has been written, created and debugged, it can be distributed to other programs to reuse in their own programs called ”reusability” E.g. Library of functions, third party softwares Inheritance provides important extention to the idea of reusability Reduces software development time

Example class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class CRectangle: public CPolygon { public: int area () { return (width * height); } }; class CTriangle: public CPolygon { public: int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); cout << rect.area() << endl; cout << trgl.area() << endl; return 0; }

Access Specifier

What is inherited from the base class? A derived class inherits every member of a base class except: its constructor and its destructor its operator=() members its friends The constructors and destructors of the base class are not inherited themselves Default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed

Cont’d An overloaded constructor can be called when a new derived object is created as following derived_constructor_name (parameters) : base_constructor_name (parameters) {...}

Example class mother { public: mother () { cout << "mother: no parameters\n"; } mother (int a) { cout << "mother: int parameter\n"; } }; class daughter : public mother { public: daughter (int a) { cout << "daughter: int parameter\n\n"; } }; class son : public mother { public: son (int a) : mother (a) { cout << "son: int parameter\n\n"; } }; int main () { daughter cynthia (0); son daniel(0); return 0; } OUTPUT mother: no parameters daughter: int parameter mother: int parameter son: int parameter

Public and Private Inheritance The keyword public specifies that objects of the derived class are able to access public member functions of the base class The Keyword private specifies that objects of the derived class cannot access public member functions of the base class

Multiple Inheritance A class inherits members from more than one class This is done by simply separating the different base classes with commas in the derived class declaration class CRectangle: public CPolygon, public COutput; class CTriangle: public CPolygon, public COutput;

Example class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class COutput { public: void output (int i); }; void COutput::output (int i) { cout << i << endl; } class CRectangle: public CPolygon, public COutput { public: int area () { return (width * height); } }; class CTriangle: public CPolygon, public COutput { public: int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); rect.output (rect.area()); trgl.output (trgl.area()); return 0; } OUTPUT 20 10

Ambiguity in Multiple Inheritance Two base classes have functions with the same name, how do object of derived class can access the correct base class function Class A{ public void show(){….}}; Class B{ public void show(){….}}; Class C: public A, public B{}; void main(){ C objC; objC.show(); // ambiguous—will not compile objC.A::show();// resolved by :: scope resolution operator objC.B::show(); }

Cont’d If you derive a class from two classes that are each derived from the same class (Diamond-shape Inheritance) Class A{ public void func();}; Class B:public A{ }; Class C: public A{} Class D: public B, public C{ }; Void main(){ D objD; objD.func(); // ambiguous.. Will not compile }