Classes have me completely at a lost. Need some info

Pages: 12
Well we started classes last week in my C++ class but I have had swine flu and missed a lot of it. Anyways heres my assignment:

a. Construct a class named Rectangl that has double-precision data members named length and width. The class should have member functions named perimeter() and area() to calculate the rectangle's perimeter and area, a member function named setdata() to set a rectangle's length and width, and a member function named showdata() that displays a rectangles length, width, perimeter, and area.


Ive been attempting this for about 3 hours now and am completely lost. I can't even get my code to compile and Im sure if I did it wouldn't work. Can anyone help me?

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Rectangle
{
	private:
		double length;
		double width;
	public:
		void perimeter(double, double);
		void area(double, double);
		void setdata(double, double);
		void showdata();
};

void Rectangle::perimeter(double xx, double yy)
{
	length = xx;
	width = yy;
}

void Rectangle::area(double xx, double yy)
{
	length = xx;
	width = yy;
}

void Rectangle::setdata(double xx, double yy)
{
	length = xx;
	width = yy;
}

void Rectangle::showdata()
{
	cout << "The length is: ";
	cout << length << endl;
	cout << "The width is: ";
	cout << width << endl;
	cout << "The perimeter is: ";
	cout << perimeter << endl;
	cout << "The area is: ";
	cout << area; << endl;
}

int main()
{
	int x;
	int y;
	
	cout << "What is the length of the rectangle: ";
	cin >> x;
	cout << "\nWhat is the width of the rectangle: ";
	cin >> y;
	cout << endl;
	
	Rectangle a;
	
	a.setdata(x,y);
	a.showdata;

	return 0;
}
Your compile errors are on lines 45 and 62?

line 45: extraneous semicolon
line 62: missing parens

also, change lines 50 and 51 to double.

then you need to rewrite perimeter() and area() to do what the assignment asked.
Too slow...
Last edited on
Im getting this for an error when I compile:

 
DELETED CODE - TOO MUCH ROOM


which is complete gibberish. It keeps going but this forum only lets 9000 chars so I didnt copy all of it.


Also im really not to sure how to rewrite my perimeter and area functions. I know the formulas for calculating them, but I missed a lot on functions too. Im trying to go by our book, but it leaves a lot out.
Last edited on
perimeter() and area() are both functions.

they _should_ take no parameters and return a double. I'll help you with one; you write the other.

The area of a rectangle is defined as the length times the width. So:

1
2
3
double Rectangle::area() {
    return length * width;
}


Now inside your showdata():

 
std::cout << "The area of the rectangle is " << area() << " sq. units" << std::endl;


Thanks =)

That helped a lot, but Im still getting the same error when I compile. What does it even mean/how can I fix it?
To fix the compile error, you need to make a similar change above with your perimeter function as I've
done above with area.
Thanks =)

I forgot to put ; at the end of the return statements lol. I think I got it, can anyone see if this is doing what the assignment asks?

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Rectangle
{
	private:
		double length;
		double width;
	public:
		double perimeter();
		double area();
		void setdata(double, double);
		void showdata();
};

double Rectangle::perimeter()
{
	return ((2 * length) + (2 * width));
}

double Rectangle::area()
{
	return length * width;
}

void Rectangle::setdata(double xx, double yy)
{
	length = xx;
	width = yy;
}

void Rectangle::showdata()
{
	std::cout << "The length of the rectangle is "
			  << length << " units" << std::endl;
	std::cout << "The width of the rectangle is "
			  << width << " units" << std::endl;
	std::cout << "The perimeter of the rectangle is "
			  << perimeter() << " units" << std::endl;
	std::cout << "The area of the rectangle is "
			  << area() << " sq. units" << std::endl;
}

int main()
{
	double x;
	double y;
	
	std::cout << "What is the length of the rectangle: ";
	cin >> x;
	std::cout << "\nWhat is the width of the rectangle: ";
	cin >> y;
	std::cout << std::endl;
	
	Rectangle a;
	
	a.setdata(x,y);
	a.showdata();

	return 0;
}


And can anyone now explain to me what classes are used for? I know they're like one of the most important things about C++ and I don't want to miss anything.

Thanks =)
The function of every computer program ever written, at a high level, is to take in input, process it, and
generate output. Your assignment explains what input your program is to take in, what it should do with
it, and what output it should generate.

You should be able to run your program over a representative set of inputs and manually verify
whether or not your program generates the correct output. Read: test it. You know what it is supposed
to do. Does it do what you want it to do?

Why use classes:
http://bytes.com/topic/c/answers/135387-why-use-classes
So would this be correct? I added the default constructor but im getting a terminal error when I run it. It asks for how many students and after the input it crashes:

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Student
{
private:
  int id_number;
  double grade[5];
  int count;
  double average;
public:
  Student(int = 0, double[5] = 0, int = 0, double = 0);
  void enterID();
  void enterGrade();
  void display();
};

Student::Student(int i, double g[5], int c, double a)
{
	id_number = i;
	grade[5] = g[5];
	count = c;
	average = a;
}
	

void Student::enterID()
{
  int xx = 0;
  std::cout << "Enter student ID number: ";
  cin >> xx;
  id_number = xx;
  
  return;
}

void Student::enterGrade()
{
  std::cout << "Enter five grades: ";
  
  for (count = 0; count < 5; count++)
    {
      cin >> grade[count];
	  average += grade[count];
    }
	
  average = (average / 5.0);
  
  return;
}

void Student::display()
{
  
  std::cout << "Student ID: " << id_number << std::endl;
  std::cout << "Average: " << average << std:: endl;
  average = 0;

  return;
}

int main()
{

  int NUM_STUDENTS = 0;
  int i = 0;
  
  cout << "How many students?: ";
  cin >> NUM_STUDENTS;

  Student a;

  for (i = 0; i < NUM_STUDENTS; i++)
    {
      a.enterID();
      a.enterGrade();
      a.display();
    }
  return 0;
}
Last edited on
Your constructor is screwed up, you cannot assign arrays to each other like that. What you are doing is only assigning the 5 element of each array (which is out of bounds, btw) to each other.

It seems your problem lies within not knowing how arrays/variables are working...I suggest re-reading those sections of your textbook or this tutorial.
Last edited on
so your saying this line is messed up?
 
grade[5] = g[5];


and if so would I change it to something like this?:

1
2
  for (count = 0; count < 5; count++)
      grade[count] = g[count];


I missed a lot in class and it really sucks because the only thing I didn't learn on my own before the class was Classes, Structures, Arrays, and Functions (most notibaly passing values to functions ect). Turns out thats what was covered when I was absent and I know they're all extremely important to C++.
Last edited on
Yes.

You should know that "copying an array" means "copying each element individually".

You should further be able to reason that the above loop does the same thing as

1
2
3
4
grade[0] = g[0];
grade[1] = g[1];
...
grade[4] = g[4];


and that that is copying each element individually.

Ok, well that fixes that, but im still getting the same application error after I run it =(

"10-2pgm5.exe" - Application Error
The instruction at "0x0040147d" referenced memory at "0x00000000". The memory could not be "read".

Click on OK to terminate the program
Click on CANCEL to debug the program

Anyone know how to fix this?
In your constructor, you are passing a NULL array if no value is passed, which is causing the issue.
What exactly does that mean? I know what my constructor is:

 
Student(int = 0, double[5] = 0, int = 0, double = 0);


I assume its something wrong with double[5] = 0. But I tried doing,

 
double[5] = {0,0,0,0,0} 

but that caused an error too.
Did you try using a debugger to step through the program to see where the error occurs? That will often help you with any problems, not just this kind.
Just debugged it in Visual C++ 2008. Heres the whole 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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Student
{
private:
  int id_number;
  double grade[5];
  int count;
  double average;
public:
  Student(int = 0, double[5] = 0, int = 0, double = 0);
  void enterID();
  void enterGrade();
  void display();
};

Student::Student(int i, double g[5], int c, double a)
{
	id_number = i;
	for (count = 0; count < 5; count++)
                     grade[count] = g[count];
	count = c;
	average = a;
}
	

void Student::enterID()
{
  int xx = 0;
  std::cout << "Enter student ID number: ";
  cin >> xx;
  id_number = xx;
  
  return;
}

void Student::enterGrade()
{
  std::cout << "Enter five grades: ";
  
  for (count = 0; count < 5; count++)
    {
      cin >> grade[count];
	  average += grade[count];
    }
	
  average = (average / 5.0);
  
  return;
}

void Student::display()
{
  
  std::cout << "Student ID: " << id_number << std::endl;
  std::cout << "Average: " << average << std:: endl;
  average = 0;

  return;
}

int main()
{

  int NUM_STUDENTS = 0;
  int i = 0;
  
  cout << "How many students?: ";
  cin >> NUM_STUDENTS;

  Student a;

  for (i = 0; i < NUM_STUDENTS; i++)
    {
      a.enterID();
      a.enterGrade();
      a.display();
    }
  return 0;
}


This is where the debugger stops:

1
2
	for (count = 0; count < 5; count++)
                     grade[count] = g[count]; //arrow points to this line. 


Whats wrong?
Since you are calling your constructor with no parameters, g is NULL at that point, so you will get a segfault trying to access it.
So I need to change something in line 15?

 
Student(int = 0, double[5] = 0, int = 0, double = 0);

Im assuming its the bolded part, but im not sure how to set each parameter in an array to 0 in a constructor. I tried:

 
double[5] = {0,0,0,0,0}



Still working on this code, taking days now sadly =(

Current 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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int NUM_STUDENTS = 0;
const int NUM_GRADES = 5;

class Student
{
private:
  int id_number;
  int count;
  double grade[5];
  double average;
public:
  Student(int, int, double[5], double);
  void enterID();
  void enterGrade();
  void display();
};

Student::Student(int i, int c, double g[NUM_GRADES], double a)
{

	id_number = i;
	count = c;
	for (count = 0; count < NUM_GRADES; count++)
		g[count] = 0;
	for (count = 0; count < NUM_GRADES; count++)
      grade[count] = g[count];
	average = a;
}
	

void Student::enterID()
{
  int xx = 0;
  std::cout << "Enter student ID number: ";
  cin >> xx;
  id_number = xx;
  
  return;
}

void Student::enterGrade()
{
  std::cout << "Enter five grades: ";
  
  for (count = 0; count < NUM_GRADES; count++)
    {
      cin >> grade[count];
	  average += grade[count];
    }
	
  average = (average / NUM_GRADES);
  
  return;
}

void Student::display()
{
  
  std::cout << "Student ID: " << id_number << std::endl;
  std::cout << "Average: " << average << std:: endl;
  average = 0;

  return;
}

int main()
{

  int NUM_STUDENTS = 0;
  int i = 0;
  
  cout << "How many students?: ";
  cin >> NUM_STUDENTS;

  Student a (0, 0, 0, 0);

  for (i = 0; i < NUM_STUDENTS; i++)
    {
      a.enterID();
      a.enterGrade();
      a.display();
    }
  return 0;
}


But it still causes the same error.
Last edited on
Pages: 12