Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef SHAPE_H
- #define SHAPE_H
- #include <iostream>
- using namespace std;
- class Shape
- {
- public:
- Shape(){};
- //Shape constructor that takes in radius.
- Shape(double _radius) : radius(_radius){
- };
- //virtual function to calculate area of the shape.
- virtual double CalculateArea(){
- return 1;
- };
- //virtual function to calculate volume of the shape.
- virtual double CalculateVolume()
- {
- return 1;
- };
- //virtual function to print out the detail of the shape.
- virtual void print()
- {
- cout << "default shape" << endl;
- };
- double radius;
- static double PI;
- };
- double Shape::PI = 3.14;
- #endif
- #include "Shape.h"
- #include <cmath>
- #include <iostream>
- #include <iomanip>
- using namespace std;
- //Cone class that inherits Shape
- class Cone :public Shape
- {
- public:
- double height;
- Cone(){};
- Cone(double _radius, double _height) : Shape(_radius){
- height = _height;
- };
- ~Cone();
- virtual double CalculateArea();
- virtual double CalculateVolume();
- virtual void print();
- private:
- };
- //calculate area of a cone.
- double Cone::CalculateArea()
- {
- return (PI * radius * (radius + sqrt(radius*radius + height*height)));
- }
- //calculate volume of a cone.
- double Cone::CalculateVolume()
- {
- return (PI * height * (radius * radius) / 3);
- }
- //print details of a cone.
- void Cone::print()
- {
- cout << "-------" << endl;
- cout << "Cone" << endl;
- cout << "Radius: " << radius << endl;
- cout << "Height: " << height << endl;
- cout << "Area: " << fixed << setprecision(3) << CalculateArea() << endl;
- cout << "Volume: " << fixed << setprecision(3) << CalculateVolume() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement