In C++, use virtual functions and inheriance to create the following classes: sh
ID: 3679562 • Letter: I
Question
In C++, use virtual functions and inheriance to create the following classes: shape, cirlce, cylinder, polygon, triangle, triangular pyramid, square. Inheritence is as followes:
shape->circle->cyllinder, shape->polygon->triangle->triangular_pyramid, shape->polygon->square.
The virtual functions are void area(), void perimeter(), and void volume(). You do not need to write the functions, only create the classes using inheritance. *Note: Shape and Polygon are abstract classes and inheritence is public.
Explanation / Answer
Here is the code as per your requirements.
As per your requirements I have given only declaration.
Answer:
abstract class shape // abstact shape class definition
{
// you can declare member varibale here .
public:
abstact void area(); // abstract area function. when it is abstract you should not define the function.
abstract void perimeter(); // only declaration of abstract functions
abstract void volume();
};
class circle: public shape // defining class circle
{
void area()
{
function definition here.
}
void perimetr()
{
function definition here.
}
void volume()
{
function definition here.
}
};
class cylinder: public shape, circle // defining class cylinder
{
void area()
{
function definition here.
}
void perimetr()
{
function definition here.
}
void volume()
{
function definition here.
}
};
abstract class plygon: public shape // abstact polygon class definition
{
void area()
{
function definition here.
}
void perimetr()
{
function definition here.
}
void volume()
{
function definition here.
}
};
class triangle: public shape, polygon // defining class triangle
{
void area()
{
function definition here.
}
void perimetr()
{
function definition here.
}
void volume()
{
function definition here.
}
};
class triangular_pyramid: public shape, polygon, triangle // defining class triangular_pyramid
{
void area()
{
function definition here.
}
void perimetr()
{
function definition here.
}
void volume()
{
function definition here.
}
};
class square: public shape, polygon
{
void area()
{
function definition here.
}
void perimetr()
{
function definition here.
}
void volume()
{
function definition here.
}
};