Lecture 10: Introduction to Inheritance

Similar documents
CS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ;

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS250 Final Review Questions

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

CS 11 C++ track: lecture 1

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

CS250 Final Review Questions

CS250 Final Review Questions

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Written by John Bell for CS 342, Spring 2018

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Assumptions. History

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Week 8: Operator overloading

And Even More and More C++ Fundamentals of Computer Science

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Operator overloading

INHERITANCE - Part 1. CSC 330 OO Software Design 1

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

C++ Important Questions with Answers

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

Come and join us at WebLyceum

Object Oriented Programming: Inheritance Polymorphism

Scope. Scope is such an important thing that we ll review what we know about scope now:

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS201- Introduction to Programming Current Quizzes

ITI Introduction to Computing II

An Introduction to C++

Implementing an ADT with a Class

G52CPP C++ Programming Lecture 13

Object Oriented Software Design II

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

CGS 2405 Advanced Programming with C++ Course Justification

CMSC202 Computer Science II for Majors

ITI Introduction to Computing II

Object Oriented Software Design II

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

C++ 8. Constructors and Destructors

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

VIRTUAL FUNCTIONS Chapter 10

Java Programming. Manuel Oriol, March 22nd, 2007

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Programming, numerics and optimization

Inheritance, and Polymorphism.

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Object Oriented Design

Java Classes & Primitive Types

Programming in C++: Assignment Week 3

CS105 C++ Lecture 7. More on Classes, Inheritance

Lecture 18 Tao Wang 1

Array Elements as Function Parameters

Lecture 15a Persistent Memory & Shared Pointers

CS Lecture #14

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Seminar on Simulation of Telecommunication Sistems. Introduction to object-oriented programming: C++

Polymorphism. Zimmer CSCI 330

Java Classes & Primitive Types

CLASSES AND OBJECTS IN JAVA

C++ Constructor Insanity

CS11 Introduction to C++ Fall Lecture 7

COMP 2355 Introduction to Systems Programming

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

SFU CMPT Topic: Has-a Relationship

C++ Programming: Inheritance

C++ STREAMS; INHERITANCE AS

CS201 Some Important Definitions

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

pointers & references

CS11 Intro C++ Spring 2018 Lecture 3

Java Object Oriented Design. CSC207 Fall 2014

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Arrays Classes & Methods, Inheritance

EL2310 Scientific Programming

Data Structures using OOP C++ Lecture 3

Come and join us at WebLyceum

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Financial computing with C++

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++


PROGRAMMING IN C++ COURSE CONTENT

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Composition I. composition is a way to combine or compose multiple classes together to create new class

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Transcription:

Lecture 10: Introduction to Inheritance CS427: Programming in C++ Lecture 10.2 11am, 12th March 2012 CS427 Lecture 10: Introduction to Inheritance 1/17

In today s class 1 Inheritance protected 2 Replacing members 3 virtual CS427 Lecture 10: Introduction to Inheritance 2/17

Inheritance The final new idea we ll introduce in C++ is the notion of Inheritance. Basic concept Given a class, construct from it a new class that has the properties of the original class, plus some new ones. The original class is called the base, parent or super class. The new one, is called the derived, child or sub class. The derived class will inherit All the data members of the base class, though it may not be able to access them directly, The ordinary function members, but not constructors, destructors, or assignment operators. CS427 Lecture 10: Introduction to Inheritance 3/17

Inheritance Some programming/syntax aspects: The syntax for defining a derived class class Derived : public Base Here public means that was public to the base class is publicly accessible to the derived class. (This is the most typical case.) class Derived : private Base Here private means everything remains private to the base class, and so can t be access by the derived class. (Not very useful). To added to the public and private specifiers, we ll add protected virtual functions CS427 Lecture 10: Introduction to Inheritance 4/17

Inheritance In our first example, 01InheritExample.cpp link!, we construct a simple base class that has a single data member. We ll then make a derived class. Notice that, although the derived class inherits the base class s private datum, it must use the access method SetX() to change it, and GetX() to evaluate it. CS427 Lecture 10: Introduction to Inheritance 5/17

Inheritance class Point { private : i n t x ; public : Point ( i n t X=0) { x=x ; } ; i n t GetX ( void ) { return ( x ) ; } ; void SetX ( i n t X) { x=x ; } ; } ; class TwoPoint : public Point { private : i n t y ; public : TwoPoint ( i n t X=0, i n t Y=0) { SetX (X ) ; y=y ; } ; i n t GetY ( void ) { return ( y ) ; } ; void SetY ( i n t Y) { y=y ; } ; void PrintXY ( void ) ; } ; void TwoPoint : : PrintXY ( void ) { cout << ( << GetX ( ) <<, << y << ) \ n ; } CS427 Lecture 10: Introduction to Inheritance 6/17

Inheritance protected To date, we designated the accessibility every data member and member function of a class with one of the following access specifiers: private: This specifies that the data/function member can only be accessed from within the class. For data members, this means that the data can be accessed or modified by a function that is a member or friend of the class. Similarly, private functions can be called by another member/friend function of the class. This is the default. OR public: specifies that a data or function member can be accessed from anywhere in your code. Now we add: protected:. These members act the same as private ones, except that they are directly accessible within a derived class. Compare the following, 02Protected.cpp link!, with our earlier example. CS427 Lecture 10: Introduction to Inheritance 7/17

Inheritance class Point { protected : / / this was private in 01Inherit i n t x ; public : Point ( i n t X=0) { x=x ; } ; i n t GetX ( void ) { return ( x ) ; } ; void SetX ( i n t X) { x=x ; } ; } ; protected class TwoPoint : public Point { private : i n t y ; public : / / Don t have to use SetX in the constructor TwoPoint ( i n t X=0, i n t Y=0) { x=x ; y=y ; } ; i n t GetY ( void ) { return ( y ) ; } ; void SetY ( i n t Y) { y=y ; } ; void PrintXY ( void ) ; } ; void TwoPoint : : PrintXY ( void ) { / / Don t have to use GetX cout << ( << x <<, << y << ) \ n ; } CS427 Lecture 10: Introduction to Inheritance 8/17

Replacing members Suppose, for example, that a base class has a method void PrintData(void). It is possible to make a derived class that also contains a (new) method call void PrintData(void). The version of PrintData(void) from the derived class will superceed the one from the base class. However, the original one does not disappear, it can still be called, but you need to use the base class name and scope resolution operator. Note: If the base and derived classes have functions with the same names, but different signatures, then the function is overloaded, not replaced. CS427 Lecture 10: Introduction to Inheritance 9/17

Replacing members 03Replacement.cpp link! class Point { protected : i n t x ; public : Point ( i n t X=0) { x=x ; } ; i n t GetX ( void ) { return ( x ) ; } ; void SetX ( i n t X) { x=x ; } ; void PrintData ( void ) ; } ; void Point : : PrintData ( void ) { cout << ( << x << ) \ n ; } CS427 Lecture 10: Introduction to Inheritance 10/17

Replacing members class TwoPoint : public P o i n t { private : i n t y ; public : TwoPoint ( i n t X=0, i n t Y=0) { x=x ; y=y ; } ; i n t GetY ( void ) { return ( y ) ; } ; void SetY ( i n t Y) { y=y ; } ; void PrintData ( void ) ; } ; void TwoPoint : : PrintData ( void ) { cout << ( << x <<, << y << ) \ n ; } CS427 Lecture 10: Introduction to Inheritance 11/17

Replacing members i n t main ( void ) { Point b ; TwoPoint d ; b. SetX ( 1 2 ) ; cout << b= ; b. PrintData ( ) ; d. SetX ( 1 ) ; d. SetY ( 2 ) ; cout << \nd= ; d. PrintData ( ) ; } cout << C a l l i n g the Point version of P r i n t Data : ; cout << d= ; d. Point : : PrintData ( ) ; return ( 0 ) ; CS427 Lecture 10: Introduction to Inheritance 12/17

virtual There are other times when we need to take care which of two functions that have the same signature and belonging the base and derived class is called. In particular, consider the following code segment: Point d, b ; b = new Point ( 3 ) ; d = new TwoPoint ( 5, 6 ) ; Here b and d are both pointers to the type Point, but created using dynamically memory allocation. In particular, d in initialised as an instance of the TwoPoint class. But, if we call (*d).printdata() (or, equivalently, d->printdata() it is base::printdata() that is called. If we want Derived::PrintData() to be called, then in the definition of Point, we put virtual before the definition of PrintData(). This is shown in 04Virtual.cpp link! CS427 Lecture 10: Introduction to Inheritance 13/17

virtual As a more natural example of when a virtual function might be required, consider the following implementation for items stored in a library. Items in the library all have, at the least, a title and a call-number. The base class item includes private members string title and int CallNumber. There are methods that print the title and call number, and a method printrecord that does both. class item { protected : s t r i n g t i t l e ; i n t CallNumber ; public : item ( s t r i n g t =, i n t n=0) { t i t l e = t ; CallNumber=n ; } ; void s e t T i t l e ( s t r i n g t ) { t i t l e = t ; } ; void setcallnumber ( i n t n ) { CallNumber=n ; } ; void p r i n t T i t l e ( void ) { cout << t i t l e ; } ; void printcall Number ( void ) { cout << CallNumber ; } ; void printrecord ( void ) ; } ; CS427 Lecture 10: Introduction to Inheritance 14/17

virtual The code for the printrecord method is: void item : : printrecord ( void ) { cout << t i t l e : ; p r i n t T i t l e ( ) ; cout << ( c a l l number : ; printcallnumber ( ) ; cout << ) << endl ; } CS427 Lecture 10: Introduction to Inheritance 15/17

virtual Next we have a derived class specifically for books. It has a different form of call number in this case, based on the Dewey Decimal system. Each book s call number is made up of two integers. The first represents the broad category (e.g., 005=Computer programming) and the second the specific subject (e.g., 100=Software Engineering). class Book : public item { private : i n t Category ; i n t Subject ; public : Book ( s t r i n g t =, i n t c =0, i n t s =0) { t i t l e = t ; Category=c ; Subject=s ; } ; void setcallnumber ( i n t c, i n t s ) { Category=c ; Subject=s ; } ; void printcal lnumber ( void ) ; } ; void Book : : printcall Number ( void ) { cout << s e t f i l l ( 0 ) << setw ( 3 ) << Category <<. << Subject ; } CS427 Lecture 10: Introduction to Inheritance 16/17

virtual So if we declare an object of type book, and call its method printrecord. But printrecord was inherited from the base class. That is, book::printrecord is the same as item::printrecord. So it will call item::printcallnumber. That is not what we intended. If we change the definition of item::printcallnumber in the base class to specify that it is virtual, then the binding that is done at compile time is changed, and even though printrecord is inherited from item, we ll find that book::printrecord will call book::printcallnumber. The full example is given in 05Library.cpp link! CS427 Lecture 10: Introduction to Inheritance 17/17