In: Computer Science
C++ Programming
Create a C++ program
program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and private as appropriate. The derived classes will override the virtual function(s) in the base class as necessary. The derived classes extend the base class by having at least one member that the base class does not have. The derived classes will access private members of the base class by using the base classes set and get functions.
Please type code and include screenshots of output of the code. Also code must be well commented.
Code
#include<iostream>
#include<string>
using namespace std;
//base class Names shape and having one ptivate member type
string name and one pure virtual function calculate area
class Shape
{
private :
string name;
public:
Shape(string);
void setName(string);
string getNmae();
virtual double calculateArea()=0;
};
Shape::Shape(string name)
{
this->name=name;
}
void Shape::setName(string nm)
{
name=nm;
}
string Shape::getNmae()
{
return name;
}
//class Rectangle derived from the class shape and have two
private member typed double length and width
class Rectangle:public Shape
{
private:
double length,widht;
public:
Rectangle(string,double,double);
double calculateArea();
};
Rectangle::Rectangle(string name,double l,double
w):Shape(name)
{
length=l;
widht=w;
}
double Rectangle::calculateArea()
{
double area=length*widht;
return area;
}
//class Circle derived from the class shape and have one private
member typed double radius
class Circle:public Shape
{
private:
double radius;
public:
Circle(string,double);
double calculateArea();
};
Circle::Circle(string name, double r):Shape(name)
{
radius=r;
}
double Circle::calculateArea()
{
double area=3.14*radius*radius;
return area;
}
//class Triangle derived from the class shape and have two
private member typed double base and height
class Triangle:public Shape
{
private:
double base,height;
public:
Triangle(string,double,double);
double calculateArea();
};
Triangle::Triangle(string name,double b,double
h):Shape(name)
{
base=b;
height=h;
}
double Triangle::calculateArea()
{
double area=0.5*base*height;
return area;
}
int main()
{
Rectangle r1("Recangle",10,20);//careate object of the
Recangle class
Triangle t1("Triangle",10,20);//create object of
Triangle class
Circle c1("Circle",100);//create object of Circle
class
cout<<"Shape :"<<r1.getNmae()<<"
and area :"<<r1.calculateArea()<<endl;
cout<<"\nShape :"<<t1.getNmae()<<"
and area :"<<t1.calculateArea()<<endl;
cout<<"\nShape :"<<c1.getNmae()<<"
and area
:"<<c1.calculateArea()<<endl<<endl;
return 1;
}
ouptut
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.