Hello everyone. I recently finished an assignment for a course and wanted to go a few steps further with it on my own. I'm fairly new to multi-file programming but I know that eventually when programs become more elaborate I'd like to be able to do it well so I'd like to start as early as possible.
I was wondering if I provided my code someone would be able to help with turning this into a multi-file program.
Edit: This isn't me asking for homework help and there is no certain time that I need to have any of this done as my course doesn't require any multi-file programming. I am just asking so that I can get started with this concept early and get ahead.
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
constdouble PI = 3.14;
//Base Class
class Shape3D
{
private:
string name;
protected:
double volume;
public:
Shape3D(string nm, double vlm = 0) {name = nm; volume = vlm;}
};
//Derived CLass
class Box : public Shape3D
{
private: //name is already inhereited so we don't need to include another name
double length;
double width;
double height;
void calcVolume() {volume = length * width * height;}
public:
Box (string n, double l = 1, double w = 1, double h = 1) : Shape3D(n)
{
length = l;
width = w;
height = h;
calcVolume();
}
void printVolume() { cout << "The Box's Volume is " << volume << endl; }
};
//Derived Class
class Sphere : public Shape3D
{
private:
double radius;
void calcVolume(){volume = (4.0/3.0) * (PI) * (radius * radius * radius);}
public:
Sphere (string n, double r = 1) : Shape3D(n)
{
radius = r;
calcVolume();
}
void printVolume() { cout << "The Sphere's Volume is " << volume << endl; }
};
int main()
{
Box B1("Box", 2, 4, 6);
B1.printVolume();
Sphere S1("Sphere", 4);
S1.printVolume();
return 0;
}
That's my code, I've been trying to get it to be a multi-file project for a while now and because of the inheritance I'm having some trouble. Any help would be appreciated, thanks.
You have three classes that could be put in separate header files.
All the methods are defined inline within the class. It you want to keep that, then there will be no separate source files for these. But there's no reason why they should be inline.
#include "Shape3D.hpp"
#include <iostream>
class Box : public Shape3D
{
private: //name is already inhereited so we don't need to include another name
double length;
double width;
double height;
void calcVolume();
public:
Box (string n, double l = 1, double w = 1, double h = 1);
void printVolume();
};
file: Box.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Box::Box(std::string n, double l = 1, double w = 1, double h = 1) :
Shape3D(n)
{
length = l;
width = w;
height = h;
calcVolume();
}
void Box::calcVolume()
{
volume = length * width * height;
}
void Box::printVolume()
{
std::cout << "The Box's Volume is " << volume << endl;
}
... and the same treatment for Sphere.
Then when you compile, you need to make sure you compile all the .cpp files and link the results together,
Still getting errors. Remember, the Box and Sphere are both inheriting the Sphere3D.
Here's what I have. Feel free to take and compile these to better see what my issue is (I'm using VS 2010). Shape3D is the parent class, then we have the Box and the Sphere inheriting the Shape3D class.
//main.cpp
#include <iostream>
#include <string>
#include "Box.h"
#include "Shape3D.h"
#include "Sphere.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;
constdouble PI = 3.14;
int main()
{
Box B1("Box", 2, 4, 6);
B1.printVolume();
Sphere S1("Sphere", 4);
S1.printVolume();
return 0;
}
The main issues I seem to be getting is with the Box.cpp. It says that printVolume() is not a member of the Box class and that volume in the definition is undefined. So, naturally, the printVolume() in main is also not working.
The only problem I'm getting in Sphere.cpp is that it's saying that (in line 14) string nm "This declaration has no storage class or type specifier". And there's also an error underneath the ) on the same line "Expected a declaration". I'm stumped.