First Program Using Classes

Hey everyone,

I'm a bit confused as to how to use classes and arrays together, and then calling functions to use on them, as this is my first experience with classes. Please help me in fundamental things I may be doing wrong from the start. Thanks.

Here is my 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

#include <iostream>
#include <string>

using namespace std;

class Box
{
	public:
		Box();
		int getDepth(int x[4],int y[4]);
		int getMaxX(int x[4]);
		int getMaxY(int y[4]);
		int getMinX(int x[4]);
		int getMinY(int y[4]);
		string getType(string type, int x[4], int y[4]){return type;}
		int getVolume(int width, int height, int depth);
		int getWidth(int x[4], int y[4]);
		void setData(int x[4],  int y[4], int z[4]);

	private:
		int width;
		int depth;
		int height;
		int Volume;
		string type, temp;
};

Box::Box()
{
	width = 5; // initialize the data members of the class object here
	depth = 14; 
	height = 1;
}

int Box::getDepth(int x[4], int y[4])
{
	for (int i=0;i<4;i++)	
	{
		while (x[i+1]-x[i]==0) //While x values remain constant, y values will determine depth of box
		{ 
			depth = y[i+1]-y[i];
		}
	}
}

int Box::getMaxX(int x[4]) // Highest Value of X from the 4 X and Y pairs
{
	for (int i=0;i<4;i++)
	{
		for (int j = 0; j<3;j++)
		{
			temp = maxX[j];
			maxX[j]=maxX[j+1];
			maxX[j+1] = temp;

		}
	}
}

int Box::getMaxY(int y[4]) // Highest Value of YX from the 4 X and Y pairs
{
	for (int i=0;i<4;i++)
	{
		for (int j = 0; j<3;j++)
		{
			temp = maxY[j];
			maxY[j]=maxY[j+1];
			maxY[j+1] = temp;
		}
	}
}

int Box::getMinX(int x[4]) // Lowest Value of X from the 4 X and Y pairs
{
	for (int i=0;i<4;i++)
	{
		for (int j = 0; j<3;j++)
		{
			while (x[j]>=x[j+1])
			{
			temp = minX[j];
			minX[j]=minX[j+1];
			minX[j+1] = temp;
			}
		}
	}
}

int Box::getMinY(int y[4],minY[]) //Lowest Value of Y the 4 X and Y pairs
{
	for (int i=0;i<4;i++)
	{
		for (int j=0; j<3; j++)
		{
			while (y[j]>=y[j+1])
			{
			temp = minY[j];
			minY[j]=minY[j+1];
			minY[j+1] = temp; 
			}
		}
	}
}

string Box::getType(string type, int x[4],int y[4]) //If sides are equal, Cube, otherwise, Rectangle
{
	for (int i=0;i<4;i++)
	{
		if (depth == width)
		{
			type = "Cube";
		}
		else 
			type = "Rectangle";
	}
}

int Box::getVolume(int width, int height, int depth)
{
	Volume = width * height * depth;
}

int Box::getWidth(int x[4], int y[4])
{
	for (int i=0;i<4;i++)
	{
		while (y[i+1]-y[i]==0) //While y values remain constant, x values will determine width of box
		{
			width = x[i+1]-x[i];
		}
	}
}

void Box::setData(int x[4], int y[4], int z[4])
{
	for (int i=0;i<4;i++) //taking 4 sets of x and y inputs for coodinates and storing to x[i],y[i],z[i] for object r
	{
		cout<<"Enter x and y separated by spaces: " <<endl;
		cin>>x[]>>y[];
		cout<<"Enter z: "<<endl;
		cin>>z[];
	}
}

int main()
{
	Box r([x[4],y[4],z[1]); // declaring array of x and y coordinates of object r
	{
		for (i = 0; i<4; i++)
		{
			width = x[i];
			depth = y[i];
			height = z[i];
		}

	}
	cout<<"Width of r: " << r.getWidth()<<endl;
	cout<<"Depth of r: " << r.getDepth()<<endl;
	cout<<"Box r is a: " << r.getType()<<endl;
	r.set_width(3);
	r.set_depth(5);

	//declare an array of 5 objects of type Box

	//call a function to load the user input into each object

	//call a function to print results for part #1 and #2

	//call a function to print results for part #3

	//print the results for part #4
}


/*

Results go here!

*/

Errors:
1>------ Rebuild All started: Project: aasdfs, Configuration: Debug Win32 ------
1> asdf.cpp
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(61): error C2065: 'maxX' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(62): error C2065: 'maxX' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(63): error C2065: 'maxX' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(75): error C2065: 'maxY' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(76): error C2065: 'maxY' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(77): error C2065: 'maxY' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(90): error C2109: subscript requires array or pointer type
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(91): error C2109: subscript requires array or pointer type
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(92): error C2109: subscript requires array or pointer type
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(106): error C2109: subscript requires array or pointer type
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(107): error C2109: subscript requires array or pointer type
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(108): error C2109: subscript requires array or pointer type
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(115): error C2084: function 'std::string Box::getType(std::string,int [],int [])' already has a body
1> c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(21) : see previous definition of 'getType'
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(148): error C2059: syntax error : ']'
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(150): error C2059: syntax error : ']'
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(156): error C2337: 'x' : attribute not found
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(156): error C2143: syntax error : missing ']' before '['
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(156): error C3409: empty attribute block is not allowed
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(156): error C2143: syntax error : missing ']' before 'constant'
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(156): error C2059: syntax error : 'constant'
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(158): error C2065: 'i' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(160): error C2065: 'width' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(160): error C2065: 'x' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(160): error C2065: 'i' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(161): error C2065: 'depth' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(161): error C2065: 'y' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(161): error C2065: 'i' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(162): error C2065: 'height' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(162): error C2065: 'z' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(162): error C2065: 'i' : undeclared identifier
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(166): error C2228: left of '.getWidth' must have class/struct/union
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(167): error C2228: left of '.getDepth' must have class/struct/union
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(168): error C2228: left of '.getType' must have class/struct/union
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(169): error C2228: left of '.set_width' must have class/struct/union
1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(170): error C2228: left of '.set_depth' must have class/struct/union
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========



Can you guys please point out the most important flaws in logic/syntax of mine? Thanks..
Most of these errors are a result of undeclared identifiers and you methods not return a value.

1
2
3
4
5
6
7
8
9
10
11
12
int Box::getMaxY(int y[4]) // Highest Value of YX from the 4 X and Y pairs
{
	for (int i=0;i<4;i++)
	{
		for (int j = 0; j<3;j++)
		{
			temp = maxY[j];
			maxY[j]=maxY[j+1];
			maxY[j+1] = temp;
		}
	}
}

You are not returning an integer and maxY and temp are both undeclared. The same applies to most of the methods defined inside of your class.

EDIT: Who's teaching you C++?
Last edited on
I know it may be homework, but if you don't understand classes maybe you should start with something a bit smaller, if you can get a small class to compile you will find it easier to get a bigger class to compile.
Im just working on the assignment really early, its not due for at least another week, were supposed to ask questions about it in class on wednesday after getting stumped by it on the weekend. I was just hoping I could get a leg up on my fellow classmates by coming in with some extra knowledge/experience =))
Topic archived. No new replies allowed.