OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
C++ Classes & Data Abstraction
Inheritance Writing and using Classes effectively.
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Object-Oriented PHP (1)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
OOP Etgar 2008 – Recitation 11 Object Oriented Programming Etgar 2008 Recitation 1.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
CMSC 202 Inheritance.
Chapter 8 More Object Concepts
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
CSC 142 Computer Science II Zhen Jiang West Chester University
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
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.
Object Oriented Programming in C++ Chapter 6 Inheritance.
CS212: Object Oriented Analysis and Design Lecture 14: Reusing classes in C++
Programming in Java CSCI-2220 Object Oriented Programming.
Object-Oriented Programming in C++ More examples of Association.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
Object Oriented Programming
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation 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.
Inheritance ndex.html ndex.htmland “Java.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance CMSC 202, Version 4/02.
Objects as a programming concept
Object-Oriented Programming
Inheritance and Run time Polymorphism
Week 6 Object-Oriented Programming (2): Polymorphism
Object-Oriented Programming (OOP) Lecture No. 22
Inheritance -I.
Object-Oriented PHP (1)
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6

OOP Etgar 2008 – Recitation 62 Inheritance Basis of Object-Oriented Programming

OOP Etgar 2008 – Recitation 63 Where It Comes From? As we try to model the “real world” in C++, we run into the need to model hierarchical relationships between classes – to express commonality. A Student and a Teacher have something in common – they are both Persons. This means that whenever we need a Person, we can use either Student or a Teacher.

OOP Etgar 2008 – Recitation 64 Terminology The mechanism that allows this in C++ is called inheritance. We say that –Student inherits from Person. –Person is a base class and Student is a derived class. –Person is a superclass and Student is a subclass. The public inheritance models the is-a relationship.

OOP Etgar 2008 – Recitation 65 What Does It Mean? Any place a base can be used, a derived can be used. A derived inherits base’s methods and data members (but not all). In practice, a derived class extends the base class, or specializes it. –A Student can do anything a Person can and more. –A Person is more general than a Student. Student is-a kind of Person.

OOP Etgar 2008 – Recitation 66 Syntax class B { public: … protected: … private: … }; class D : public B { public: … private: … };

OOP Etgar 2008 – Recitation 67 Syntax Explained The syntax for saying that D inherits from B is: : public B before class’s opening brace. The public keyword is essential (this is called public inheritance). protected: is a new access level – anything in the protected zone can be accessed by the derived classes only.

OOP Etgar 2008 – Recitation 68 Access Levels The base class is “embedded” in the derived class. Derived uses public and protected members of base like they were his own. Derived contains private members of base, but can’t access them. Clients of derived class can use public members from base and derived like they were all defined public in derived.

OOP Etgar 2008 – Recitation 69 Acess Levels Summary An object of class D “exposes to the world” –Everything declared public in D. –Everything declared public in B. An object of class D can access: –Everything declared public, protected or private in class D. –Everything declared public or protected in class B. An object of class D contains: –Everything declared public, protected or private in class D or in class B.

OOP Etgar 2008 – Recitation 610 Student s and Person s class Person { public: void print_name(); protected: string get_name(); private: string _name; }; class Student : public Person { public: void print_univ(); private: string _univ; };

OOP Etgar 2008 – Recitation 611 Their Use int main() { Student s; s.print_name();// Prints the name s.print_univ();// Prints the university cout << s._univ;// Can't access cout << s._name;// Can't access cout << s._get_name();// Can't access }

OOP Etgar 2008 – Recitation 612 Their Implementation void Student::print_univ() { cout << _univ;// Student's member cout << _name;// Can't access cout << get_name();// Inherited protected member } void Person::print_name() { cout << _name; }

OOP Etgar 2008 – Recitation 613 Class Hierarchy Base doesn’t know about derived classes. Person should not “think about” Student in particular BUT Person should “think about” things common to all derived classes.

OOP Etgar 2008 – Recitation 614 The (Limited) Benefit void introduce(Person p) { cout << "Hello, my name is "; p.print_name(); } int main() { Person p; Student s; introduce(p); introduce(s); return 0; }

OOP Etgar 2008 – Recitation 615 Constructing and Assigning Derived Classes

OOP Etgar 2008 – Recitation 616 Constructors and Inheritance An object of a derived class “includes” members of the base class. When constructing an object all members should be initialized. Therefore derived’s constructors needs to make sure the members inherited from base are initialized correctly. It does it by “calling” base’s constructor.

OOP Etgar 2008 – Recitation 617 Example Person::Person(string iname) : _name(iname) {} Student::Student(string i_name, string i_univ) : Person(i_name), _univ(i_univ) {}

OOP Etgar 2008 – Recitation 618 Construction Order Note the derived’s construction order: –First all base classes are constructed. –Then all data members are constructed. –Then the constructor’s code is executed. The destruction is in reversed order. –Destructor’s code is executed. –Data members are destructed. –Base classes are destructed. If we do not “call” base’s constructor, it’s default constructor will be called.

OOP Etgar 2008 – Recitation 619 Who Should Do It? These “calls” to base’s constructor should be made in all derived’s constructors (including copy constructor). Other derived’s functions may need to “work with” base’s members. For example, operator= must assign to base’s members too. It’s easy by calling appropriate method from base. Compiler generated methods do this by default.

OOP Etgar 2008 – Recitation 620 operator= Example // Redundant operator=. Compiler-generated // would do the same. No need to check // self-assignment Student& Student::operator=(const Student& rhs) { Person::operator=(rhs);// Calls base's operator= _univ = rhs._univ; return *this; }