This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
C++ Classes & Data Abstraction
1 class Rectangle{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); float area(); }; Inheritance Concept.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Inheritance – Derived Classes. Employee Inheritance Hierarchy Employee HourlyEmp SalariedEmp derived classes base class.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 4 Inheritance.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
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 IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
CS212: Object Oriented Analysis and Design Lecture 14: Reusing classes in C++
Chapter 10 Inheritance and Polymorphism
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Foundations: Language mechanisms and primitive OO concepts Lecture 1: Classification and Inheritance Michigan State University Spring 2008 E. Kraemer Notes.
Chapter -6 Polymorphism
Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
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.
1 Another way to define a class Inheritance..!!. 2 Why Inheritance ? Inheritance is a mechanism for building class types from existing class types defining.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
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..!!
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
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.
Chapter 2 Objects and Classes
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Inheritance Concept Polygon Rectangle Triangle class Rectangle{
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Introduction to Classes
Chapter 5 Classes.
Constructor & Destructor
Understanding Inheritance
Introduction to Classes
Learning Objectives Inheritance Virtual Function.
Chapter 6: UNDERSTANDING FRIENDS AND OVERLOADING OPERATORS
Object-Oriented Programming (OOP) Lecture No. 22
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Another way to define a class
Chapter 11 Classes.
Inheritance in C++ Inheritance Protected Section
Computer Science II for Majors
Presentation transcript:

This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson Learning, Inheritance Chapter 12

COMP Inheritance2 Extending Class from other classes We have seen basic class functions Data hiding (private, protected, public) Friend Functions Overloading (functions, operators) One of the most powerful elements of ADT is the ability to derive classes from other classes This provides the ability to create new classes while retaining the basic characteristics of the original This concept is called: Inheritance

COMP Inheritance3 Figure 12-1 Case Study: Simple Polygons

COMP Inheritance4 Polygon Base Class Figure 12-2

COMP Inheritance5 Figure 12-3, Part I class Polygon { protected: double area; double perimeter; public: Polygon() {}; ~Polygon() {}; void printArea(); void printPeri(); }; // class Polygon Class Definition of Polygon

COMP Inheritance6 Private, Protected and Public Private data and functions Can only be accessed within the class Protected data and functions Can be accessed within the class as well as the derived class from this class Public data and functions Can be accessed by “everyone”.

COMP Inheritance7 Polygon : Function definition void Polygon :: printArea () { cout << "The area of your polygon is " << area << endl; } // Polygons printArea void Polygon :: printPeri () { cout << "The perimeter of your polygon is " << perimeter << endl; } // Polygons printPeri

COMP Inheritance8 Figure 12-3, Part III class Rectangle : public Polygon { private: double sideA, sideB; void calcArea(); void calcPeri(); public: Rectangle(double a, double b); }; Rectangle Object & Polygon

COMP Inheritance9 Derived Class Syntax class derived_class_name : inheritance_type base_class_name Example: class Rectangle : public Polygons class Rectangle : protected Polygons class Rectangle : private Polygons Inheritance rules: All data members of the base object are inherited All function members of the base object are inherited, except: a. Constructors b. Destructors c. friend functions d. Assignment operators

COMP Inheritance10 Inheritance of data and functions class Rectangle : public Polygon { private: double sideA, sideB; void calcArea(); void calcPeri(); protected: double area; double perimeter; public: Rectangle(double a, double b); void printArea(); void printPeri(); }; Inherited Data and Functions

COMP Inheritance11 Rectangle: Constructor and functions Rectangle::Rectangle(double A, double B) { sideA = A; sideB = B; calcPeri( ); // update perimeter field calcArea( ); // update area field } void Rectangle::calcPeri() { perimeter = 2*(sideA + sideB); } void Rectangle::calcArea() { area = sideA * sideB; }

COMP Inheritance12 Main( ) : using Rectangle type void main() { Rectangle rect(3,4); rect.printArea( ); rect.printPeri( ); }

COMP Inheritance13 Figure 12-3, Part II Polygon: Triangle Object class Triangle : public Polygon { private: double sideA, sideB, sideC; void calcArea(); void calcPeri(); public: Triangle(double a, double b, double c ); };

COMP Inheritance14 Inheritance of data and functions class Triangle : public Polygon { private: double sideA, sideB, sideC; void calcArea(); void calcPeri(); protected: double area; double perimeter; public: Triangle(double a, double b, double c); void printArea(); void printPeri(); };

COMP Inheritance15 Triangle: Constructor and Functions Triangle::Triangle(double A,double B,double C) { sideA = A; sideB = B; sideC = C; calcPeri(); // update perimeter field calcArea();// update area field } void Triangle::calcPeri(){ perimeter = sideA + sideB + sideC; } void Triangle::calcArea(){ // Compute the area of a triangle based on // the given side lengths }

COMP Inheritance16 Main( ) : using Triangle type void main() { Triangle tri(3, 4, 5); tri.printArea(); tri.printPeri(); } void main() { Rectangle rect(3, 4); rect.printArea(); rect.printPeri(); } compared with

COMP Inheritance17 Base and Derived Class Access Inheritance TypeBase Access TypeDerived Access Type private inaccessible protectedprivate publicprivate protected privateinaccessible protected publicprotected public privateinaccessible protected public Note: This is confusing for C++ programmers

COMP Inheritance18 Another Example class EmpInfo { private: int ID; protected: int Salary; public: char Name[200]; }; Base Class class EmpInfo : private SecurityEmp {... } ID (private)  inaccessible Salary (protected)  private Name (public)  private class EmpInfo : protected ImportantEmp {... }; ID (private)  inaccessible Salary (protected)  protected Name (public)  protected class EmpInfo : public RegularEmp {... }; ID (private)  inaccessible Salary (protected)  protected Name (public)  public Derived Classes

COMP Inheritance19 Inheritance types (p.601) For all types, Private member data/functions in the base class are inaccessible in the derived class Private (default type) Protected and public data/functions in the base class become private in the derived class Protected Protected and public data/functions in the base class become protected in the derived class Public Protected and public types are preserved!

COMP Inheritance20 Constructors and Derived Types Since Constructors are not inherited, derived types must declare their own constructors, e.g. Polygon case. When a derived class is constructed, it must first execute the constructor for the base class Sometimes we need to pass data to the base class’ constructor (initialize constructor). How?  with base-member-initialization list, e.g. Employee case.

COMP Inheritance21 Example: Employee Class (p ) class Employee { protected: int id; public: Employee(int idIn); }; class SalaryEmp:public Employee { protected: int salary; public: SalaryEmp(int idIn, int sal); }; class HourlyEmp:public Employee { protected: float payrate, hours; public: HourlyEmp(int idIn, float rate); }; Figure 12-6 (p.608) Employee id : integer SalaryEmp salary : integer HourlyEmp payRate : real hours : real

COMP Inheritance22 Constructors Employee::Employee(int idIn) { id = idIn; } SalaryEmp::SalaryEmp(int idIn, int sal) : Employee(idIn) { salary = sal; } HourlyEmp::HourlyEmp(int idIn, float rate) : Employee(idIn) { payRate = rate; hours = 0.0; } pass idIn to Employee(idIn) constructor pass idIn to Employee(idIn) constructor

COMP Inheritance23 Base-Class-Initialization SalaryEmp::SalaryEmp(int idIn, int sal): Employee(idIn) { salary = sal; }  Explicit “base-class-initialization” - Employee(idIn)  Inherited-class constructor SalaryEmp::SalaryEmp(int idIn, int sal) calls the explicit-value base-class constructor Employee(idIn)

COMP Inheritance24 Summary Polygon Case Study Access types Private, Protected, Public (Confusing part of C++) Constructor initialization syntax Employee example