Inheritance Is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Shape.h Shape Point Circle Cylinder // SHAPE.H
Inheritance Inheritance-2. Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void);
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Object-Oriented Programming C programmers concentrate on writing functions.functions By contrast, MC++ programmers concentrate in creating their own user-defined.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
Properties Methods of a class can manipulate that class’s private data members Methods of a class can manipulate that class’s private data members Classes.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
1 Review (Week7)_ Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
計算機概論實習 Class Sample: RECT Class weight length class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter();
UML Class Diagram: class Rectangle
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.
Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.
1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
C++ Lecture 7 Thursday, 21 July Chapter 9 Inheritance l Creating new classes from the existing classes l The notions of base classes and derived.
CLASS INHERITANCE WEEK 7 Chapter 20 CS SENEM KUMOVA METIN1.
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
CMSC 2021 Polymorphism. CMSC 2022 Static vs. Dynamic Binding Binding The determination of which method in the class hierarchy is to be used for a particular.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
P Improvements - Constructor p Parameter passing Constructors Problem Solving With C++
Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Outline Class and Object-Oriented Programing –Encapsulation and inheritance Example –Commission Employee –Encapsulation and inheritance Strings and Formatting.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
1 Chapter 4: Another way to define a class Inheritance..!!
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
1 Inheritance inheritance –a mechanism that build a new class by deriving from an existing class –derived class (or subclass) inherit from based class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming: Inheritance
Classes and Objects Introduced
Lecture 11: Virtual Function and Pure Virtual Function
Chapter 9 - Object-Oriented Programming: Inheritance
Chapter 9 – Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
UML Class Diagram: class Rectangle
Learning Objectives Inheritance Virtual Function.
MSIS 670 Object-Oriented Software Engineering
Polymorphism CT1513.
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Practical Session 2 Class Design
Chapter 20- Virtual Functions and Polymorphism
Chapter 20 - C++ Virtual Functions and Polymorphism
Object-Oriented Programming: Polymorphism
Chapter 9 - Object-Oriented Programming: Inheritance
Virtual Functions Example
Computer Science II for Majors
Presentation transcript:

Inheritance Is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with new capabilities Programmer designate that the new class should inherit the members of an existing class

Class Point { int x; int y; public: void setX(int xval); void setY(int yval); int getX(); int getY(); void print() const; }; Class Circle {int x; int y; double r; public: void setX(int xval); void setY(int yval); void setR(double rad); int getX(); int getY(); double getR(); double area(); double diameter(); double circumference(); void print() const; };

Class Circle repeats part of class Point Software reusability saves time during program development Existing class is called base class The new class is referred to as derived class A derived class represents a more specialized group of objects A derived class contains behaviors inherited from its base class plus additional behaviors

Vehicle CarsTrucks Motorcycle 2 doors 4 doors Inheritance

Point Circle rectangle cylinder

shape Two Dim shape Three Dim shape Circle Square Trianglesphere cube

Person Employee student Full TimePart time Undergraduate Graduate Student worker

C++ offers three kind of inheritance Public Protected Private Syntax class derived : public base {... } Every object of a derived class is also an object of base class Base class object are not objects of their derived class

Public inheritance Derived class member function might required access to base class data member and member functions A derived class can access the non- private members of its base class A derived class cannot access the private member of its base class

Class Circle inherits class Point class Circle : public Point { double r; public: void setR(double rad); double getR(); void print(); double area(); double diameter(); double circumference(); };

Protected member A base class’s protected members can be accessed by members and friends of that base class and by members and friends of any classes derived from that base class

Rewrite Point and Circle class class Point { protected: int x; int y; public: void setX(int xval); void setY(int yval); int getX(); int getY(); void print() const; }; class Circle:public Point {double r; public: void setR(double rad); double getR(); double area(); double diameter(); double circumference(); void print() const; };

Constructors class Point { protected: int x; int y; public: Point(int=0,int=0); void setX(int xval); void setY(int yval); int getX(); int getY(); void print() const; }; class Circle:public Point {double r; public: Circle(int=0,int=0,double=0); void setR(int rad); double getR(); double area(); double diameter(); double circumference(); void print() const; };

implementation Point::Point(int xval, int yval) { x = xval; y = yval; } void Point::print() const { cout<<“Point is at (“ <<x<<“,”<<y<<“)”; } Circle::Circle(int xval, int yval, double rval) { x = xval; y = yval; r = rval; } void Circle::print() const { cout<<“center at (“<<x<<“,” <<y<<“)”; cout<<“radious is “<<r; }

Point want to have private data members class Point { int x; int y; public: Point(int=0,int=0); void setX(int xval); void setY(int yval); int getX(); int getY(); void print() const; }; class Circle:public Point {double r; public: Circle(int=0,int=0,double=0); void setR(int rad); double getR(); double area(); double diameter(); double circumference(); void print() const; };

implementation Point::Point(int xval, int yval) { x = xval; y = yval; } Void Point::print() const { Cout<<“Point is at (“ <<x<<“,”<<y<<“)”; } Circle::Circle(int xval, int yval, double rval):Point( xval, yval) { r = rval; } void Circle::print() const { cout<<“center at )“; Point::print(); cout<<“radious is “<<r; }