CS125: Programming Fundamentals

Lab No. 12 Inheritance & Polymorphism
1. 2. 3. 4. Inheritance polymorphism Virtual Functions Abstract Base Classes

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

Lab No. 12: inheritance & polymorphism

Shape *ppoly2 = &trgl.getArea() << endl. ppoly3->set_values (4. Tri. } Write Output of the above code.5). cout << "Triangle Total area: " << tri. cout << ppoly1->area() << endl. } Polymorphism In Base Class Add the Area function.7).5). Triangle tri.set_values(3. Shape poly. int main() { Rectangle rect. Rect. ppoly1->set_values (4. As written below. Shape *ppoly3 = &poly. return 0. 12: inheritance & polymorphism . system("pause"). Lab No.set_values(5.6). cout << ppoly3->area() << endl. ppoly2->set_values (4. Line by line.CS125: Programming Fundamentals Access Same class Derived classes Outside classes public Yes Yes Yes protected yes yes no Private Yes No No int main() { Rectangle Rect. Shape *ppoly1 = &rect. virtual int area(){ return 0. return 0. cout << "Rectangle Total area: " << Rect. system("pause").5). Triangle trgl.} Now test it with the following code.getArea() << endl. // Print the area of the object. cout << ppoly2->area() << endl.

CS125: Programming Fundamentals Abstract Base Class class Shape { protected: int width. This type of function is called a pure virtual function. } virtual int getArea()=0. } void set_values(int w. }. ppoly1->set_values (4. return 0.5). height=h. system("pause"). cout<<"Height is : "<<height<<endl. Shape *ppoly1 = &rect. and all classes that contain at least one pure virtual function areabstract base classes. int height. Triangle trgl. Notice how we appended =0 to virtual int area () instead of specifying an implementation for the function. int h) { width=w. Shape *ppoly2 = &trgl. } Lab No.5). 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. 12: inheritance & polymorphism . ppoly2->set_values (4. But a class that cannot instantiate objects is not totally useless. public: void print() { cout<<"Width is : "<<width<<endl. cout << ppoly2->getArea() << endl. cout << ppoly1->getArea() << endl. We can create pointers to it and take advantage of all its polymorphic abilities. Therefore a declaration like: int main() { Rectangle rect.

and then displaying the data with putdata(). Checked By: Date: September 2 . 2013 Lab No. and tape. Create a class publication that stores the title (a string) and price (type float) of a publication. 12: inheritance & polymorphism . and a putdata()function to display its data. which adds a page count (type int).CS125: Programming Fundamentals Lab work Imagine a publishing company that markets both book and audiocassette versions of its works. From this class derive two classes: book. asking the user to fill in data with getdata(). 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. Write a main()program to test the book and tape classes by creating instances of them.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.