Download presentation
Presentation is loading. Please wait.
Published byErnesto Denner Modified over 10 years ago
1
Chapter 14 Inheritance Pages (273-298) 1
2
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
3
Example: #include using namespace std; class Mammal { public: //empty constructor & destructor Mammal() {cout<<" Mammal constructor\n\n"; }; ~Mammal() { cout<<"\n\n Mammal destructor \n";}; //inline setter & getter methods void setAge ( int yrs) { age = yrs; } void setWeight ( int kgs) { weight = kgs; } int getAge() { return age; } int getWeight() { return weight; } //inline method void speak() { cout << "MAMMAL SOUND!\n"; } protected: //variables to store data int age; int weight; }; 3
4
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
5
//destructor method ~Cat() { cout << "Cat destructor: " << name << "\n"; }; //setter & getter methods string getName() { return name; } //inline method void purr() { cout << "PRRR!\n"; } protected: string name; }; 5
6
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
7
Example: #include 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
8
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; else length = 0; if ( w >= 0 ) width = w; else width = 0; } 8
9
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
10
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
11
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
12
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
13
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
14
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
15
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 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(); };
16
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; }
17
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); }
18
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; }
19
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; }
20
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; }
21
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.