Top Banner
1 Object-Oriented Programming Using C++ CLASS 11 Honors
36

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

Jan 02, 2016

Download

Documents

Geoffrey Lyons
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Object-Oriented Programming Using C++ CLASS 11 Honors.

1

Object-Oriented Programming

Using C++

CLASS 11 Honors

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

2

• Using inheritance to promote software reusability

Ob

ject

ives

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

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

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

4

// hardware.cpp// models hardware store inventory using inheritance#include <iostream.h>

const int LEN = 80;

class items{private:

char name[LEN]; // item namefloat price; // item priceint quantity; // number in stock

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

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; }};

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

6

class pipe: public item // pipe class{private:

float length; // length of pipefloat size; // size of pipechar type[LEN]; // type of pipe

public: void getdata( ) // get data from user

{ item::getdata( );cout << “ Enter length: ”; cin >> length;cout << “ Enter size: ”; cin >> size;cout << “ Enter type: “; cin >> type;}

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

7

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

};

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

8

class light bulbs : public item{ private int watts; // wattage of light bulbs public: void getdata( ) // get data from user

{ item::getdata( ); cout << “ Enter wattage: ”; cin >> watts;}

void putdata( ) // display data{item::putdata( );cout << “\n Wattage: ” << watts;}

};

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

9

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

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

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 << “ Enter size(pints): ”; cin >> size; cout << “ Enter color: ”; cin >> color;}

void putdata( ) // display data{ item::putdata( ); cout << “\n Size: ” << size; cout << “\n Color: ” << color; }

};

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

11

void main( ){pipe p1; // make one itemlightbulbs b1; // of each classtools 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( )

}

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

12

void main( ){pipe p1; // make one itemlightbulbs b1; // of each classtools 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( )

}

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

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

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

14

Points to Remember

Software Engineering Observations

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

15

Software Engineering Observations

• A derived class cannot directly access private members of its base class.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

24

Looking at Some Examples

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

25

class Employee{ public:

Employee ( const char *, const char *); void print() const; ~ Employee();

private:

char * firstName;

char * lastName:

};

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

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; }

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

27

class HourlyWorker: public Employee

{ public : HourlyWorker (const char*, const char*, double, double); double getPay() const; void print const; private:

double wage;

double hours;

};

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

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(); }

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

29

int main()

{ HourlyWorker h ( “Bob”, “Smith”, 40.0, 10.00);

h.print();

return 0;

}

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

30

Object Oriented Design

• Employee database of salaried managers, permanent hourly employees, temporary hourly employees

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

31

In-Class ExerciseGetting Up to Speed with

InheritanceCreate an inheritance scheme, a UML diagram , and the interfaces

for the following:

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

32

Getting Up to Speed with Inheritance

name name name

home phone home phone home phone

office phone office phone office phone

salary level wage wage

bonus level

reports to reports to reports to

assistant

Salaried Hrly. Perm. Hrly. Temp

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

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];

};

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

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];

};

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

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;};

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

36

Q & A