Classes and Objects help

A question says I need to design a class called Length. The class should store the length for a string in two integers: feet and inches, and have two member functions: one called setLength which receives two integers and assigns to the two data members and the other called printLength which can display the length in the following format : 6 feet 11 inches Then in the main program, declare a Length object and call setLength() function with some data and then call printLength() to display the length.
Ok so I'm confused in how the data should be input. Should it be input in feet-inches format or does the program have to convert it. Also, how would I go about designing the class structure for this? Here's my attempt..

#include <iostream>
using namespace std;
#include <string>
using std::string;

class Length
{
Public:
setLength (int x, int y) //Don't know what type this should be
{
feet=x;
inches=y;
}
printLength () //This one either
{
cout << "The length is " << feet << " feet " << inches << " inches." << endl;
}
Private:
int feet, inches;
}
int main ()
{
int x, y;
cout << "Enter length in feet and inches: ";
cin >> x >> y;
Length stu (x, y); // Not sure if this is correct
stu.printLength (); // Saying Length has no member printLength
system ("PAUSE");
return 0;
}
Looks fine. As long as the user places a space between the two values. Its safer to ask for one at a time.
I'm no expert, but I copied your code into codeblocks and made it work by making the following changes:

1) "Public" and "Private" should not begin with capital "P"s;
2) You forgot a semicolon after the closing curly brace of your Length class;
3) You also forgot to put "void" before each of your functions in Length class;
4) You put the parameters, x and y, after you created the object "stu." You should have created a new line after you created your object, and called your setLength function from Length class, then set your parameters... like this:

1
2
3
4
5
6
7
8
9
10
int main ()
{
int x, y;
cout << "Enter length in feet and inches: ";
cin >> x >> y;
Length stu; // create the object (no parameters)
stu.setLength(x, y); // then call setLength function
stu.printLength (); // then call printLength function
return 0;
}


Here's 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
#include <iostream>
using namespace std;
#include <string>
using std::string;

class Length
{
public: // lower-case p
    void setLength (int x, int y) // I added "void"
{
feet=x;
inches=y;
}
    void printLength () //I added "void" here too.
{
cout << "The length is " << feet << " feet " << inches << " inches." << endl;
}
private:
int feet, inches;
};// I added a semicolon
int main ()
{
int x, y;
cout << "Enter length in feet and inches: ";
cin >> x >> y;
Length stu; // Created the object first. 
stu.setLength(x, y); // Use the object to call your setLength function first. 
stu.printLength (); // Then call printLength memeber. 
return 0;
} 

Hope this helps.

Kevin
Last edited on
I changed it to:
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
#include <iostream>
using namespace std;

class Length
{
public:
	void setLength (int f, int i)
	{
		feet = f;
		inches = i;
	}
	void printLength()
	{
		cout << feet << " feet " << inches << " inches";
	}
private:
	int feet, inches;
};

int main ()
{
	Length l;
	int x, y;
	cout << "Input length in feet-inches format: ";  
	cin >> x >> y;                  
	l.setLength (x, y);     
	l.printLength();
	system ("PAUSE");
	return 0;
}

And it works fine! Thanks so much for all your help guys! Life savers!
Nice...

Alternatively, you could ask for feet and inches separately, like this...

1
2
3
4
5
6
7
8
9
10
11
12
int main ()
{
	Length l;
	int x, y;
	cout << "Input length in feet: " << endl;  
	cin >> x;
               cout << "Enter inches: " << endl; 
               cin >> y; 
	l.setLength (x, y);     
	l.printLength();
	system ("PAUSE");
	return 0;
Topic archived. No new replies allowed.