chapter 14 inheritance pages (273-298) 1. inheritance: ☼ inheritance and composition are...

21
Chapter 14 Inheritance Pages (273-298) 1

Upload: ernesto-denner

Post on 02-Apr-2015

226 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

Chapter 14

Inheritance

Pages (273-298)

1

Page 2: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

Inheritance:

☼ Inheritance and composition are meaningful ways to relate two or more classes.

☼ Inheritance lets us create new classes from existing classes. The new classes that we create from the existing classes are called the derived classes; the existing classes are called the base classes.

☼ The derived classes inherit the properties of the base classes.

☼ Each derived class, in turn, becomes a base class for a future derived class.

☼ A derived class can redefine the member functions of a base class, but this redefinition applies only to the objects of the derived class.

☼ A call to a base class’s constructor (with parameters) is specified in the heading of the definition of the derived class’s constructor.

☼ if in the heading of the definition of a derived class’s constructor, no call to derived class’s object declaration and initialization the default constructor (if any) of the base class executes.

☼ When initializing the object of a derived class, the constructor of the base class is executed first. 2

Page 3: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

Example:#include <iostream>#include <string>using namespace std;class Mammal{ public:

//empty constructor & destructorMammal() {cout<<" Mammal constructor\n\n"; };~Mammal() { cout<<"\n\n Mammal destructor \n";};

//inline setter & getter methodsvoid setAge ( int yrs) { age = yrs; }void setWeight ( int kgs) { weight = kgs; }int getAge() { return age; }int getWeight() { return weight; }

//inline methodvoid speak() { cout << "MAMMAL SOUND!\n"; }

protected://variables to store data

int age;int weight;

};3

Page 4: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

class Cat : public Mammal{ public:

Cat(string initName) //constructor methods {

name = initName; cout << "Cat constructor - setting name\n";

};

Cat(string initName, int initAge) {

name = initName; age = initAge; cout << "Cat constructor - setting name & age\n";

};

Cat(string initName, int initAge, int initWeight) {

name = initName; age = initAge; weight = initWeight; cout << "Cat constructor - setting name, age & weight\n";

}; 4

Page 5: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

//destructor method~Cat() { cout << "Cat destructor: " << name << "\n"; };

//setter & getter methodsstring getName() { return name; }

//inline methodvoid purr() { cout << "PRRR!\n"; }

protected: string name;

};

5

Page 6: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

int main(){ Cat MyCat("Tiddles", 2, 3); cout << "My cat\'s name is " << MyCat.getName(); cout << "\nHe\'s " << MyCat.getAge() << " years old"; cout << " and weighs " << MyCat.getWeight() << " kg\n";

Cat HisCat("Felix", 5); cout << "My cat\'s name is " << HisCat.getName(); cout << " and he\'s " << HisCat.getAge(); cout << " years old\n";

Cat HerCat("Whiskers"); cout << "My cat\'s name is " << HerCat.getName(); cout << endl; HerCat.purr(); cout << endl; return 0;} 6

Page 7: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

Example:

#include <iostream>using namespace std;

class rectangleType{protected:

double length;double width;

public:rectangleType();rectangleType( double l, double w);~rectangleType();void setDimension ( double l, double w);double getLength();double getWidth();double area();double perimeter();void print();

};7

Page 8: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

rectangleType::rectangleType(){

length = 0;width = 0;

}rectangleType::rectangleType( double l, double w){

setDimension( l , w);}rectangleType::~rectangleType(){}void rectangleType::setDimension( double l, double w){

if ( l >= 0 )length = l;

elselength = 0;

if ( w >= 0 )width = w;

elsewidth = 0;

}8

Page 9: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

double rectangleType::getLength(){

return length;}

double rectangleType::getWidth(){

return width;}

double rectangleType::area(){

return length * width;}

double rectangleType::perimeter(){

return 2 * ( length + width );}void rectangleType::print(){

cout << " Length = " << length << " ; Width = " << width;} 9

Page 10: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

class boxType: public rectangleType{private:

double height;public:

boxType();boxType( double l, double w, double h);~boxType();void setDimension ( double l, double w, double h);double getHeight();double area();double volume();void print();

};

Redefining (Overriding) member function of the base class

10

Page 11: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

boxType::boxType(){

rectangleType();height = 0 ;

}

boxType::boxType( double l, double w, double h){

setDimension( l, w, h);}

boxType::~boxType(){}

void boxType::setDimension( double l, double w, double h){

rectangleType::setDimension( l , w );if ( h >= 0)

height = h;else

height = 0;}

11

Page 12: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

double boxType::getHeight(){

return height;}

double boxType::area(){

return 2 * ( length * width + length * height + width * height );}

double boxType::volume(){

return rectangleType::area() * height;}

void boxType::print(){

rectangleType::print();cout << " ; Height = " << height;

}

12

Page 13: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

int main(){

rectangleType myRectangle1;rectangleType myRectangle2(8, 6);

boxType myBox1;boxType myBox2(10, 7, 3);

cout << "\n myRectangle1: ";myRectangle1.print();cout << endl;cout << " Area of myRectangle1: " << myRectangle1.area() << endl;

cout << "\n myRectangle2: ";myRectangle2.print();cout << endl;cout << " Area of myRectangle2: " << myRectangle2.area() << endl;

13

Page 14: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

cout << "\n myBox1: ";myBox1.print();cout << endl;cout << " Surface Area of myBox1: "

<< myBox1.area() << endl;cout << " Volume of myBox1: " << myBox1.volume() << endl;

cout << "\n myBox2: ";myBox2.print();cout << endl;cout << " Surface Area of myBox2: "

<< myBox2.area() << endl;cout << " Volume of myBox2: " << myBox2.volume() << endl;

return 0;}

14

Page 15: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

15

Composition:Composition is another way to relate two classes. In composition, one or more members of a class are objects of another class type. In composition, a call to the constructor of the member objects is specified in the heading of the definition of the class’s constructore.

Example:#include<iostream>#include<string>using namespace std;//......................... the class person ....class Person{

private:string firstName;string lastName;

public:Person(string first="", string last="");void setName(string first, string last);string getFirstName();string getLastName();void print();

};

Page 16: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

16

Person::Person(string first, string last){

setName(first,last);}void Person::setName(string first, string last){

firstName=first;lastName=last;

}string Person::getFirstName(){

return firstName;}

string Person::getLastName(){

return lastName;}

void Person::print(){

cout<< firstName << " " << lastName;}

Page 17: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

17

//.................. the class date .....class Date{

private:int dDay;int dMonth;int dYear;

public:Date( int day = 1, int month = 1, int year = 1900);void setDate( int day, int month, int year);int getDay();int getMonth();int getYear();void printDate();

};Date::Date( int day, int month, int year){

setDate(day, month, year);}

Page 18: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

18

void Date::setDate( int day, int month, int year){

dDay=day;dMonth=month;dYear=year;

}int Date::getDay(){

return dDay;}int Date::getMonth(){

return dMonth;}int Date::getYear(){

return dYear;}void Date::printDate(){

cout<< dDay << "-" << dMonth << "-" << dYear;}

Page 19: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

19

//.................. the class personal information ....class personalInfo{private:

Person name;Date birthday;int personID;

public:personalInfo(string first="", string last="", int day = 1, int month = 1, int year = 1, int ID = 0);void setPersonalInfo(string first, string last, int day, int month, int year,int ID);void printPersonalInfo();};personalInfo::personalInfo(string first, string last, int day, int month, int year, int ID): name(first, last), birthday(day, month, year){

personID=ID;}

Page 20: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

20

void personalInfo::setPersonalInfo(string first, string last, int day, int month, int year, int ID){

name.setName(first,last);birthday.setDate(day,month,year);personID = ID;

}void personalInfo::printPersonalInfo(){

name.print();cout<< "'s date of birth is ";birthday.printDate();cout<< endl;cout << "and personal ID is " << personID;

}

//...................... the main function ....int main(){

personalInfo p1("Abeer","Ahmad",3,11,1989,123456789);p1.printPersonalInfo();cout<< endl;return 0;

}

Page 21: Chapter 14 Inheritance Pages (273-298) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance

Isolating class declarations

The previous examples in this presentation have introduced class declaration in the source code file for simplicity , in reality this is not good. Most programmers place the class declaration in a header file.

The header file can be used in the main program file by adding an #include compiler directive. This is similar to including a standard C++ class but the header file name, including the file extension, must be enclosed inside double quotes instead of angled brackets.

21