Write a program in C++ to create a base class shape and derive two classes named Circle and Square from it. Create and initialize objects from these classes using appropriate constructors.

 #include<iostream>

#include<string.h>

using namespace std;


class Shape

{

public:

double radius;

double side;


//constructor to assign initial values to side and circle;

Shape()

{

side=0;;

radius-=0;

}



void get_data()

{

cout<<"\nEnter side of square to compute area  :";

cin>>side;

cout<<"\nEnter radius of circle to compute area  :";

cin>>radius;

}

virtual void display_area()

{

}

};



class Square: public Shape

{

public:



void display_area()

{

cout<<side;

cout<<"\nArea of Square = "<<(side*side);

}

};



class Circle : public Shape

{

public:



void display_area()

{

cout<<"\nArea of Circle= "<<(3.14*radius*radius);

}

};

int  main()

{


Shape *s;

Square t;

t.get_data();

s=&t;

s->display_area();

Circle r;

r.get_data();

s=&r;

s->display_area();

return 0;

}

Comments

Popular posts from this blog

[ C++ program] Assume that a bank maintains two kinds of accounts for customers, | | one called as savings account and the other as current account. | | The savings account provides compound interest and withdrawal facilities but no cheque book facility. | | The current account provides cheque book facility but no interest. | | Current account holders should also maintain a minimum balance | | and if the balance falls below this level, a service charge is imposed. | | Create a class account that stores customer name, account number and type of account. | | From this derive the classes curacct and savacct to make them more specific to their requirements.

Perform all operation in a singly linked list using C language: