1 Object-Oriented Programming Using C++ CLASS 11 Honors.

Slides:



Advertisements
Similar presentations
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Advertisements

Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
1 Abstract Data Types Chapter 1. 2 Objectives You will be able to: 1. Say what an abstract data type is. 2. Implement a simple abstract data type in C++
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
C++ Classes & Data Abstraction
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
C++ Inheritance Systems Programming.
Before Introducing Inheritance … Here is another way to re-use what have been developed: This is known as the object composition! A real-world example.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 - Inheritance Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes 19.3Protected.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Review of C++ Programming Part II Sheng-Fang Huang.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
CS342 Data Structure Review of C++: Overriding base- class functions.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 CSC241: Object Oriented Programming Lecture No 13.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 19 - C++ Inheritance Outline 19.1Introduction.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
1 CSC241: Object Oriented Programming Lecture No 16.
Object Oriented Programming in C++ Chapter 6 Inheritance.
1 Chapter 5 - Object-Oriented Programming 1 What is inheritance? Object-oriented systems allow classes to be defined in terms of other classes. Classes.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
LECTURE 4. Composition Definition: A data member of a class is an object of some other class Example: an AlarmClock object needs to know when it is supposed.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 Class 19 Chapter 13 – Creating a class definition.
1 Object-Oriented Programming Using C++ CLASS 17 Honors.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Chapter 19 - C++ Inheritance
CSC241: Object Oriented Programming
CSC241: Object Oriented Programming
Review: Two Programming Paradigms
Introduction to Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
CS212: Object Oriented Analysis and Design
Introduction to Classes
Chapter 19 - Inheritance Outline 19.1 Introduction
Object-Oriented Programming (OOP) Lecture No. 22
CPS120: Introduction to Computer Science
Capitolo 9 - Inheritance
Chapter 19 - Inheritance Outline 19.1 Introduction
Continued from last class
Types of Computer Languages
Inheritance in C++ Inheritance Protected Section
Presentation transcript:

1 Object-Oriented Programming Using C++ CLASS 11 Honors

2 Using inheritance to promote software reusability Objectives

3 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits the protected member variables and public functions of the class it is based on. Represents an “is a” relationship

4 // hardware.cpp // models hardware store inventory using inheritance #include const int LEN = 80; class items { private: char name[LEN]; // item name float price;// item price int quantity;// number in stock

5 public: void getdata( ) //get data from user { cout << “\n Enter item name: ”; cin >> name; cout << “ Enter price (format 12.95): ”; cin >> price; cout << “ Enter quantity in stock: ”; cin >> quantity; } void putdata( )// display data { cout << “\n Name: ” << name; cout << “\n Price: ” << price; cout << “\nQuantity: ” << quantity;} };

6 class pipe: public item // pipe class { private: float length;// length of pipe float size;// size of pipe char type[LEN]; // type of pipe public: void getdata( )// get data from user { item::getdata( ); cout > length; cout > size; cout > type; }

7 void putdata( )// display data { item::putdata( ); cout << “\n Length: ” << length; cout << “\n Size: ” << size; cout << “\n Type: ” << type; } };

8 class light bulbs : public item { private int watts;// wattage of light bulbs public: void getdata( )// get data from user { item::getdata( ); cout > watts; } void putdata( )// display data { item::putdata( ); cout << “\n Wattage: ” << watts; } };

9 class tools : public item // tool class { // (no additional data) };

10 class paint : public item // paint class { private: int size:// size of pints char color[LEN]; // color of paint public: void getdata( )// getdata from user { item::getdata( ); cout > size; cout > color; } void putdata( )// display data { item::putdata( ); cout << “\n Size: ” << size; cout << “\n Color: ” << color; } };

11 void main( ) { pipe p1;// make one item lightbulbs b1;// of each class tools t1; paint pnt1; cout << endl; cout << “\nEnter data for pipe item”; p1.getdata( ); cout << “\nEnter data for light bulb item”; b1.getdata( ); p1.putdata( ); b1.putdata( ) }

12 void main( ) { pipe p1;// make one item lightbulbs b1;// of each class tools t1; paint pnt1; cout << endl; cout << “\nEnter data for pipe item”; p1.getdata( ); cout << “\nEnter data for light bulb item”; b1.getdata( ); p1.putdata( ); b1.putdata( ) }

13 Constructors and Destructors in Inheritance Constructor Order: Constructor of any objects within the Base Class Constructor of the base class Constructor of objects within the Derived class Constructor of the Derived class Destructor is the reverse

14 Points to Remember Software Engineering Observations

15 Software Engineering Observations A derived class cannot directly access private members of its base class.

16 Software Engineering Observations Suppose we create an object of a derived class where both the base class and the derived class contain objects of other classes. When an object of that derived class is created, first the constructors for the base class’ member objects execute, then the base-class constructor executes, then the constructors for the derived class’ member objects execute, then the derived class constructor executes. Destructors are called in the reverse of the order in which their corresponding constructors are called.

17 Software Engineering Observations The order in which member objects are constructed is the order in which those objects are declared within the class definition. The order in which the member initializers are listed does not affect construction.

18 Software Engineering Observations In inheritance, base-class constructors are called in the order in which inheritance is specified in the derived-class definition. The order in which the base-class constructors are specified in the derived-class member initializer list does not affect the order of construction.

19 Software Engineering Observations Creating a derived class does not affect it’s base class’s source code or object code; the integrity of a base class is preserved by inheritance.

20 Software Engineering Observations In an object-oriented system, classes are often closely related. “Factor out” common attributes and behavior and place these in a base class. Then use inheritance to form derived classes.

21 Software Engineering Observations A derived class contains the attributes and behaviors of its base class. A derived class can also contain additional attributes and behaviors. With inheritance, the base class can be compiled independent of the derived class. Only the derived class’s incremental attributes and behaviors need to be compiled to be able to combine these with the base class to form a derived class.

22 Software Engineering Observations Modifications to a base class do not require derived classes to change as long as the public and protected interfaces to the base class remain unchanged. Derived classes may, however, need to be recompiled.

23 Software Engineering Observations Program modifications to a class that is a member of another class do not require the enclosing class to change as long as the public interface to the member class remains unchanged. Note that the composite class may, however, need to be recompiled.

24 Looking at Some Examples

25 class Employee { public: Employee ( const char *, const char *); void print() const; ~ Employee(); private: char * firstName; char * lastName: };

26 Employee : Employee(const char * first, const char *last) { firstName = new char [strlen(first) + 1]; assert (firstName !=0); strcpy ( firstNmae, first); lastName = new char [ strlen(last) + 1]; assert (lastName, last); } Employee:: print() { cout << firstName << ‘ ‘ << lastName; } Employee:: ~Employee() { delete [] first Name; delete[] lastName; }

27 class HourlyWorker: public Employee { public : HourlyWorker (const char*, const char*, double, double); double getPay() const; void print const; private: double wage; double hours; };

28 HourlyWorker:: HourlyWorker( const char * first, const char * last, double initHours, double initWage) : Employee (first, last) { hours = initHours; wage = initWage; } double HourlyWorker:: getPay () const { return wage * hours; } void HourlyWorker:: print() const { Employee::print(); cout << is an hourly worker with pay of << getPay(); }

29 int main() { HourlyWorker h ( “Bob”, “Smith”, 40.0, 10.00); h.print(); return 0; }

30 Object Oriented Design Employee database of salaried managers, permanent hourly employees, temporary hourly employees

31 In-Class Exercise Getting Up to Speed with Inheritance Create an inheritance scheme, a UML diagram, and the interfaces for the following:

32 Getting Up to Speed with Inheritance namenamename home phonehome phonehome phone office phoneoffice phoneoffice phone salary levelwagewage bonus level reports toreports toreports to assistant Salaried Hrly. Perm. Hrly. Temp

33 class employee { public: employee(char *name, char *home_phone, char *office_phone, char *reports_to); void show_employee(void); private: char name[64]; char home_phone[64]; char office_phone[64]; char reports_to[64]; };

34 class salaried : public employee { public: salaried(char *name, char *home_phone, char *office_phone, char *reports_to, float salary, float bonus_level, char *assistant); void show_salaried(void); private: float salary; float bonus_level; char assistant[64]; };

35 class hourly : public employee { public: hourly(char *name, char *home_phone, char *office_phone, char *reports_to, float wage); void show_hourly(void); private; float wage; };

36 Q & A