So, I am the first to admit, I do not know how to word this that it may make sense to a seasoned C++ programmer. But I will do my best. I am trying to make a program to calculate the area of 3 shapes, each with their own class file. I have to make a virtual function getArea() which I've figured out. But I also have to include shapeID(), shapeType(), and unitOfMeasure() in the shape class to call in the individual shapes classes. With Shape.h and Shape.cpp being the overarching class file.
So here's my question. I want to make a function, in Shape.h, that is defined in the individual shape class files. Where shapeID() becomes a numeric value that I can call as menu options in main. And shapeType() is applied as what kind of shape it is in the menu, and subsequent output. The unitOfMeasure() can mimic shapeType() so I wouldn't need that if I could get pointed in the right direction on setting up those two, shapeID() and shapeType().
Just for an example, here is the files for what I currently have, and what I'm hoping to do with main.
Any help would be greatly appreciated.
Shape.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#ifndef SHAPE_H
#define SHAPE_H
class Shape
{
public:
virtual double getArea() = 0;
protected:
private:
};
#endif // SHAPE_H
|
Triangle.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "Shape.h"
class Triangle : public Shape
{
public:
Triangle(double base =0, double height =0);
double getArea(){return base * .5 * height;};
void shapeID(int x){x=1;};
void shapeType(char Triangle);
double getBase() const;
double getHeight() const;
void setBase(double base);
void setHeight(double height);
private:
double base, height;
};
#endif // TRIANGLE_H
|
Triangle.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include "Triangle.h"
#include <iostream>
using namespace std;
Triangle::Triangle(double b, double h)
{
base = b;
height = h;
}
double Triangle::getBase() const{
return base;
}
double Triangle::getHeight() const{
return height;
}
void Triangle::setBase(double b){
base = b;
}
void Triangle::setHeight(double h){
height = h;
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include "Shape.h"
#include "Triangle.h"
#include <iostream>
using namespace std;
int main()
{
Triangle t;
double base, height, radius, length;
int menu;
do {
cout << endl << "Welcome to the area calculator, please select a shape to work with below, or press 0 to exit" << endl;
cout << shapeID(menu) << ": " << shapeType() << endl;
}
k cout << "Enter the base of the Triangle: ";
cin >> base;
t.setBase(base);
cout << "Enter the height of the Triangle: ";
cin >> height;
t.setHeight(height);
cout << "the area of the triangle is: " << t.getArea() << endl;
return 0;
}
|
In main.cpp, I have the code working up to the Do loop I'm starting to write. I will be using a switch for the menu and would like shapeID() to populate that menu, with shapeType() to display what type of shape it is.
I hope this makes sense, and someone can assist me.
Thank you,
Sam