OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.

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 Writing and using Classes effectively.
OOP: Inheritance By: Lamiaa Said.
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
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.
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.
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.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
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++
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.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Overview of C++ 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)
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
EEL 3801 Part VII Fundamentals of C and C++ Programming Inheritance.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Object-Oriented Programming (OOP) Lecture No. 24.
Chapter 2 Objects and Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance CMSC 202, Version 4/02.
Object-Oriented Programming
Inheritance and Run time Polymorphism
Inheritance II CMSC 202.
Object-Oriented Programming (OOP) Lecture No. 22
Object-Oriented Programming
Overview of C++ Polymorphism
Inheritance -I.
Object-Oriented PHP (1)
Types of Computer Languages
Object-Oriented Programming (OOP) Lecture No. 27
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.
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4

OOP Spring 2007 – Recitation 42 Inheritance Basis of Object-Oriented Programming

OOP Spring 2007 – Recitation 43 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.

OOP Spring 2007 – Recitation 44 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 Spring 2007 – Recitation 45 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 Spring 2007 – Recitation 46 Syntax class B { public: … protected: … private: … }; class D : public B { public: … private: … }; Derived class name Inheritance type Base class name

OOP Spring 2007 – Recitation 47 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 Spring 2007 – Recitation 48 Student s and Person s class Person { public: void print_name(); protected: char* get_name(); private: char* name; }; class Student : public Person { public: void print_univ(); private: char* univ; };

OOP Spring 2007 – Recitation 49 Their Use int main() { Student s; s.print_name(); s.print_univ(); cout << s.univ; cout << s.name; cout << s.get_name(); } // Prints the name // Prints the university // Can't access

OOP Spring 2007 – Recitation 410 Their Implementation void Student::print_univ() { cout << univ; cout << name; cout << get_name(); } void Person::print_name() { cout << name; } // Student’s member // Can't access // Inherited protected member

OOP Spring 2007 – Recitation 411 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 Spring 2007 – Recitation 412 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 Spring 2007 – Recitation 413 Constructing and Assigning Derived Classes

OOP Spring 2007 – Recitation 414 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 Spring 2007 – Recitation 415 Example Person::Person(char* n) { … } Student::Student(char* name, char* u) : Person(n) { … }

OOP Spring 2007 – Recitation 416 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 Spring 2007 – Recitation 417 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 Spring 2007 – Recitation 418 operator= Example Student& Student::operator=(const Student& rhs) { Person::operator=(rhs);// Calls base's operator= univ = rhs.univ; return *this; }