Inheritance ITK 169 Fall 2003 Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Operator overloading redefine the operations of operators
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Functions Prototypes, parameter passing, return values, activation frams.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
C++ Classes & Data Abstraction
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
Const Parameters & In Line Functions 04/15/11. Next Time  Quiz, Monday, 04/18/11  Over 5.2 and 5.3 void functions pass-by-reference  Read 7.1 about.
Inheritance1 Inheritance Software re-use with derived classes.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
Static Class Member Function We can declare a member function being static. A static member function doesn’t have to be invoked by an object. A static.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
Chapter 12: Adding Functionality to Your Classes.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Inline Functions, Operator Overloading and Inheritance COSC 102: Programming in C++
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Module 10: Simple Inheritance #1 2000/01Scientific Computing in OOCourse code 3C59 Module 10: Simple Inheritance In this module we will cover Inheritance.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
Inheritance One of the most powerful features of C++
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
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.
CS410 – Software Engineering Lecture #12: C++ Basics V
More Object Oriented Programming
Data structures in C++.
Operator Overloading; String and Array Objects
Domain Classes Chapter 9.
Abstraction: Generic Programming, pt. 2
Lists - I The List ADT.
Institute of Petroloeum Technology, Gandhinagar
CIS 199 Final Review.
Lab4 problems More about templates Some STL
Anatomy of a class Part II
Class: Special Topics Overloading (methods) Copy Constructors
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
Object-Oriented Programming (OOP) Lecture No. 27
Overloading the << operator
Presentation transcript:

Inheritance ITK 169 Fall 2003

Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec Center decided to keep track of those members who play table tennis. Consider the base class Player listed on the next slide.

class Player { private: string fName, lName; bool hasTable; public: // default and special constructor in one using default values… Player(const string& first = "empty", const string& last = "empty", bool table = false); void set_fName(const string& first) { fName = first; } void set_lName(const string& last) { lName = last; } void set_hasTable(bool table) { hasTable = table; } string get_fullnameLF()const { return (lName + ", " + fName); } string get_fullnameFL()const { return (fName + " " + lName); } bool get_hasTable()const { return hasTable; } void printNameFL(ostream& outs) { outs << fName << " " << lName; } friend ostream& operator <<(ostream& outs, const Player& aPlayer); };

Derived Class Some members have played in local table tennis tournaments. They would like the class to include the point ratings they’ve earned. Rather than start from scratch, you can derive a class from the Player class. RatedPlayer will be the derived class.

Notice - : public Player class RatedPlayer : public Player { private: unsigned int rating; public: // default and special constructor in one using default values… RatedPlayer(unsigned int rt = 0, const string& first = "empty", const string& last = "empty", bool table = false); // constructor to add a rating to an existing player RatedPlayer(unsigned int rate, const Player& aPlayer); void set_rating(unsigned int num) { rating = num; } unsigned int get_rating() { return rating; } };

Public Derivation In the class header : public Player is added to indicate that –RatedPlayer is derived from Player, –and that Player is a public base class. With public derivation, all public members of the base class become public members of the derived class. The private portions of the base class become part of the derived class but they can only be accessed through public and protected methods of the base class. (More on protected methods later.)

So… An object of type RatedPlayer has in store: – 3 data members of Player fName, lName; hasTable; – plus a rating. An object of type RatedPlayer can use the methods of the base type and its own class. An object of type Player has only 3 data members and can only access Player methods.

Derived Requirements A derived class can add additional data members and member functions as needed. A derived class needs its own constructors. –Note the constructors must provide data for the any new data member(s) AND the base data members.

Access Considerations A derived class does not have direct access to the private members of the base class. It has to work through the base class methods. In particular, the derived constructors have to use the base class constructors.

Derived Constructors Each constructor includes a call to a Player constructor at the end of the parameter list - note the use of a : RatedPlayer::RatedPlayer(unsigned int rate, const string& first, const string& last, bool table) : Player(first, last, table) { rating = rate; } RatedPlayer::RatedPlayer(unsigned int rate, const Player& aPlayer) : Player(aPlayer), rating(rate) { } This is call an initializer list. Note: We can have more than just the Player constructor call here.

Key Points to Remember The base class object is constructed first. The derived class constructor should pass base class information to a base class constructor via a member initialization list. The derived class constructor should initialize those data members that were added to the derived class.

Using our 2 Classes Player mickey("Mickey", "Mouse"); RatedPlayer daffy(10, "Daffy", "Duck"); cout << mickey << " plays occasionally for fun.\n"; cout << daffy << " is a serious tournament player.\n"; daffy.printNameFL(cout); cout << " is rated " << daffy.get_rating( ) << " out of 50, in the city tournaments.\n";

Private Members Why would a derived class NOT have direct access to private data members? What about private functions? Is there a “go around”? Is it a good idea to allow direct access to private members (data or functions)?