I can't seem to get my cylinder class to properly perform its print and volume functions. Here are the instructions for the assignment: Design a class named Shape which is an abstract base class. Shape has two pure virtual functions, printShapeName and print.
Shape contains two other virtual functions, area and volume, each of which has a default implementation that returns a value of zero.
Point class inherits these implementations (both area and volume of a point are zero) from Shape. A Point has x and y coordinate private members.
Class Circle is derived from Point with public inheritance. A Circle has a volume of 0.0, so base-class member function volume is not overridden. A Circle has nonzero area, so the area function is overridden in this class. Write get and set functions to return and to assign a new radius to a Circle.
Class Cylinder is derived from Circle with public inheritance. A Cylinder has area and volume different from those of Circle, so the area and volume functions are both overridden in this class. Write get and set functions to return height and assign new height.
Create one point, one circle, and one Cylinder then print the results.
//
// Shape.hpp
// HW6_VirtualFunctions
//
// Created by Aviv Fedida on 11/25/17.
// Copyright © 2017 Aviv Fedida. All rights reserved.
//
#ifndef Shape_hpp
#define Shape_hpp
#include <stdio.h>
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
protected: // protected members
double width, height, radius, pi, x, y;
public:
void setWidth(double a); // prototype for width setter
void setHeight(double b); // prototype for height setter
void setX(double c);
void setY(double d);
void setRad(double r);
void setPi(double p);
double getWidth() { // get width to return only
return width;
}
double getHeight() { // get height to return only
return height;
}
double getX() {
return x;
}
double getY() {
return y;
}
double getRad() {
return radius;
}
double getPi() {
return pi;
}
// Create public virtual functions
virtual void printShapeName() = 0; // pure virtual for printing shape's name
virtual void print(double a, double b) = 0; // pure virtual print function
virtual double area() { // virtual area function returns default null
return 0;
}
virtual double volume() { // virtual default volume function returns null
return 0;
}
}; // END BASE CLASS -------------------------------------------------------------------------------------
class Point: public Shape {
// x & y coordinates needed for a point
public:
// Prototypes for print & set functions
void printShapeName();
void print(double a, double b);
}; // end first derived class ------------------------------------------------------------------------
class Circle: public Point {
public:
// Protoypes for print functions
void printShapeName();
void print(double r, double p);
// Prototypes for area & volume functions
double area(double r, double p);
}; // end second derived class ----------------------------------------------------------------------------
class Cylinder: public Circle {
public:
double area(double r, double p);
void printShapeName();
double volume(double r, double p, double h);
void print(double r, double p, double h);
}; // end third and final derived class --------------------------------------------------------------------------
// Some definitions outside classes
//-------------------------------------------------- BEGIN -----------------------------------------------------------
// Shape base class setter functions defined
void Shape::setWidth(double a) {
width = a;
}
void Shape::setHeight(double b) {
height = b;
}
void Shape::setX(double c) {
x = c;
}
void Shape::setY(double d) {
y = d;
}
void Shape::setRad(double r) {
radius = r;
}
void Shape::setPi(double p) {
p = 3.1416;
pi = p;
}
void Point::printShapeName() { // Print name of class
cout << "Point " << endl;
}
void Point::print(double a, double b) { // Print values within class
cout << "(" << a << "," << b << ")" << endl;
}
void Circle::printShapeName() {
cout << "Circle " << endl;
}
// Circle area function defined
double Circle::area(double r, double p) {
double area;
area = p*(pow(r,2));
return area;
}
void Circle::print(double r, double p) {
cout << "Area of circle is: " << area(r, p) << endl;
cout << "Volume of circle is: " << volume() << endl;
}
void Cylinder::printShapeName() {
cout << "Cylinder " << endl;
}
double Cylinder::area(double r, double p) {
double area;
area = 2*p*r;
return area;
}
double Cylinder::volume(double r, double p, double h) {
double volume;
volume = p*(pow(r,2))*h;
return volume;
}
void Cylinder::print(double r, double p, double h) {
cout << "Area of cylinder is: " << area(r, p) << endl;
cout << "Volume of cylinder is: " << volume(r, p, h) << endl;
}
#endif /* Shape_hpp */
//
// main.cpp
#include <iostream>
#include "Shape.hpp"
#include "Shape.cpp"
using namespace std;
int main() {
double pi = 3.1416; // Variable for pi
// Instantiate class objects
Point guard;
Circle k;
Cylinder cid;
// Instantiate pointers to class objects
Shape *pptr;
Shape *kptr;
Shape *cptr;
// Assign memory of objects to pointer variables
pptr = &guard;
kptr = &k;
cptr = &cid;
// Call objects via pointers and print members
pptr->printShapeName();
pptr->print(5,6);
cout << '\n';
kptr->printShapeName();
kptr->print(9,pi);
cout << '\n';
cptr->printShapeName();
cptr->getHeight();
cptr->setHeight(8);
cptr->print(5,pi);
return 0;
}
If I try to add a third height argument to my print function for Cylinder class, I get an error. It ends up using my Circle class definition.