Beginner on Classes!

!!Can anyone help me out with this question so i can get started in writing a program for it... and this is what i have so far...... the question is..

Create a class that represents a length specified in inches or in inches and feet. The class will have the following features:

A constructor that takes the length in inches (e.g. 74 inches)
A constructor that takes the length in feet and inches (e.g. 6'4'')
Setters and getters for the length and height. See the driver for the prototypes.

Name the file FeetInches.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
#include <iostream>
 using namespace std;

 class FeetInches
{
    private:
        int feet;
        int inches;
        int inches2;

    public:
        FeetInches();
        void addInches(int inches);
        void addInchesFeet(int feet, int inches2);

};

FeetInches::FeetInches()
{

}

void FeetInches::addInches(int inches)
{

}

void FeetInches::addInchesFeet(int feet, int inches2)
{

}


this is the main that will be tested with my class.

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
#include <iostream>
#include "FeetInches.cpp"
using namespace std;
 
void doOutput(string name, FeetInches h)
{
    cout << name << " is " << h.getFeetComponent() << "'" << h.getInchesComponent() << "\"" << endl;
}
 
 
 
int main()
{
    cout << "-- Testing constructors --" << endl;
    FeetInches h1(54);
    FeetInches h2(7, 9);
    doOutput("h1", h1);
    doOutput("h2", h2);
 
    cout << "-- Testing setLength --" << endl;
    h1.setLength(2, 9);
    h2.setLength(37);
    doOutput("h1", h1);
    doOutput("h2", h2);
 
    h1.setLength(6, 4);
    h2.setLength(76);
    doOutput("h1", h1);
    doOutput("h2", h2);
 
    h1.setLength(6, 3);
    h2.setLength(76);
    doOutput("h1", h1);
    doOutput("h2", h2);
 
    h1.setLength(6, 5);
    h2.setLength(76);
    doOutput("h1", h1);
    doOutput("h2", h2);
}
Last edited on
A constructor that takes the length in inches (e.g. 74 inches)
A constructor that takes the length in feet and inches (e.g. 6'4'')

You only have one constructor, to stay true to the problem and in order for the "main.cpp" file to work with your class, you are going to have to use two constructors, like so:

1
2
FeetInches(int inches);
FeetInches(int feet, int inches); // There is no need for the proceeding "2" in "inches" 


Your private members should look something like this:

1
2
3
int feet;
int inches;
// No need for "inches2" 


And you still need to implement the methods used in the "main.cpp" file, which can be written similarly to:

1
2
void setLength(int inches);
void setLength(int feet, int inches);


and

1
2
int getFeetComponent();
int getInchesComponent();


This may hopefully work for you.

Edit:
You can write code similar to this in both your constructors and setters:
1
2
this.feet = feet;
this.inches = inches;
Last edited on
so my code should look something similar too this?
and for the

1
2
this->feet = feet;
this->inches = inches;


thats how we do it... so in which constructors and setters should i add that into?

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

 class FeetInches
{
    private:
        int feet;
        int inches;

    public:
        FeetInches();
        void addInches(int inches);
        void addInchesFeet(int feet, int inches);
        void setLength(int inches);
        void setLength(int feet, int inches);
        int getFeetComponent();
        int getInchesComponent();

};

FeetInches::FeetInches()
{

}

void FeetInches::addInches(int inches)
{

}

void FeetInches::addInchesFeet(int feet, int inches)
{

}

void FeetInches::setLength(int inches)
{

}

void FeetInches::setLength(int feet, int inches)
{

}

int FeetInches::getFeetComponent()
{

}

int FeetInches::getInchesComponent()
{

}
nevermind. this is my new 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
#include <iostream>
 using namespace std;

 class FeetInches
{
    private:
        int feet;
        int inches;

    public:
        FeetInches(int inches);
        FeetInches(int feet, int inches);
        void addInches(int inches);
        void addInchesFeet(int feet, int inches);
        void setLength(int inches);
        void setLength(int feet, int inches);
        int getFeetComponent();
        int getInchesComponent();

};

FeetInches::FeetInches(int inches)
{
    this->inches = inches;
}

FeetInches::FeetInches(int feet, int inches)
{
    this->feet = feet;
    this->inches = inches;
}

void FeetInches::addInches(int inches)
{

}

void FeetInches::addInchesFeet(int feet, int inches)
{

}

void FeetInches::setLength(int inches)
{

}

void FeetInches::setLength(int feet, int inches)
{

}

int FeetInches::getFeetComponent()
{

}

int FeetInches::getInchesComponent()
{

}
the ones that have "int" i use "return" yes?... sorry if some questions that i ask sound dumb. i like to know exactly what is going on.
line 25: What's the value of this->feet? when you exit the constructor? Hint: It's garbage, because you don't initialize it.

main.cpp line 15: Your inches only contructor can get called with a value greater than 12. You need to take the number of inches supplied and convert it to inches and feet. Same is true for addInches() and setLength().

the ones that have "int" i use "return" yes?

Correct.






You mean the getters? Of course, they must return their respective components(probably by value) and also tag the as const(defensive coding).
The setters should be doing some assignment similar to those in the ctors.

Aceix.
so in this case if i have to convert feet AND inches into just inches, then i have to have a formula that does just that, correct? and return the result. and for just inches i return the same input back.. correct?
new 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
#include <iostream>
using namespace std;

class FeetInches
{
    private:
        int feet;
        int inches;

    public:
        FeetInches(int inches);
        FeetInches(int feet, int inches);
        void addInches(int inches);
        void addInchesFeet(int feet, int inches);
        void setLength(int inches);
        void setLength(int feet, int inches);
        int getFeetComponent();
        int getInchesComponent();

};

FeetInches::FeetInches(int inches)
{
    this->inches = inches;
}

FeetInches::FeetInches(int feet, int inches)
{
    //this->feet = feet;
    this->inches = inches;
}

void FeetInches::addInches(int inches)
{

}

void FeetInches::addInchesFeet(int feet, int inches)
{

}

void FeetInches::setLength(int inches)
{

}

void FeetInches::setLength(int feet, int inches)
{

}

int FeetInches::getFeetComponent()
{

    return feet;
}

int FeetInches::getInchesComponent()
{

    return inches;
}
a formula like...
 
result = feet * 12 + inches;
You're gonna have to initialize feet and inches in both constructors. If, for example, feet isn't being used, you can initialize it to 0: feet = 0;.
I don't really know why you would want to convert feet into inches, but that would be a correct formula I suppose.
Line 22: You're still not initializing feet.

Line 29: Why did you comment out the initialization of feet?

Your add ad set functions still have no code.

You might find it easier to have only an inches variables in your class (get rid of the feet variable). Then only do the conversion (division by 12) to feet in the getFeetComponent function. The getInchesComponent shoud perform a modulo function and never return an inches values >= 12.
Topic archived. No new replies allowed.