Object Oriented Programming: Inheritance

Documenti analoghi
A.A. 2006/2007 Laurea di Ingegneria Informatica. Fondamenti di C++ Horstmann Capitolo 3: Oggetti Revisione Prof. M. Angelaccio

Finite Model Theory / Descriptive Complexity: bin

C++ Copy Constructor

LA SACRA BIBBIA: OSSIA L'ANTICO E IL NUOVO TESTAMENTO VERSIONE RIVEDUTA BY GIOVANNI LUZZI

Question 1: introduction to computer programming

Linguaggi Corso M-Z - Laurea in Ingegneria Informatica A.A I/O, thread, socket in Java

UNIVERSITÀ DEGLI STUDI DI TORINO

LA SACRA BIBBIA: OSSIA L'ANTICO E IL NUOVO TESTAMENTO VERSIONE RIVEDUTA BY GIOVANNI LUZZI

and Generic Programming

LA SACRA BIBBIA: OSSIA L'ANTICO E IL NUOVO TESTAMENTO VERSIONE RIVEDUTA BY GIOVANNI LUZZI

6in1. brand identity guidelines. Indice. Il logo The logo. Interazione con altri elementi grafici Use with other graphic elements

SVITOL. brand identity guidelines. Indice. Il logo The logo. Interazione con altri elementi grafici Use with other graphic elements

Constant Propagation. A More Complex Semilattice A Nondistributive Framework

LA SACRA BIBBIA: OSSIA L'ANTICO E IL NUOVO TESTAMENTO VERSIONE RIVEDUTA BY GIOVANNI LUZZI

Resources and Tools for Bibliographic Research. Search & Find Using Library Catalogues

Qui u ck c k PE P R E L

Canti Popolari delle Isole Eolie e di Altri Luoghi di Sicilia (Italian Edition)

my WORLD is brand identity guidelines Indice Il logo The logo Interazione con altri elementi grafici Use with other graphic elements

FEROX. brand identity guidelines. Indice. Il logo The logo. Interazione con altri elementi grafici Use with other graphic elements

Lecture 131. Inheritance and Abstraction. Inheritance. Inheritance. Inheritance. Seminars. Seminars. Recall Player class (noughts and crosses)

BDM Motorola MC32xxx User Manual

College Algebra. Logarithms: Denitions and Domains. Dr. Nguyen November 9, Department of Mathematics UK

SYSTEM. brand identity guidelines. Indice. Il logo The logo. Interazione con altri elementi grafici Use with other graphic elements

Database support Prerequisites Architecture Driver features Setup Stored procedures Where to use. Contents

Copyright 2012 Binary System srl Piacenza ITALIA Via Coppalati, 6 P.IVA info@binarysystem.eu

UML: Aggregazione. class A { int s; public void sets(int){ }; public int gets() { }; class B {A ob; public void usea() { }; }

Fiori di campo. Conoscere, riconoscere e osservare tutte le specie di fiori selvatici più note

LA SACRA BIBBIA: OSSIA L'ANTICO E IL NUOVO TESTAMENTO VERSIONE RIVEDUTA BY GIOVANNI LUZZI

Wizzy. brand identity guidelines. Indice. Il logo The logo. Interazione con altri elementi grafici Use with other graphic elements

ECOLE POLYTECHNIQlE FEDERALE DE LAUSANNE

AVERE 30 ANNI E VIVERE CON LA MAMMA BIBLIOTECA BIETTI ITALIAN EDITION

IL GIOVANE HOLDEN FRANNY E ZOOEY NOVE RACCONTI ALZATE LARCHITRAVE CARPENTIERI E SEYMOUR INTRODUZIONE BY JD SALINGER

C++ funzioni Alberto Ferrari. Alberto Ferrari Programmazione di applicazioni SW

Il paradigma OO e le Classi

Stringhe. Prof. Lorenzo Porcelli

fresca foglia brand identity guidelines Indice Il logo The logo Interazione con altri elementi grafici Use with other graphic elements

I CAMBIAMENTI PROTOTESTO-METATESTO, UN MODELLO CON ESEMPI BASATI SULLA TRADUZIONE DELLA BIBBIA (ITALIAN EDITION) BY BRUNO OSIMO

Il Piccolo Principe siamo noi: Adattamento teatrale per la scuola primaria (ABW. Antoine de Saint- Exupery) (Volume 1) (Italian Edition)

ECCO LE ISTRUZIONI PER INSERIRE IL MATERIALE RICHIESTO DAL BANDO TEATRO SENZA FILO CONTEST:

Installazione di DraftSight Enterprise

RESTful Services. Sistemi Informativi Aziendali A.A. 2012/2013

INTERNET & MARKETING INNOVATIVE COMMUNICATION.

Guida ai Promessi Sposi - Riassunto e analisi dei personaggi: Analisi e interpretazione del romanzo di A. Manzoni (Italian Edition)

Capitolo 1. Introduzione. Cay S. Horstmann Concetti di informatica e fondamenti di Java

Constants, t Pointers and Arrays

LA SACRA BIBBIA: OSSIA L'ANTICO E IL NUOVO TESTAMENTO VERSIONE RIVEDUTA BY GIOVANNI LUZZI

intensity brand identity guidelines Indice Il logo The logo Interazione con altri elementi grafici Use with other graphic elements

Succhi di frutta e verdura con la centrifuga (Italian Edition)

Code: GW-IMP-WEB-1. Datalogger web pulses counter. Version 6 inputs with Ethernet. MarCom

Combinazioni serie HL-MHL + MHL. Sono disponibili varie combinazioni tra e riduttori coassiali serie MHL (2 stadio).

REGISTRATION GUIDE TO RESHELL SOFTWARE

Functional programming in F#: Data Structures

An Introduction to Design Patterns

100 consigli per vivere bene (Italian Edition)

Technical Guidelines GON % Italian production. sports car oriented

How to use the verb Piacere.

Famiglia Spirituale nel XXI secolo (La) (Italian Edition)

SRT064 BTH SRT051 BTH SRT052 BTH

Basic English Grammar. long forms short forms long forms short forms

Programmazione 2 - Marco Ronchetti. Fondamenti di Java. Fac.Scienze Università di Trento. Static

UNIVERSITÀ DEGLI STUDI DI TORINO

LA SACRA BIBBIA: OSSIA L'ANTICO E IL NUOVO TESTAMENTO VERSIONE RIVEDUTA BY GIOVANNI LUZZI

String. String: Esercizio 1

Informatica e Laboratorio di Programmazione C++ Object Oriented Programming Alberto Ferrari

Esercitazione di lunedì - TUTTE LE MATRICOLE -

WEB OF SCIENCE. COVERAGE: multidisciplinary TIME RANGE: DOCUMENT TYPES: articles, proceedings papers, books

How to register online for exams (Appelli) Version updated on 23/10/2017

How to register for exam sessions ( Appelli ) Version updated on 17/10/2018

//=============================== class String { // VARIABILI DI ISTANZA char * base; int length;

Java Virtual Machine (JVM)

Morte e Reincarnazione (Italian Edition)

AE RZT QSO RKPT SQZC PRFGE

Arexons. brand identity guidelines. Indice. Segno d identità Distinctive mark. Il logo The logo

Le piccole cose che fanno dimagrire: Tutte le mosse vincenti per perdere peso senza dieta (Italian Edition)

SolidNetwork License Manager

Preghiere potenti e miracolose (Italian Edition)

Esercizi. Cosa fa questa linea di codice: while (*a++=*b++); Costruiamo una funzione che la utilizzi (hint. Usiamo I char..)

Gocce d'anima (Italian Edition)

Gestire La Sicurezza Sul Lavoro

Ereditarietà gerarchica

Analisi dei Requisiti, Progettazione Preliminare ed Esecutiva di Grandi Sistemi Ingegneristici: Casi di Studio

IM-IU v0.1. alternata e continua. pag. 1 / 5

WELCOME UNIPA REGISTRATION:

Viaggio di un naturalista intorno al mondo (Viaggi e Viaggiatori) (Italian Edition)

Quadrature. Emma Perracchione. Corso di Calcolo Numerico per Ingegneria Meccanica - Matr. PARI (Univ. PD)

Marketing non Convenzionale: Viral, Guerrilla e prospettive future (Italian Edition)

A review of some Java basics. Java pass-by-value and List<> references

Una Ricerca Erboristica (Italian Edition)

La programmazione ad oggetti (OOP)

File: /home/peppino/area_lavori/ese enti/oggetti/timbro_classe.cpp Pagina 1 di 5

Downloading and Installing Software Socio TIS

Pimsleur Italian 11. Listen to this conversation

U Corso di italiano, Lezione Quattordici

Introduzione C++ Compilatori Librerie Usare oggetti

29/02/2016. How to Access MyEconLab. Getting started: XL2A-41W1-901Z-4NT2. (included with your textbook) (Scienze e tecn. Ristorazione) 1.

General info on using shopping carts with Ingenico epayments

TO BE - SIMPLE PRESENT

Percorsi: L'Italia Attraverso La Lingua E La Cultura, Books A La Carte Plus MyItalianLab By Francesca Italiano, Irene Marchegiani READ ONLINE

U Corso di italiano, Lezione Dodici

Terra del sole (Italian Edition)

Transcript:

Object Oriented Programming: Inheritance Shahram Rahatlou University of Rome La Sapienza Corso di Programmazione++ http://www.roma1.infn.it/cms/rahatlou/programmazione++/ Roma, 11 June 2007

Today s Lecture Introduction to elements of object oriented programming (OOP) Inheritance Polymorphism Base and Derived Classes Inheritance as a mean to provide common interface 2

What is Inheritance? Powerful way to reuse software without too much re-writing Often several types of object are in fact special cases of a basic object keyboard and files are different types of an input stream screen and file are different types of output stream Resistors ansd capacitors are different types of circuit elements Circle, square, ellipse are different types of shapes In StarCraft, engineers, builders, soldiers are different types of units Inheritance allows to define a base class that provides basic functionalities to derived classes Derived classes can extend the base class by adding new data members and functions 3

Inheritance: Student is a Person // example1.cpp #include <string> #include <iostream> using namespace std; class Person { Person(const string& name) { name_ = name; cout << "Person(" << name << ") called" ~Person() { cout << "~Person() called for " << name_ string name() const { return name_; void print() { cout << "I am a Person. My name is << name_ Student(const string& name, int id) : Person(name) { id_ = id; cout << "Student(" << name << ", " << id << ") called" ~Student() { cout << "~Student() called for name:" << name() << " and id: " << id_ int id() const { return id_; int id_; ; string name_; ; 4

Example of Inheritance in Use // example1.cpp int main() { Person* john = new Person("John"); john->print(); Student* susan = new Student("Susan", 123456); susan->print(); cout << "name: " << susan->name() << " id: " << susan->id() delete john; delete susan; return 0; $./example1 Person(John) called I am a Person. My name is John Person(Susan) called Student(Susan, 123456) called I am a Person. My name is Susan name: Susan id: 123456 ~Person() called for John ~Student() called for name:susan and id: 123456 ~Person() called for Susan 5

Student behaves as Person Person* john = new Person("John"); john->print(); print() and name() are methods of Person Student* susan = new Student("Susan", 123456); susan->print(); cout << "name: " << susan->name() << " id: " << susan->id() delete john; delete susan; id() is a method of Student return 0; Methods of Person can be called with an object of type Student Functionalities implemented for Person available for free No need to re-implement the same code over and over again If a functionality changes, we need to fix it just once! 6

Student is an extension of Person Person* john = new Person("John"); john->print(); int id() const { return id_; int id_; ; Student* susan = new Student("Susan", 123456); susan->print(); cout << "name: " << susan->name() << " id: " << susan->id() id() is a method of Student delete john; delete susan; return 0; Student provides all functionalities of Person and more Student has additional data members and member functions Student is an extension of Person but not limited to be the same 7

Typical Error: Person is not Student! // bad1.cpp int main() { susan is a pointer to Person but initialized by a Student! Person* susan = new Student("Susan", 123456); cout << "name: " << susan->name() cout << "id: " << susan->id() delete susan; OK because a Student is also a Person! elements of polymorphism return 0; $ g++ -o bad1 bad1.cpp bad1.cpp: In function `int main()': bad1.cpp:53: error: 'class Person' has no member named 'id' You can not use methods of Student on a Person object Inheritance is a one-way relation Student knows to be derived from Person Person does not know who could be derived from it You can treat a Student object (*susan) as a Person object 8

Student cannot Access Everything in Person class Person { Person(const string& name) { name_ = name; cout << "Person(" << name << ") called" ~Person() { cout << "~Person() called for " << name_ string name() const { return name_; void print() { cout << "I am a Person. My name is << name_ string name_; ; Student(const string& name, int id) : Person(name) { id_ = id; cout << "Student(" << name << ", " << id << ") called" ~Student() { cout << "~Student() called for name:" << name() << " and id: " << id_ int id() const { return id_; int id_; ; Student can use only public methods and data of Person like anyone else No special access privilege as usual access can be granted not taken 9

public and private in public inheritance Student is derived from Person through public inheritance ; private and protected inheritance are possible but rare and will not be discussed here All public members of Person become public members of Student as well Both data and functions Private members of Person REMAIN private and not accessible directly by Student Access provided only through public methods (getters) You don t need to access source code of a class to inherit from it! Use public inheritance and add new data members and functions 10

protected members protected members become protected members of derived classes Protected is somehow between public and private class Person { Person(const string& name, int age) { name_ = name; age_ = age; cout << "Person(" << name << ", " << age << ") called" ~Person() { cout << "~Person() called for << name_ ; string name() const { return name_; int age() const { return age_; void print() { cout << "I am a Person. name: " << name_ << " age: " << age_ string name_; protected: int age_; Student(const string& name, int age, int id) : Person(name,age) { id_ = id; cout << "Student(" << name << ", " << age << ", " << id << ") called" ~Student() { cout << "~Student() called for name:" << name() << " age: " << age_ << " and id: << id_ int id() const { return id_; int id_; ; protected members can be used by derived classes 11

Constructors of Derived Classes Compiler calls default constructor of base class in constructors of derived class UNLESS you call explicitly a specific constructor Necessary to insure data members of the base class ALWAYS initialized when creating instance of derived class Student(const string& name, int id) { id_ = id; cout << "Student(" << name << ", " << id << ") called" int id_; ; Bad Programming! Constructor of Student does not call constructor of Person Compiler is forced to call Person() to make sure name_ is intialized correctly Bad: we rely on default constructor to do the right thing 12

Common Error with Missing Constructors class Person { Person(const string& name) { name_ = name; cout << "Person(" << name << ") called" ~Person() { cout << "~Person() called for " << name_ Student(const string& name, int id) { id_ = id; cout << "Student(" << name << ", " << id << ") called" int id_; ; string name_; ; // bad2.cpp int main() { Person anna("anna"); Student* susan = new Student("Susan", 123456); susan->print(); delete susan; return 0; $ g++ -o bad2 bad2.cpp bad2.cpp: In constructor `Student::Student(const std::string&, int)': bad2.cpp:32: error: no matching function for call to `Person::Person()' bad2.cpp:7: note: candidates are: Person::Person(const Person&) bad2.cpp:9: note: Person::Person(const std::string&) No default constructor implemented for Person Compiler can use a default one to make anna But gives error dealing with derived classes. You need to provide a default constructor or call one of the implemented constructors 13

Bad Working Example class Person { Person() { // default constructor Person(const string& name) { name_ = name; cout << "Person(" << name << ") called ; Student(const string& name, int id) { id_ = id; cout << "Student(" << name << ", << id << ") called" ; // bad3.cpp int main() { Student* susan = new Student("Susan", 123456); susan->print(); delete susan; $ g++ -o bad3 bad3.cpp $./bad3 Student(Susan, 123456) called I am a Person. My name is ~Student() called for name: and id: 123456 ~Person() called for return 0; Default constructor is called by compiler No name assigned to student by default Code compiles and runs but bad behavior 14

Destructors Similar to constructors Compiler calls the default destructor of base class in destructor of derived class No compilation error if destructor of base class not implemented Default will be used but Extremely important to implement correctly the destructors to avoid memory leaks! 15

Member Functions of Derived Classes Derived classes can also overload functions provided by the base class Same signature but different implementation class Person { void print() { cout << "I am a Person. My name is " << name_ string name_; ; void print() { cout << "I am Student " << name() << " with id " << id_ int id_; ; 16

Overloading Methods from Base Class // example3.cpp #include <string> #include <iostream> using namespace std; int main() { Person* john = new Person("John"); john->print(); // Person::print() Student* susan = new Student("Susan", 123456); susan->print(); // Student::print() susan->person::print(); // Person::print() Compiler calls the correct version of print() for Person and Student We can use Person::print() implementation for a Student object by specifying its scope Remember: a function is uniquely identified by its namespace and class scope Person* p2 = susan; p2->print(); // Person::print() delete john; delete susan; $ g++ -o example3 example3.cpp $./example3 Person(John) called I am a Person. My name is John return 0; Person(Susan) called Student(Susan, 123456) called I am Student Susan with id 123456 I am a Person. My name is Susan I am a Person. My name is Susan ~Person() called for John ~Student() called for name:susan and id: 123456 ~Person() called for Susan 17