Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Python Objects and Classes
Chapter 1 Inheritance University Of Ha’il.
C++ Classes & Data Abstraction
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
Written by: Dr. JJ Shepherd
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Module 10: Simple Inheritance #1 2000/01Scientific Computing in OOCourse code 3C59 Module 10: Simple Inheritance In this module we will cover Inheritance.
4.1 Instance Variables, Constructors, and Methods.
1 Inheritance Chapter 9. 2 What You Will Learn Software reusability (Recycling) Inheriting data members and functions from previously defined classes.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Classes, Interfaces and Packages
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
EEL 3801 Part VII Fundamentals of C and C++ Programming Inheritance.
Object Oriented Programming COP3330 / CGS5409.  Class Templates  Bitwise Operators.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
By Muhammad Waris Zargar
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance Basics Fall 2008
COMP 2710 Software Construction Inheritance
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Understanding Inheritance
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Andy Wang Object Oriented Programming in C++ COP 3330
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Learning Objectives Inheritance Virtual Function.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
Inheritance Dr. Bhargavi Goswami Department of Computer Science
COP 3330 Object-oriented Programming in C++
Java Programming Language
COP 3330 Object-oriented Programming in C++
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
COP 3330 Object-oriented Programming in C++
Presentation transcript:

Object Oriented Programming COP3330 / CGS5409

 Inheritance  Assignment 5

 Many types of classes that we create can have similarities.  Useful to take advantage of the object- oriented programming technique known as Inheritance. To do this, we : ◦ factor out the common features into a single class, usually referred to as a base class. ◦ build derived classes that will share all of the data and functionality found in the base class. ◦ derived class will inherit all of the base class features.

 This base class / derived class relationship referred to as an "is a" relationship  Derived object really is an instance of the base object -- it's usually just a bit more specific (like a subcategory). Examples:  A class called Geometric_Object. From this base class we could derive classes like Circle, Square, and Line.  A class called Sport. From this class we could derive classes like Football, Baseball, and Soccer, because each one is a Sport.  A class called BankAccount. From this we could derive the classes Savings, Checking, Debit (each is a type of account).  A class called Vehicle. From this we could derive classes Car, Train, Bus (each one is a vehicle). Furthermore, we could use the Car class as a base class from which to derive more new classes, such as Ford, Toyota, and Honda.

 To declare a class as a derived class, just add a little bit of syntax to the class declaration block. Here is the format: class derivedClassName : public baseClassName  The word "public" in this format causes derivedClassName to be publicly derived from baseClassName.  The base class would be declared normally somewhere above (or in an included file). This essentially means that the protection levels in the derived class are the same as in the base class. (Note: It is possible to derive with different protection levels -- we will not worry about those here).

 So, the class declarations for some of the above examples might look like this: class Sport {.... }; class Football : public Sport {.... }; class Checking : public Account class Baseball : public Sport class Car : public Vehicle class Honda : public Car

 Notice that in the last two declarations, ◦ Car inherits everything in the Vehicle class, and ◦ Honda inherits everything in the Car class. Because of this, ◦ Honda has also inherited everything from the Vehicle class as well.  Example: classes in a drawing programclasses in a drawing program

 Remember that the protection levels we have are public and private. ◦ public - Any member that is public can be accessed by name from anywhere. ◦ private - Any member that is private can be accessed directly only by the class in which it is declared.  This poses a problem, since we would like derived classes to have access to the members that it inherited from the base class, but we still want protection from outside access. For this reason, there is a third level of protection for member data and functions, called protected. ◦ protected - Any member that is protected can be accessed directly by the class in which it is declared, and by any classes that are derived from that class (but not from anywhere else).  Protection levels in the drawing program exampledrawing program example

 We know that when an object is created, its constructor runs.  However, what happens when a derived object is created? ◦ In this case, the derived object "is-an" instance of the base class as well. ◦ So, when a derived object is created, the constructors from the base and derived classes will run. The order in which they run is important, too - - the base class constructor runs first, then the derived.

Vehicle obj1; Car obj2; Honda obj3;  In the first declaration, only the Vehicle() constructor runs.  In the second declaration, the Vehicle() constructor runs for obj2, followed by the Car() constructor.  In the third declaration, the constructors that run, in order, are: Vehicle(), Car(), Honda()  Note: When an object goes out of scope, the destructors will run in reverse order: ~Honda(), ~Car(), ~Vehicle()  You can verify for yourself in code what order the constructors and destructors run in.

 Since no parameters were specified in the above examples, the default constructors (i.e. no parameters) are used.  What if we have parameters? Remember in the Fraction class, we could declare the following:  Fraction f(3,4); // calls constructor with parameters  So, with a derived class, we might want to declare:  Honda h(2,3,4,"green","Accord"); // wants to send in 5 parameters

 However, the 2 and 3 might be codes intended for a variables like "vehicleCategory" and "idNumber" in the Vehicle class; the 4 and "green" might be intended for variables like "numDoors" and "carColor" in the Car class; and the "Accord" might be intended for a variable "model" in the Honda class. This means that the first two parameters are needed by the Vehicle constructor, the second two are needed by the Car constructor, and the last is needed in the Honda constructor. How do we distribute the parameters to the appropriate places?  To do this with derived classes, use an Initialization List. And initialization list is something that can go along with any function definition. The format is: function prototype : initialization list { function body }

Vehicle::Vehicle(int c, int id) // this parent constructor uses the first two parameters { vehicleCategory = c; idNumber = id; } Car::Car(int c, int id, int nd, char* cc) : Vehicle(c, id) // this function passes the first two parameters up to Vehicle, and uses nd and cc { numDoors = nd; strcpy(carColor,cc); } Honda::Honda(int c, int id, int nd, char* cc, char* mod) : Car(c, id, nd, cc) // This function passes the first four parameters up to car, and uses mod { strcpy(model, mod); }

 Suppose we have the following base class: class Student { public: void GradeReport();... (other member functions and data) };  Let's assume that the "GradeReport" function will print out a grade report for a Student

 Now, take the following as classes derived from Student: class Grad : public Student class Undergrad : public Student  Since these classes are derived from Student, they inherit everything from the Student class. So, we could build the following objects and make the following function calls: Student s; Grad g; Undergrad u; s.GradeReport(); g.GradeReport(); u.GradeReport();

 The Grad and Undergrad classes both inherited the GradeReport function, so they can call it. However, what if grade reports look different for undergrads and grads? Then, these classes need to have their own functions! With inheritance, a derived class can create its own version of a function in the base class, with the exact same prototype, which will override the base class version: class Grad : public Student { public: void GradeReport();... (other stuff) }; class Undergrad : public Student { public: void GradeReport();... (other stuff) };

 So, in the calls listed above, each object calls it's own version of the GradeReport function: s.GradeReport(); // runs Student's version g.GradeReport(); // runs Grad's version u.GradeReport(); // runs Undergrad's version  Now, does this mean that for the grad object g, the parent version is no longer accessible? No! Remember, a derived class inherits everything from its parent class. If, inside the Grad's version of GradeReport, you would like to call upon the parent's version as well (usually done if you want to split the work and let the parent function do as much as possible, based on data stored in the base class), you can.

 Here's an example -- these are outlines of the function definitions: void Student::GradeReport() {... processing done by parent function... } void Grad::GradeReport() { Student::GradeReport(); // explicit call to parent function // other processing specific to Grad's version }  Notice that you can explicitly call the parent's version of the function by specifying the parent class name and the scope resolution operator: className::memberName Examples of function overriding for the drawing program example.

 From this directory from our textbook (Ch. 14 on inheritance):   Several classes that go together in an inheritance hierarchy: ◦ Employee (employee.h and employee.cpp) -- base class for Employee hierarchy ◦ HourlyEmployee (hourlyemployee.h and hourlyemployee.cpp) -- a subclass ◦ SalariedEmployee (salariedemployee.h and salariedemployee.cpp) -- another subclass ◦ cpp -- a sample main program that uses the Employee and subcategory objects  This one is fairly basic -- but it does illustrate things we've seen so far, including the handling of constructors in base and derived classes, as well as some simple function overrides.

 Ch14/ Ch14/  Inheritance features: ◦ Basic use of base and derived classes ◦ use of initialization list in derived class constructors to explicitly call appropriate base class constructor ◦ Derived class functions (like printCheck() ) calling base class member functions ◦ In this example be sure to point out that the derived functions need to call the accessors to get data declared in the parent class -- because that data is private to the base class