Question about classes

I've gotten used to writing all the classes in the main but I also learned that you can create new class and headers. That is where I become confused. This is what i have to give some background info. I use CodeBlocks.

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
  #include <iostream>
#include <string>
#include <windows.h>
#include <math.h>

using namespace std;

class Regular
{
float a;
float p;
float c;
public:
      void setarea(float a)
      {
          area = a;
      }

      void setsides(float c)
      {
          sides = c;
      }

      void setperimeter(float p)
      {
          perimeter = p;
          sum1 = perimeter * sides;
      }

      float getsides()
      {
          return sides;
      }

      float getarea()
      {
          return area;
      }

      float getperimiter()
      {
          return sum1;
      }

      float setanswer()
      {
        answer = (area * sum1);
        answer / 2;
      }

      float getanswer()
      {
          return answer;
      }

private:
    float area;
    float perimeter;
    float sides;
    float sum1;
    float answer;
};

int main()
{
string regular;
float b;
float s;
float p;
    cout << "Is the polygon regular or composite?" << endl;
    cin >> regular;
    if(regular == "Regular" || regular == "regular")
    {
        cout << "What is the radius?" <<endl;
        cin >> b;
        Regular Regular1;
        Regular1.setarea(b);
        cout << Regular1.getarea() <<endl;
        cout << "What is the number of sides?" <<endl;
        cin >> s;
        Regular1.setsides(s);
        cout << Regular1.getsides() <<endl;
        cout << "What is the perimeter?" <<endl;
        cin >> p;
        Regular1.setperimeter(p);
        cout << Regular1.getperimiter() <<endl;
        cout << "Your answer is going to be " << Regular1.getanswer() <<endl;
    }
    return 0;
}


Then I went to new then class and did the things that sololearn told me to but I don't know where to put the class. Do I put it in the .cpp or .h

Thanks,
garrows
Last edited on
Your code is the same as this if you wrote it all in main
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
#include <iostream>
#include <string>
#include <windows.h>
#include <math.h>

using namespace std;

class Regular
{
private:
	float area;
	float perimeter;
	float sides;
	float sum1;
	float answer;
public:
	void setarea(float a);
	void setsides(float c);
	void setperimeter(float p);
	float getsides();
	float getarea();
	float getperimiter();
	void setanswer();
	float getanswer();



};

void Regular::setarea(float a)
{
	area = a;
}

void Regular::setsides(float c)
{
	sides = c;
}

void Regular::setperimeter(float p)
{
	perimeter = p;
	sum1 = perimeter * sides;
}

float Regular::getsides()
{
	return sides;
}

float Regular::getarea()
{
	return area;
}

float Regular::getperimiter()
{
	return sum1;
}

void Regular::setanswer()//needs to be a void because you are not returning anything
{
	answer = (area * sum1);
	answer /= 2; // i think you meant that

}

float Regular::getanswer()
{
	return answer;
}

int main()
{
	string regular;
	float b;
	float s;
	float p;
	cout << "Is the polygon regular or composite?" << endl;
	cin >> regular;
	if (regular == "Regular" || regular == "regular")
	{
		cout << "What is the radius?" << endl;
		cin >> b;
		Regular Regular1;
		Regular1.setarea(b);
		cout << Regular1.getarea() << endl;
		cout << "What is the number of sides?" << endl;
		cin >> s;
		Regular1.setsides(s);
		cout << Regular1.getsides() << endl;
		cout << "What is the perimeter?" << endl;
		cin >> p;
		Regular1.setperimeter(p);
		cout << Regular1.getperimiter() << endl;
		cout << "Your answer is going to be " << Regular1.getanswer() << endl;
	}
	return 0;
}


If your were to break this code up among the Regular.cpp, Regular.h, and source.cpp it would look like:
.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Regular
{
private:
	float area;
	float perimeter;
	float sides;
	float sum1;
	float answer;
public:
	void setarea(float a);
	void setsides(float c);
	void setperimeter(float p);
	float getsides();
	float getarea();
	float getperimiter();
	void setanswer();
	float getanswer();

};


Regular.cpp
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
void Regular::setarea(float a)
{
	area = a;
}

void Regular::setsides(float c)
{
	sides = c;
}

void Regular::setperimeter(float p)
{
	perimeter = p;
	sum1 = perimeter * sides;
}

float Regular::getsides()
{
	return sides;
}

float Regular::getarea()
{
	return area;
}

float Regular::getperimiter()
{
	return sum1;
}

void Regular::setanswer()//needs to be a void because you are not returning anything
{
	answer = (area * sum1);
	answer /= 2; // i think you meant that

}

float Regular::getanswer()
{
	return answer;
}


and the source would keep your main function.

Note: Though I do suggest 2 changes I only tested the code to compile and not to run correctly. You should also have a constructor, both a default and overloaded.
Also:

Prefer double rather than float. The latter looses precision quickly, e.g. 9.99 * 9.99 looses 1 significant figure.

You should not have the setarea, setperimeter, functions, the respective get functions should do the calculation. And they are considerably more involved that what you have now.

setanswer is also dodgy, it shouldn't be there at all.

sum1 is a poor name for a variable, change to something more meaningful.
Last edited on
Topic archived. No new replies allowed.