Run time and compile time polymorphism in oop(object oriented programming)

 polymorphism

Task 1

Create parent class Polygon with protected parameters width and height and function printarea() and a virtual function area(). Create three sub classes Rectangle , Square and Triangle.In main() create 3 pointers of Polygon and assign Rectangle , Square and Triangle to it. Call printarea function with the pointers.

#include<iostream>
using namespace std;
class Polygon{
protected:
            int width, height;
public:
            void printArea()
            {
                        cout << "This is the area of Polygon" << endl;
            }
            virtual void area()
            {
                        cout << "This is virtual area" << endl;
            }
};
class Rectangle :public Polygon{
public:
            void area()
            {
                        cout << "This is the area of Rectangle" << endl;
            }
            void printArea()
            {
                        cout << "Print printarea of Rectangle" << endl;
            }
};
class Square :public Polygon{
public:
            void area()
            {
                        cout << "This is the area of Square" << endl;
            }
            void printArea()
            {
                        cout << "This is the printarea of square" << endl;
            }
};
class Triangle :public Polygon{
public:
            void area()
            {
                        cout << "This is the area of Triangle" << endl;
            }
            void printArea()
            {
                        cout << "This is the print area of Triangle" << endl;
            }
};
int main()
{
            Polygon *p, *p1, *p2; Rectangle r1; Square s1; Triangle t1;
            p = &r1; p->area(); r1.printArea(); p->printArea(); r1.area(); cout << endl;
            p1 = &s1; p1->area(); s1.printArea();p1->printArea(); s1.area(); cout << endl;
            p2 = &t1; p2->area(); t1.printArea();p2->printArea(); t1.area(); cout << endl;
            getchar();
            getchar();
            return 0;
}      

          

       
 

 

Task 2:

Implement Task 1 using abstract class.

#include<iostream>
using namespace std;
class Polygon{
protected:
            int width, height;
public:
            virtual void area() = 0;
            void printArea()
            {
                        cout << "This is the area of Polygon" << endl;
            }
};
class Rectangle :public Polygon{
public:
            void area()
            {
                        cout << "This is the area of Rectangle" << endl;
            }
            void printArea()
            {
                        cout << "Print printarea of Rectangle" << endl;
            }
};
class Square :public Polygon{
public:
            void area()
            {
                        cout << "This is the area of Square" << endl;
            }
            void printArea()
            {
                        cout << "This is the printarea of square" << endl;
            }
};
class Triangle :public Polygon{
public:
            void area()
            {
                        cout << "This is the area of Triangle" << endl;
            }
            void printArea()
            {
                        cout << "This is the print area of Triangle" << endl;
            }
};
int main()
{
            Polygon *p, *p1, *p2; Rectangle r1; Square s1; Triangle t1;
            p = &r1; p->area(); r1.printArea(); p->printArea(); r1.area(); cout << endl;
            p1 = &s1; p1->area(); s1.printArea();p1->printArea(); s1.area(); cout << endl;
            p2 = &t1; p2->area(); t1.printArea();p2->printArea(); t1.area(); cout << endl;
            getchar();
            getchar();
            return 0;
}       

          

       
 

Task 3:

Imagine some publishing company that markets both book and audiocassette versions of its works. Create a class called 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 the three classes should have a getdata( ) function to get its data from the user at the keyboard, and a putdata( ) function to display the data.
Write a main( ) program that creates an array of pointers to publication. In a loop, ask the user for data about a particular book or tape, and use new to create an object of type book or tape to hold the data. Put the pointer to the object in the array. When the user has finished entering the data for all books and tapes, display the resulting data for all the books and tapes entered, using a for loop and a single statement such aspubarr[j]->putdata( );to display the data from each object in the array.

#include<iostream>
#include<string>
using namespace std;
class Publication{
            string title; float price;
public:
            Publication()
            {
                        title = "0"; price = 0.0;
            }
            void getdata()
            {
                        cin.ignore();
                        cout << "Enter the title of the book:";
                        getline(cin, title);
                        cout << "Enter the price of the book:";
                        cin >> price;
            }
            void putdata(){
                        cout << "Name of the book is:" << title << endl;
                        cout << "Price of the book is:" << price << endl;
            }
            ~Publication()
            {
                        cout << "Destructor is calling" << endl;
            }
};
class Book :public Publication{
            int count;
public:
            void getdata()
            {
                        cout << "Enter the page of the book:";
                        cin >> count;
            }
            void putdata()
            {
                        cout << "Pages of the book are:" << count << endl;
            }
};
class tape :public Publication{
     float time;
public:
            void getdata()
            {
                        cout << "Enter the minuts of tape:";
                        cin >> time;
            }
            void putdata()
            {
                        cout << "PLying time of tape is:" << time << endl;
            }
};
int main()
{
            Book b; tape t; b.getdata(); t.getdata();
            Publication *p[2]; p[0] = new Book; p[1] = new tape;
            for (int i = 0; i < 2; i++)
            {
                        p[i]->getdata();
            }
            for (int i = 0; i < 2; i++)
            {
                        p[i]->putdata();
                        cout << endl;
            }
            b.putdata(); t.putdata();
            getchar();
            getchar();
            return 0;
}      

          

       
 

Comments

Post a Comment

Popular posts from this blog

Multiple inheritance,friend function and multiple file in oop(object oriented programming)

Concepts of OOP (object oriented programming)

Concepts in c++........