What is wrong with my code?

I am new to C++ and I have been working on this for a while to display volume for two classes of objects. What is wrong with the code I typed. Thank you!
#include <iostream>
#include <string>
using namespace std;
class Prism //setting the class as prism.
{
private:
double height;
double width;
double length;
public:
double getVolume(void)
{
return length * width * height;
}

void setLength(double l)
{
length = l;
}

void setWidth(double w)
{
width = w;
}

void setHeight(double h)
{
height = h;
}

Prism(double h, double w, double l)
{
cout << "You have crafted a Rectangular prism.\n";
height = h;
width = w;
length = l;
}
};

class Pyramid
{
private:
double Height;
double Width;
double Length;
public:
double getVolume(void)
{
return (Length * Width * Height) / 3;
}

void setLength(double L)
{
Length = L;
}

void setWidth(double W)
{
Width = W;
}

void setHeight(double H)
{
Height = H;
}
};

int main()
{
Prism Prism1; //Declaring prism.
Pyramid Pyramid1; //declaring pryriamid.
double volume = 0.0; //setting what the volume is.
double Volume = 0.0; //setting what the volume is.

Prism1.setHeight(7.0); //setting the height for the prism.
Prism1.setWidth(7.0); //setting the Width for the prism.
Prism1.setLength(7.0); //setting the Length for the prism.

Pyramid1.setHeight(7.0); //setting the height for the pyriamid.
Pyramid1.setWidth(7.0); //setting the width for the pyriamid.
Pyramid1.setLength(7.0); //setting the length for the pyriamid.

volume = Prism1.getVolume(); //
cout << "The volmue of the rectangular prism is " << volume << ".\n"; //outputting a message of what the final volume is to the user.

Volume = Pyramid1.getVolume(); //
cout << "The volmue of the square base pyramid is " << Volume << ".\n"; //outputting a message of what the final volume is to the user.

return 0;
}
There is an error in the function Prism in Class Prism since the function type is not defined


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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

#include <iostream>
#include <string>
using namespace std;
class Prism //setting the class as prism.
{
private:
double height;
double width;
double length;
public:
double getVolume(void)
{
return length * width * height;
}

void setLength(double l)
{
length = l;
}

void setWidth(double w)
{
width = w;
}

void setHeight(double h)
{
height = h;
}

void Display()
{
cout << "You have crafted a Rectangular prism.\n";
cout<<"Height "<<height<<endl;
cout<<"Width "<<width<<endl;
cout<<"Length "<<length<<endl;
}
};

class Pyramid
{
private:
double Height;
double Width;
double Length;
public:
double getVolume(void)
{
return (Length * Width * Height) / 3;
}

void setLength(double L)
{
Length = L;
}

void setWidth(double W)
{
Width = W;
}

void setHeight(double H)
{
Height = H;
}
void Display2()
{
cout << "You have crafted a Rectangular pyramid.\n";
cout<<"Height "<<Height<<endl;
cout<<"Width "<<Width<<endl;
cout<<"Length "<<Length<<endl;
}
};

int main()
{
Prism Prism1; //Declaring prism.
Pyramid Pyramid1; //declaring pryriamid.
double volume = 0.0; //setting what the volume is.
double Volume = 0.0;
Prism1.setHeight(7.0); //setting the height for the prism.
Prism1.setWidth(7.0); //setting the Width for the prism.
Prism1.setLength(7.0); //setting the Length for the prism.

Pyramid1.setHeight(7.0); //setting the height for the pyriamid.
Pyramid1.setWidth(7.0); //setting the width for the pyriamid.
Pyramid1.setLength(7.0); //setting the length for the pyriamid.

volume = Prism1.getVolume(); //
cout << "The volmue of the rectangular prism is " << volume << ".\n"; //outputting a message of what the final volume is to the user.
Prism1.Display();

Volume = Pyramid1.getVolume(); //
cout << "The volmue of the square base pyramid is " << Volume << ".\n"; //outputting a message of what the final volume is to the user.
Pyramid1.Display2();
return 0;
}

Last edited on
looookster wrote:
There is an error in the function Prism in Class Prism since the function type is not defined


That is a constructor, they don't have a return type. Your code removes the ability to create a Prism with it's 3 values directly.
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
#include <iostream>
using namespace std;

class Prism {
	double height {};
	double width {};
	double length {};

public:
	double getVolume(void) const { return length * width * height; }
	void setLength(double l) { length = l; }
	void setWidth(double w) { width = w; }
	void setHeight(double h) { height = h; }

	Prism() {}
	Prism(double h, double w, double l) : height(h), width(w), length(l) {
		cout << "You have crafted a Rectangular prism.\n";
	}
};

class Pyramid {
	double Height {};
	double Width {};
	double Length {};

public:
	double getVolume(void) const { return (Length * Width * Height) / 3.0; }
	void setLength(double L) { Length = L; }
	void setWidth(double W) { Width = W; }
	void setHeight(double H) { 	Height = H; }
};

int main()
{
	Prism Prism1;			//declaring prism.
	Pyramid Pyramid1;		//declaring pyramid.

	Prism1.setHeight(7.0);	        //setting the height for the prism.
	Prism1.setWidth(7.0);	        //setting the Width for the prism.
	Prism1.setLength(7.0);	        //setting the Length for the prism.

	Pyramid1.setHeight(7.0);	//setting the height for the pyramid.
	Pyramid1.setWidth(7.0);		//setting the width for the pyramid.
	Pyramid1.setLength(7.0);	//setting the length for the pyramid.

	const auto volprism {Prism1.getVolume()};
	cout << "The volume of the rectangular prism is " << volprism << ".\n";

	const auto volpyr {Pyramid1.getVolume()};
	cout << "The volume of the square base pyramid is " << volpyr << ".\n";
}

Last edited on
Registered users can post here. Sign in or register to post.