Improving

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

class Measurements
{
private:
	int sum1, sum2, temp;
	//float fFraction;
public:
	void getSum(int, int, int, int);
	void showSum();
	//void showFrac();
	~Measurements();
};
void Measurements::getSum(int a, int b, int x, int y)
{
	sum1 = a + b; // sum1 is the sum of the feet
	sum2 = x + y; // sum2 is the sum of the inches
	
	if(sum2 > 12)
	{
		sum1 = sum1 + (int)(sum2 / 12);
		sum2 = sum2 % 12;
	}
	
	return;
}
void Measurements::showSum()
{
	std::cout << "The sum of your measurements is: " << sum1 << " feet " << sum2 << " inches\n";
	return;
}
Measurements::~Measurements()
{
	std::cout << "Press the ENTER key to continue.";
	std::cin.ignore();
}		

int main()
{
	Measurements measurementObject;
	int feet1, feet2, inch1, inch2;
		
	std::cout << "Enter measurements as follows: 3 4, representing 3 feet 4 inches.\n";
	std::cout << "Enter your first integer measurement: "; std::cin >> feet1 >> inch1;
	std::cout << "Enter your second integer measurement: "; std::cin >> feet2 >> inch2;
	
	measurementObject.getSum(feet1, feet2, inch1, inch2);
	measurementObject.showSum();
	
		
	return 0;
}


Anyone have any suggestions on what I should improve on? Perhaps good reads.
Last edited on
Limit the scope of your variables. ¿why is temp a member?
Avoid magic numbers, ¿why 12?
IIRC your class may be better as
1
2
3
4
5
6
7
8
9
10
class lenght{
private:
  int feet, inch;
public:
  lenght(int feet, int inch);
  lenght add( const length &b ) const; //const-correctness 
  void write() const;
  void read();
//...
};


Integer division returns an integer, there is no need for that casting.
Those return; are no needed.

1
2
3
4
5
Measurements::~Measurements()
{
	std::cout << "Press the ENTER key to continue.";
	std::cin.ignore();
}
Yuck. ¿What would happen if you need several objects of that class?
Consider changing the name of getSum() to makeSum(). I would expect getSum to return the sum.

You don't really need to store feet, just inches.
1
2
3
4
5
6
7
8
9
10
11
12
int Measurements::getTotalInches()
{
  return inches;
}
int Measurements::getFeet()
{
  return inches / 12;
}
int Measurements::getInches()
{
  return inches % 12;
}


The whole point of that, for me, would be to get the cout statements out of the class.
Last edited on
Topic archived. No new replies allowed.