Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 11 More.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 11: More About.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using work from: Starting Out with C++ Early Objects Eighth Edition.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Object-Oriented Programming Chapter Chapter
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Object-Oriented Programming: Inheritance and Polymorphism.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming Concepts
Abstract Data Types Programmer-created data types that specify
7. Inheritance and Polymorphism
Inheritance and Polymorphism
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object Oriented Analysis and Design
Understanding Inheritance
Inheritance Basics Programming with Inheritance
Corresponds with Chapter 7
Inheritance Using work from:
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance, Polymorphism, and Virtual Functions
Computer Programming with JAVA
Java – Inheritance.
Java Programming, Second Edition
Object-Oriented Programming: Inheritance and Polymorphism
COP 3330 Object-oriented Programming in C++
Fundaments of Game Design
Chapter 8: Class Relationships
CIS 199 Final Review.
NAME 436.
Object Oriented Analysis and Design
Final and Abstract Classes
C++ Object Oriented 1.
Presentation transcript:

Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design, structure and reusability of code.

Encapsulation the bundling of an object’s data and procedures into a single unit. all of the object's data is contained and hidden in the object and access to it is restricted to members of the class. Examples A function encapsulates the details of an algorithm A class encapsulates both variables and functions together in a single unit.

Encapsulation C++ supports encapsulation and data hiding with user-defined types called classes. A class combines data and functions/methods into a single unit. The method of hiding details of a class is called abstraction. Classes can contain private, protected and public members.

Encapsulation Although all the items in a class are private by default, programmers can change the access levels when needed. Three levels of access are available in both C++ and C# and an additional two in C# only. They are: Public: All objects can access the data. Protected: Access is limited to members of the same class or descendants. Private: Access is limited to members of the same class.

Encapsulation Advantage of Encapsulation security of the data. Benefits of encapsulation: protection of objects from unwanted access by clients. allows access to a level without revealing the complex details below that level. reduces human errors. simplifies the maintenance of the application makes the application easier to understand.

Encapsulation For the best encapsulation, object data should almost always be restricted to private or protected. If you choose to set the access level to public, make sure you understand the ramifications of the choice.

Inheritance the capability to derive a new class from an existing class. The initial class used as the basis for the derived class is referred to as either the base, parent or superclass. The derived class is referred to as either the derived, child, or subclass. The derived class inherits all the member variables and functions (except constructors and destructors) of its base class.

Inheritance C++ class hierarchy is based upon the principle of increased specialization. The base class carries attributes that are common to all classes and virtual functions that may or may not be overridden. The derived class adds its own additional new data and member functions/methods. A function in the derived class may override a base class function.

Inheritance The derived class inherits the attributes of classes above it in the class hierarchy The specialization can continue over multiple levels

Inheritance Protection attributes: Base class member access specifiers public - Any holder of a pointer to an instance of the class may invoke any public method or modify any public data item. private – methods/data members may be accessed only by methods belonging to the class protected - methods/data members may be access by derived classes but not others

class Child : public Parent { }; - protected inheritance Base Class Access C++ supports three inheritance modes, also called base class access modes: - public inheritance class Child : public Parent { }; - protected inheritance class Child : protected Parent{ }; - private inheritance class Child : private Parent{ };

Base Class Access vs. Member Access Specification Base class access is not the same as member access specification: Base class access: determines access for inherited members Member access specification: determines access for members defined in the class

Member Access Specification Specified using the keywords private, protected, public class MyClass { private: int a; protected: int b; void fun(); public: void fun2(); };

Base Class Access Specification class Child : public Parent { protected: int a; public: Child(); }; base access member access

Base Class Access Specifiers public – object of derived class can be treated as object of base class (not vice-versa) protected – more restrictive than public, but allows derived classes to know some of the details of parents private – prevents objects of derived class from being treated as objects of base class.

Inheritance Base class access specifier Type of inheritance public protected private public in derived class. Can be accessed directly by member functions, friend functions, and nonmember functions. protected in derived class. Can be accessed directly by member functions and friend functions. private in derived class. Can be accessed directly by member functions and friend functions. Can be accessed directly by member functions and friend functions. private in derived class. Can be accessed directly by member functions and friend functions. Hidden in derived class. Can be accessed by member functions and friend functions through public or protected member functions of the base class. Can be accessed by member functions and friend functions through public or protected member functions of the base class. Can be accessed by member functions and friend functions through public or protected member functions of the base class.

appear in derived class Effect of Base Access How base class members appear in derived class Base class members public base class private: x protected: y public: z x inaccessible protected: y public: z protected base class private: x protected: y public: z x inaccessible protected: y protected: z private: x protected: y public: z private base class x inaccessible private: y private: z

Order of Execution When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor When an object of a derived class is destroyed, its destructor is called first, then that of the base class See pr11-20.cpp

Order of Execution int main() { UnderGrad u1; ... return 0; // Student – base class // UnderGrad – derived class // Both have constructors, destructors int main() { UnderGrad u1; ... return 0; }// end main Execute Student constructor, then execute UnderGrad constructor Execute UnderGrad destructor, then execute Student destructor

Passing Arguments to Base Class Constructor Allows selection between multiple base class constructors Specify arguments to base constructor on derived constructor heading Can also be done with inline constructors Must be done if base class has no default constructor

Passing Arguments to Base Class Constructor class Parent { public: Parent(int,int); private: int x, y; }; class Child : public Parent { Child(int a): Parent(a,a*a){ z = a; } int z; See inheritance2.h, inheritance2.cpp, pr11-21App.cpp

Overriding Base Class Functions Overriding function: function in a derived class that has the same name and parameter list as a function in the base class Typically used to replace a function in base class with different actions in derived class Not the same as overloading – with overloading, the parameter lists must be different See inheritance3.h, inheritance3.cpp, pr11-21.cpp

Access to Overridden Function When a function is overridden, all objects of derived class use the overriding function. If necessary to access the overridden version of the function, it can be done using the scope resolution operator with the name of the base class and the name of the function: Student::getName(); See inheritance4.h, inheritance4.cpp, and Pr11-22App.cpp

Type Compatibility in Inheritance Hierarchies Classes in a program may be part of an inheritance hierarchy Classes lower in the hierarchy are special cases of those above Vehicle Car Truck 18-Wheeler

Type Compatibility in Inheritance A pointer to a derived class can be assigned to a pointer to a base class. Another way to say this is: A base class pointer can point to derived class objects Vehicle *vehPtr = new Car;

Type Compatibility in Inheritance Assigning a base class pointer to a derived class pointer requires a cast Vehicle *carPtr = new Car; Car *carPtr; carPtr = static_cast<Car *>(vehPtr); The base class pointer must already point to a derived class object for this to work See inheritance4.h and pr15-01.cpp

Using Type Casts with Base Class Pointers C++ uses the declared type of a pointer to determine access to the members of the pointed-to object If an object of a derived class is pointed to by a base class pointer, all members of the derived class may not be accessible Type cast the base class pointer to the derived class (via static_cast) in order to access members that are specific to the derived class