convert struct to class

Good afternoon,

I'm trying to switch this code from a struct to a class to get some practice in, I have hit a road block and unclear of what i'm trying to do. I have little experience with structs or classes. I started some of it, I am trying to change the height1, height2 to constructor to set the private members. I don't want any solutions just a nudge in the right direction and how to better conceptualize classes. Thank you

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

const double INCHES_IN_FOOT        = 12.0;
const double CENTIMETERS_IN_INCHES = 2.54;

// PersonHeight structure definition with member variables.
class PersonHeight
{
private:
    int feet;
	int inches;

public:
    //set the inches and feet
    PersonHeight()
    {
        setHeight(5,8);
    }

    void setHeight(int a, int b)
    {
        feet = a;
        inches = b;
    }
    //accessors
    int getFeet()
    {
        return feet;
    }

    int getInches()
    {
        return inches;
    }

    // Prototypes for functions dealing with ingredient measurements.

// Display person's height in feet and inches.
void outputHeight(PersonHeight height);

// Convert person's height to centimeters.
double convertHeightToCentimeters(PersonHeight height);

};



int main()
{
	PersonHeight height1;
	PersonHeight height2;// = { 6, 1 };

	// Output floating-point numbers to 1 decimal place.
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(1);

	cout << "person1 is ";
	PersonHeight::outputHeight(height1);
	cout << " tall (read "
		     << height1.feet << " feet, "
			 << height1.inches << " inches)"
			 << endl;

	cout << "person2 is ";
	outputHeight(height2);
	cout << " tall, which is "
	     << convertHeightToCentimeters(height2)
		 << " centimeters" << endl;

	int feet, inches;
	do
	{
		cout << "\nEnter height (feet inches, 0 0=exit): ";
		cin >> feet >> inches;

		if (feet == 0 && inches == 0)
			break;

		height1.feet = feet;
		height1.inches = inches;

		cout << "\nperson is ";
		outputHeight(height1);
		cout << " tall, which is "
			 << convertHeightToCentimeters(height1)
			 << " centimeters" << endl;
	} while (true);

	return 0;
}

// Definitions for functions dealing with heights.

void PersonHeight::outputHeight(PersonHeight height)
{
	cout << height.getFeet() << "\'" << height.getInches() << "\"";
}

double PersonHeight::convertHeightToCentimeters(PersonHeight height)
{
	return (height.feet * INCHES_IN_FOOT + height.inches) * CENTIMETERS_IN_INCHES;
}
change the word struct to class and add a public: after the first {

eg
struct foo{stuff}

becomes
class foo{ public: stuff}

you may need to clean it up after, to make private things private with access functions and all that, but this is all that is required for syntax. The other stuff you need to do is OOP style and convention.

in syntax, in c++, a struct is just a class with default public access (class is default private).
in usage and convention, a struct is usually seen as just pure data in c++ (no methods at all) and classes have methods and other complexity (inheritance, operators (which are technically methods), etc).

I don't see a struct here?
Last edited on
Also... member functions don't take an argument of themselves, so...

void outputHeight(PersonHeight height);

would simply become...

void outputHeight();

To call the member function, you would say...

height1.outputHeight();
Ok thank you I will try these. And I have to figure out the mutation, to change and use the inches and feet. So I was trying it out and made some changes and having still some issues

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

const double INCHES_IN_FOOT        = 12.0;
const double CENTIMETERS_IN_INCHES = 2.54;

// PersonHeight structure definition with member variables.
class PersonHeight
{
private:
    int feet;
	int inches;

public:
    //set the inches and feet
    PersonHeight()
    {
        setHeight(5,8);
    }

    void setHeight(int a, int b)
    {
        feet = a;
        inches = b;
    }

    void setInches(int a)
    {
        inches = a;
    }

    void setFeet(int a)
    {
        feet = a;
    }


    //accessors
    int getFeet()
    {
        return feet;
    }

    int getInches()
    {
        return inches;
    }

    // Prototypes for functions dealing with ingredient measurements.

// Display person's height in feet and inches.
void outputHeight();

// Convert person's height to centimeters.
double convertHeightToCentimeters();

};



int main()
{
	PersonHeight height1;
	PersonHeight height2;// = { 6, 1 };

	// Output floating-point numbers to 1 decimal place.
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(1);

	cout << "person1 is ";
	height1.outputHeight();
	cout << " tall (read "
		     << height1.getFeet() << " feet, "
			 << height1.getInches() << " inches)"
			 << endl;

	cout << "person2 is ";
	height2.outputHeight();
	cout << " tall, which is "
	     << height2.convertHeightToCentimeters()
		 << " centimeters" << endl;

	int feet, inches;
	do
	{
		cout << "\nEnter height (feet inches, 0 0=exit): ";
		cin >> feet >> inches;

		if (feet == 0 && inches == 0)
			break;

		height1.setFeet(feet);
		height1.setInches(inches);

		cout << "\nperson is ";
		height1.outputHeight();
		cout << " tall, which is "
			 << height1.convertHeightToCentimeters()
			 << " centimeters" << endl;
	} while (true);

	return 0;
}

// Definitions for functions dealing with heights.

void PersonHeight::outputHeight()
{
	cout << PersonHeight.getFeet() << "\'" << PersonHeight.getInches() << "\"";
}

double PersonHeight::convertHeightToCentimeters()
{
	return (height.feet * INCHES_IN_FOOT + height.inches) * CENTIMETERS_IN_INCHES;
}

Last edited on
Warning!!! Here is the answer. Only look if you want to see the answer!!!!

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

const double INCHES_IN_FOOT = 12.0;
const double CENTIMETERS_IN_INCHES = 2.54;

// PersonHeight structure definition with member variables.
class PersonHeight
{
private:
	int feet;
	int inches;

public:
	//set the inches and feet
	PersonHeight()
	{
		setHeight(5, 8);
	}

	void setHeight(int a, int b)
	{
		feet = a;
		inches = b;
	}
	//accessors
	int getFeet()
	{
		return feet;
	}

	int getInches()
	{
		return inches;
	}

	// Prototypes for functions dealing with ingredient measurements.

	// Display person's height in feet and inches.
	void outputHeight();

	// Convert person's height to centimeters.
	double convertHeightToCentimeters();

};



int main()
{
	PersonHeight height1;
	PersonHeight height2;// = { 6, 1 };

						 // Output floating-point numbers to 1 decimal place.
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(1);

	cout << "person1 is ";
	//PersonHeight::outputHeight(height1);
	height1.outputHeight();
	cout << " tall (read "
		<< height1.getFeet() << " feet, "
		<< height1.getInches() << " inches)"
		<< endl;

	cout << "person2 is ";
	//outputHeight(height2);
	height2.outputHeight();
	cout << " tall, which is "
		<< height2.convertHeightToCentimeters()
		<< " centimeters" << endl;

	int feet, inches;
	do
	{
		cout << "\nEnter height (feet inches, 0 0=exit): ";
		cin >> feet >> inches;

		if (feet == 0 && inches == 0)
			break;

		height1.setHeight(feet, inches);
		

		cout << "\nperson is ";
		height1.outputHeight();
		cout << " tall, which is "
			<< height1.convertHeightToCentimeters()
			<< " centimeters" << endl;
	} while (true);

	return 0;
}

// Definitions for functions dealing with heights.

void PersonHeight::outputHeight()
{
	cout << feet << "\'" << inches << "\"";
}

double PersonHeight::convertHeightToCentimeters()
{
	return (feet * INCHES_IN_FOOT + inches) * CENTIMETERS_IN_INCHES;
}
some issues

What issues? Is this supposed to be some kind of guessing game?
The "some issues" are easy to solve with "some answers". That makes us all top-notch psychics.


Some variations:
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
PersonHeight::PersonHeight()
 : feet( 5 ), inches( 8 )
{
}

PersonHeight::PersonHeight( int f, int i )
 : feet( f ), inches( i )
{
}

void PersonHeight::outputHeight( std::ostream& out ) const
{
  out << feet << "\'" << inches << "\"";
}

std::ostream& operator<< ( std::ostream& lhs, const PersonHeight & rhs )
{
  rhs.outputHeight( lhs );
  return lhs;
}

double PersonHeight::convertHeightToCentimeters()
{
  // non-global constants
  static const double INCHES_IN_FOOT = 12.0;
  static const double CENTIMETERS_IN_INCHES = 2.54;

  return (feet * INCHES_IN_FOOT + inches) * CENTIMETERS_IN_INCHES;
}
Heh :)

I see his post as "I am still working on it" not "I have problems I want you guys to fix".
Thank you everyone for your help! I will continue on with maybe trying to add another object for weight and do a conversion with that as well. I have always had trouble with programming but I refuse to believe i can't grasp all this.

Once again thank you everyone!
less Java and more C++ and you'll be fine ;D
Topic archived. No new replies allowed.