problems with code

hi all,
I'm trying to use derived classes to create 3d shapes and output them, this is my header file
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
#include <math.h>
using namespace std;

const double PI = 3.14;
class Shape {
public:
	virtual double SurfaceArea() = 0;
	virtual double Volume() = 0;
	virtual double WhatShapeAmI() = 0;
	string Dimensions() = 0;
};

class Cone: public Shape {
public:
	double m_height, m_radius, m_side;
	Cone cone(double height, double radius);
	void setData(int h, int r){
        m_height = h;
        m_radius = r;
    }
	double SurfaceArea(){
		m_side = ((m_height*m_height)+(m_radius*m_radius)); 
		return ((PI*m_radius*m_side)+(PI*m_radius*m_radius));}
	double Volume(){
		return ((1/3)*PI*m_radius*m_radius*m_height);}
	string WhatShapeAmI(){
	return ("cone");
	}
	string dimensions(){
		string dims = "height= " + m_height + "\n" + "radius= " + m_radius + "/n";
		return dims;
	}
};

class Cylinder: public Shape {
public:
	double m_height, m_radius;
	Cylinder cylinder(double height, double radius);
	void setData(int h, int r){
        m_height = h;
        m_radius = r;
    }
	double SurfaceArea(){
		return ((2*PI*m_radius*m_radius)+(2*PI*m_radius*m_height));}
	double Volume(){
		return (PI*m_radius*m_radius*m_height);}
	string WhatShapeAmI(){
		return ("Cylinder");
	}
	string dimensions(){
		string dims = "height= " + m_height + "\n" + "radius= " + m_radius + "/n";
		return dims;

};

class RectangularPrism : public Shape {
public:
	double m_height, m_length, m_width;
	RectangularPrism rectangularprism(double height, double length, double width);
	void setData(int h, int l, int w){
        m_height = h;
        m_length = l;
		m_width = w;
    }
	double SurfaceArea(){
		return (2*((m_width*m_height)+(m_length*m_width)+(m_length*m_height)));}
	double Volume(){
		return (m_length*m_width*m_height);}
	string WhatShapeAmI(){
		return ("rectangular prism");}
	string dimensions(){
		string dims = "height= " + m_height + "\n" + "length= " + m_width + "/n" + "width=" + m_width;
	}
		return dims;
};


class Pyramid: public Shape {
double m_height, m_base;

public:	
	Pyramid pyramid(double height, double base);
	void setData(int h, int b){
        m_height = h;
        m_base = b;
    }
	double SurfaceArea(){
		return((2*m_base* (sqrt((((1/2)*m_base) * ((1/2)*m_base)) + (m_height*m_height))))+(m_base*m_base)); }
	double Volume(){
		return((1/3)*(m_base*m_base)*m_height);}
	string WhatShapeAmI(){
		return("pyramid");}
	string dimensions(){
		string dims = "height= " + m_height + "\n" + "base= " + m_base + "/n";
		return dims;
	}
};

class Sphere : public Shape {
public:
	double m_radius;
	Sphere sphere(double radius);
	void setData( int r){
        m_radius = r;
    }
	double SurfaceArea(){
		return (4*PI*m_radius*m_radius);}
	double Volume(){
		return((4/3)*PI*m_radius*m_radius*m_radius);}
	string WhatShapeAmI(){
		return ("sphere");}
	string dimensions(){
		string dims = "radius= " + m_radius + "/n";
		return dims;}
};

and my main code
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
#include "shapes.h"
#include <vector>

int main() {

vector<Shape*> shapes;

shapes.reserve(5);

Cone cone;
cone.SetData(4,5);
shapes.push_back(cone);

Cylinder cylinder;
cylinder.SetData(6,12);
shapes.push_back(cylinder);

RectangularPrism rectangularprism;
rectangularprism.SetData(9,3);
shapes.push_back(rectangularprism);

Pyramid pyramid;
pyramid.SetData(4,8);
shapes.push_back(pyramid);

Sphere sphere;
sphere.SetData(6,12);
shapes.push_back(sphere);

for ( unsigned int i = 0; i<shapes.size(); i++)
{

	cout << endl << endl << "*************************" << endl;
	cout << "shape number " <<i+1<< " is a "<< shapes[i] ->WhatShapeAmI()<< endl;
	cout << "dimensions: " <<shapes[i] ->Dimensions()<< endl;
	cout << "Surface = " <<shapes[i] ->SurfaceArea() <<endl;
	cout << "volume = " <<shapes[i] ->Volume() <<endl;

}

and this is my output
1>------ Build started: Project: 3d_shapes, Configuration: Debug Win32 ------
1>  3d_shapes.cpp
 error C2253: 'Shape::Dimensions' : pure specifier or abstract override specifier only allowed on virtual function
(36): error C2555: 'Cone::WhatShapeAmI': overriding virtual function return type differs and is not covariant from 'Shape::WhatShapeAmI'
shapes.h(12) : see declaration of 'Shape::WhatShapeAmI'
\shapes.h(33): error C2440: 'initializing' : cannot convert from 'System::String ^' to 'std::basic_string<_Elem,_Traits,_Ax>'
         with
         [
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
         ]
         No constructor could take the source type, or constructor overload resolution was ambiguous
shapes.h(77): error C2059: syntax error : 'return'
shapes.h(77): error C2238: unexpected token(s) preceding ';'
shapes.h(78): error C2555: 'Cylinder::RectangularPrism::WhatShapeAmI': overriding virtual function return type differs and is not covariant from 'Shape::WhatShapeAmI'
shapes.h(12) : see declaration of 'Shape::WhatShapeAmI'
shapes.h(100): error C2555: 'Cylinder::Pyramid::WhatShapeAmI': overriding virtual function return type differs and is not covariant from 'Shape::WhatShapeAmI'
shapes.h(12) : see declaration of 'Shape::WhatShapeAmI'
shapes.h(118): error C2555: 'Cylinder::Sphere::WhatShapeAmI': overriding virtual function return type differs and is not covariant from 'Shape::WhatShapeAmI'
\shapes.h(12) : see declaration of 'Shape::WhatShapeAmI'
\memory(16): error C2059: syntax error : 'namespace'
memory(16): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
setjmp.h(40): error C2059: syntax error : 'string'
setjmp.h(40): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
setjmp.h(250): error C2059: syntax error : 'string'
\setjmp.h(251): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
mmintrin.h(26): error C2059: syntax error : 'string'
\mmintrin.h(26): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
xmmintrin.h(178): error C2059: syntax error : 'string'
\xmmintrin.h(178): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
emmintrin.h(60): error C2059: syntax error : 'string'
emmintrin.h(60): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
pmmintrin.h(55): error C2059: syntax error : 'string'
pmmintrin.h(55): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
tmmintrin.h(30): error C2059: syntax error : 'string'
\tmmintrin.h(30): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
smmintrin.h(78): error C2059: syntax error : 'string'
smmintrin.h(78): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
nmmintrin.h(31): error C2059: syntax error : 'string'
\nmmintrin.h(31): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
wmmintrin.h(30): error C2059: syntax error : 'string'
wmmintrin.h(30): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
immintrin.h(23): error C2059: syntax error : 'string'
immintrin.h(23): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
ammintrin.h(23): error C2059: syntax error : 'string'
ammintrin.h(23): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
mm3dnow.h(26): error C2059: syntax error : 'string'
mm3dnow.h(26): error C2334: unexpected token(s) preceding '{'; skipping apparent function body

intrin.h(38): error C2334: unexpected token(s) preceding '{'; skipping apparent function body

memory(993): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
memory(2047): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
\xfunctional(18): error C2059: syntax error : 'namespace'
xfunctional(18): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
vector(15): error C2059: syntax error : 'namespace'
vector(15): error C2334: unexpected token(s) preceding '{'; skipping apparent function body


any help would be appreciated,
const double PI = 3.14;
class Shape {
public:
virtual double SurfaceArea() = 0;
virtual double Volume() = 0;
virtual double WhatShapeAmI() = 0;
string Dimensions() = 0;
};

Dimensions isn't virtual.
i have fixed that, still have error messages popping up
No closing brace in Cylinder, and out of scope return in rectangle.
thank you, i have ran through it and tried to fix the little mistypes with my code, i keep getting
No constructor could take the source type, or constructor overload resolution was ambiguous
1>shapes.h(100): error C2440: 'initializing' : cannot convert from 'System::String ^' to 'std::basic_string<_Elem,_Traits,_Ax>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous

and
'double Shape::SurfaceArea(void)' : is abstract
1>          shapes.h(10) : see declaration of 'Shape::SurfaceArea'
1>          'double Shape::Volume(void)' : is abstract
1>         shapes.h(11) : see declaration of 'Shape::Volume'
1>          'std::string Shape::WhatShapeAmI(void)' : is abstract
1>         shapes.h(12) : see declaration of 'Shape::WhatShapeAmI'
1>          'std::string Shape::Dimensions(void)' : is abstract
1>         shapes.h(13) : see declaration of 'Shape::Dimensions'
1>          3d_shapes.cpp(6) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=Shape
1>          ]
1>3d_shapes.cpp(15): error C2039: 'SetData' : is not a member of 'Cylinder'
1>         shapes.h(40) : see declaration of 'Cylinder'
1>3d_shapes.cpp(19): error C2039: 'SetData' : is not a member of 'RectangularPrism'
1>          shapes.h(62) : see declaration of 'RectangularPrism'
1>3d_shapes.cpp(23): error C2039: 'SetData' : is not a member of 'Pyramid'
1>       shapes.h(84) : see declaration of 'Pyramid'
1>3d_shapes.cpp(27): error C2039: 'SetData' : is not a member of 'Sphere'
1>         shapes.h(105) : see declaration of 'Sphere'
1>3d_shapes.cpp(34): error C2819: type 'Shape' does not have an overloaded member 'operator ->'
1>         shapes.h(8) : see declaration of 'Shape'
1>          did you intend to use '.' instead?
1>3d_shapes.cpp(34): error C2232: '->Shape::WhatShapeAmI' : left operand has 'class' type, use '.'
1>3d_shapes.cpp(35): error C2819: type 'Shape' does not have an overloaded member 'operator ->'
1>         shapes.h(8) : see declaration of 'Shape'
1>          did you intend to use '.' instead?
1>3d_shapes.cpp(35): error C2232: '->Shape::Dimensions' : left operand has 'class' type, use '.'
1>3d_shapes.cpp(36): error C2819: type 'Shape' does not have an overloaded member 'operator ->'
1>          shapes.h(8) : see declaration of 'Shape'
1>          did you intend to use '.' instead?
1>3d_shapes.cpp(36): error C2232: '->Shape::SurfaceArea' : left operand has 'class' type, use '.'
1>3d_shapes.cpp(37): error C2819: type 'Shape' does not have an overloaded member 'operator ->'
1> shapes.h(8) : see declaration of 'Shape'
1>          did you intend to use '.' instead?
1>3d_shapes.cpp(37): error C2232: '->Shape::Volume' : left operand has 'class' type, use '.'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

i have no idea what these errors mean, im sorry for being oblivious, but any help?
Last edited on
Topic archived. No new replies allowed.