Turning my project into a multi-file project

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.

Thanks.
Last edited on
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

const double 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.
Last edited on
The first step is to create a new header (.h) and implementation (.cpp) file for each class. For example, Shape3D.h:
1
2
3
4
5
6
7
8
9
10
11
class Shape3D
{
private:
	string name;

protected:
	double volume;
	
public:
	Shape3D(string nm, double vlm = 0);
};


And Shape3D.cpp:
1
2
3
4
5
Shape3D::Shape3D(string nm, double vlm = 0)
{
  name = nm;
  volume = vlm;
}


After that, you can use #include to link these files. For example, the header for class Box can use:
#include "Shape3D.h"

Finally, the main function should be in a separate CPP and include headers for only the top level classes (Box and Sphere).
Last edited on
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.

So, you have file: Shape3D.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>

class Shape3D
{
private:
	std::string name;

protected:
	double volume;
	
public:
	Shape3D(std::string nm, double vlm = 0);
};


file: Shape3D.cpp:
1
2
3
4
5
6
7
#include "Shape3D.hpp"

Shape3D::Shape3D(std::string nm, double vlm)
{
	name = nm;
	volume = vlm;
}


file: Box.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Shape3D.h

#ifndef SHAPE3D_H
#define SHAPE3D_H
#include <string>

class Shape3D
{
 private:
	 std::string name;

protected:
	double volume;

public:
	Shape3D(std::string nm, double vlm = 0);

};

#endif 


1
2
3
4
5
6
7
8
9
10
11
//Shape3D.cpp

#include "Shape3D.h"

//place your member function definitions below

Shape3D::Shape3D(std::string nm, double vlm = 0)
{
	name = nm;
	volume = vlm;
}


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
//Box.h

#ifndef BOX_H
#define BOX_H

#include "Shape3D.h"
#include <iostream>

class Box : public Shape3D
{
 private:
	 double length;
	 double height;
	 double width;

	 void calcVolume();

 public:
	 Box (std::string nm, double l = 1, double w = 1, double h = 1) : Shape3D

	 void printVolume();

};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Box.cpp
#include "Box.h"
#include <iostream>


//place your member function definitions below
Box::Box(std::string nm, double l = 1, double w = 1, double h = 1) : Shape3D(nm)
{
	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 << std::endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Sphere.h

#ifndef SPHERE_H
#define SPHERE_H

#include "Shape3D.h"
#include <string>

class Sphere : public Shape3D
{
 private:
	 double radius;
	 void calcVolume();

 public:

	 Sphere (std::string nm, double r = 1) : Shape3D(nm)
	 void printVolume();
	 

};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Sphere.cpp

#include "Sphere.h"
#include <iostream>
#include <string>
const double PI = 3.14;

//place your member function definitions below
void Sphere::calcVolume()
{ 
	volume = (4.0/3.0) * (PI) * (radius * radius * radius); 
}

 Sphere (string nm, double r = 1) : Shape3D(nm)
	{
		radius = r;
		calcVolume();
	}

void printVolume() 
{ 
	cout << "The Sphere's Volume is " << volume << endl; 
}


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
//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;

const double 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.
Last edited on
Use Sphere::Sphere instead of Sphere in line 14 at Sphere.cpp.
About box.cpp, dunno, everything looks legit.
I don't think this is correct in ur header:
Box (std::string nm, double l = 1, double w = 1, double h = 1) : Shape3D

Your Box::printVolume() looks fine.
printVolume() in ur Sphere class is wrong tho.
Last edited on
Topic archived. No new replies allowed.