Create a base class Shape. Derive three different classes Circle, Rectangle and Triangle from Shape class. Write a C++ program to calculate area of Circle, Rectangle and Triangle. (Use pure virtual function).
//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;
#include<iostream.h>
#include<conio.h>
class shape
{
public:
virtual void area()=0; // pure virtual function
};
class circle: public shape
{
float r; //r=radius
public:
void area()
{
cout<<"\nEnter radius : ";
cin>>r;
cout<<"\nArea of circle : "<<(2.146*r*r);
}
};
class rectangle: public shape
{
int l,b; // l=length , b=bredth
public:
void area()
{
cout<<"\nEnter length : ";
cin>>l;
cout<<"\nEnter breadth : ";
cin>>b;
cout<<"\nArea of rectangle : "<<l*b;
}
};
class triangle: public shape
{
int h,b;
float a;
public:
void area()
{
cout<<"\nEnter height : ";
cin>>h;
cout<<"\nEnter breadth : ";
cin>>b;
a=0.5*h*b;
cout<<"\nArea of triangle : "<<a;
}
};
int main()
{
circle c; // object created
c.area();
rectangle r; // object created
r.area();
triangle t; // object created
t.area();
getch();
return(0);
}
-
Previous Create a class Clock that contains integer data members as hours, minutes and seconds. Write a C++ program to perform following member functions: void setclock(int, int, int ) to set the initial time of clock object. void showclock() to display the time in hh:min:sec format. Write a function tick( ) which by default increment the value of second by 1 or according to user specified second. The clock uses 24 hours format.
0 Comments