4
CS125: Programming Fundamentals Lab No. 12: inheritance & polymorphism Lab No. 12 Inheritance & Polymorphism Objective 1. Inheritance 2. polymorphism 3. Virtual Functions 4. Abstract Base Classes Inheritance A key feature of C++ classes is inheritance. Inheritance allows to create classes which are derived from other classes, so that they automatically include some of its "parent's" members, plus its own. Base Class class Shape { protected: int width; int height; public: void print() { cout<<"Width is : "<<width<<endl; cout<<"Height is : "<<height<<endl; } void set_values(int w, int h) { width=w; height=h; } virtual int area(){ return 0;} }; Derived classes class Rectangle: public Shape { public: int getArea() { print(); return (width * height); } }; class Triangle: public Shape { public: int getArea() { print(); return ((width * height)/2); } };

CS125 - Lab 12 - Inheritance

Embed Size (px)

DESCRIPTION

Lab 12 - Inheritance

Citation preview

Page 1: CS125 - Lab 12 - Inheritance

CS125: Programming Fundamentals

Lab No. 12: inheritance & polymorphism

Lab No. 12 Inheritance & Polymorphism

Objective 1. Inheritance

2. polymorphism

3. Virtual Functions

4. Abstract Base Classes

Inheritance A key feature of C++ classes is inheritance. Inheritance allows to create classes which are derived

from other classes, so that they automatically include some of its "parent's" members, plus its

own.

Base Class class Shape { protected: int width; int height; public: void print() { cout<<"Width is : "<<width<<endl; cout<<"Height is : "<<height<<endl; } void set_values(int w, int h) { width=w; height=h; } virtual int area(){ return 0;} };

Derived classes

class Rectangle: public Shape { public: int getArea() { print(); return (width * height); } };

class Triangle: public Shape { public: int getArea() { print(); return ((width * height)/2); } };

Page 2: CS125 - Lab 12 - Inheritance

CS125: Programming Fundamentals

Lab No. 12: inheritance & polymorphism

Access public protected Private

Same class Yes yes Yes

Derived classes Yes yes No

Outside classes Yes no No

int main() { Rectangle Rect; Triangle tri; Rect.set_values(5,7); Tri.set_values(3,6); // Print the area of the object. cout << "Rectangle Total area: " << Rect.getArea() << endl; cout << "Triangle Total area: " << tri.getArea() << endl; system("pause"); return 0; }

Polymorphism

In Base Class Add the Area function. As written below.

virtual int area(){ return 0;}

Now test it with the following code.

int main() { Rectangle rect; Triangle trgl; Shape poly; Shape *ppoly1 = &rect; Shape *ppoly2 = &trgl; Shape *ppoly3 = &poly; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly3->set_values (4,5); cout << ppoly1->area() << endl; cout << ppoly2->area() << endl; cout << ppoly3->area() << endl; system("pause"); return 0; }

Write Output of the above code. Line by line.

Page 3: CS125 - Lab 12 - Inheritance

CS125: Programming Fundamentals

Lab No. 12: inheritance & polymorphism

Abstract Base Class class Shape { protected: int width; int height; public: void print() { cout<<"Width is : "<<width<<endl; cout<<"Height is : "<<height<<endl; } void set_values(int w, int h) { width=w; height=h; } virtual int getArea()=0; };

Notice how we appended =0 to virtual int area () instead of specifying an implementation for the

function. This type of function is called a pure virtual function, and all classes that contain at least

one pure virtual function areabstract base classes.

The main difference between an abstract base class and a regular polymorphic class is that because

in abstract base classes at least one of its members lacks implementation we cannot create instances

(objects) of it.

But a class that cannot instantiate objects is not totally useless. We can create pointers to it and take

advantage of all its polymorphic abilities. Therefore a declaration like:

int main() { Rectangle rect; Triangle trgl; Shape *ppoly1 = &rect; Shape *ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << ppoly1->getArea() << endl; cout << ppoly2->getArea() << endl; system("pause"); return 0; }

Page 4: CS125 - Lab 12 - Inheritance

CS125: Programming Fundamentals

Lab No. 12: inheritance & polymorphism

Lab work Imagine a publishing company that markets both book and audiocassette versions of its works.

Create a class publication that stores the title (a string) and price (type float) of a publication. From

this class derive two classes: book, which adds a page count (type int); and tape, which adds a

playing time in minutes (type float). Each of these three classes should have a getdata()function to

get its data from the user at the keyboard, and a putdata()function to display its data.

Write a main()program to test the book and tape classes by creating instances of them, asking the

user to fill in data with getdata(), and then displaying the data with putdata().

Checked By: Date: September 2 , 2013