Manipulating data members?

I'm trying to design a class called "Weight" which is made up of a double datatype (for the magnitude of weight) and a char for the unit. I've written out the header file and in the private section, included the data members which are

double myHeavy;
char myUnit;

My problem is, that I am trying to write a utility function in my .cpp file that will take the inputs of weight and unit, and return the converted weight (kilos to pounds or vice versa) and the other unit. My original plan was to do this:

1
2
3
4
5
6
7
8
9
10
11
// Definitions of Ultility Functions
double toKilos(double weight, char unit){      //converts from pounds to Kilos
	myUnit = 'K';
	return (weight * .4536);
}


double toPounds(double weight, char unit){      //Converts from kilos to pounds
	myUnit = 'L';								//Changes unit to L for pounds
	return (weight / .4536);                    //Returns converted weight
}



However, when compiling I get the following errors

15 [jason]/home/student/munoz> g++ weight.cpp driver.cpp
weight.cpp: In member function `unsigned int Weight::getHeavy() const':
weight.cpp:37: warning: converting to `unsigned int' from `const double'
weight.cpp: In function `double toKilos(double, char)':
weight.cpp:117: error: `myUnit' was not declared in this scope
weight.cpp: In function `double toPounds(double, char)':
weight.cpp:123: error: `myUnit' was not declared in this scope
weight.cpp:125:2: warning: no newline at end of file

I'm assuming its because I'm trying to alter the private function member.

Anyone have any good idea on how to do this? or perhaps an easier method? I don't suppose you can return a double and char using just one function???
I wouldn't keep a char to determine the Unit. Just use a consistent unit, and if you need a different unit just use a different function.

Like for example, you could always use pounds and then just convert to kilograms when needed (or vice versa)

Maybe something like this:

1
2
3
4
5
Weight myweight;
myweight.SetPounds( 150 );

cout << "My weight in pounds is " << myweight.GetPounds() << ".\n";
cout << "My weight in kilograms is " << myweight.GetKilos() << ".\n";
Are theese functions part of a class?
If so, you're missing a part of the definition (the name eludes me, but heres an example):
1
2
3
4
5
6
7
8
9
class A
{
    void func();
};

void A::func()
{
    std::cout << "properly declared function!\n";
}
How about I just post my code?

weight.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
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
117
118
119
120
121
122
123
124
125
/*-- Weight.cpp------------------------------------------------------------
 
   This file implements the Weight member functions.

-------------------------------------------------------------------------*/

#include <iostream>
using namespace std;

#include "weight.h"

/** Utility functions / Prototypes **/

double toKilos (double weight, char unit);
double toPounds (double &weight, char &unit);

//Definition of default constructor

Weight::Weight()
	: myHeavy(120.0), myUnit('L')
{
}

//Definition of explicit value constructor

Weight::Weight(double initHeavy, char initUnit){
	if(initHeavy > 0 && (initUnit == 'L' || initUnit == 'K')){
		myHeavy = initHeavy;
		myUnit = initUnit;
	} else
		cerr << "Invalid initial values. Sorry." <<endl;
}

// Definition of getHeavy

unsigned Weight::getHeavy() const {
	return myHeavy;
} 

//Definition of getUnit

unsigned Weight::getUnit() const {
	return myUnit;
}

// Definition of the display function 

void Weight::display(ostream & out) const
{
out << myHeavy << " " << myUnit;
}

// Definition of the read function

void Weight::read(istream &in)
{
	unsigned heavy; //local variables to check for class invariants before putting in members
	char unit;
	
	in >> heavy >> unit;
	//Check class invariants
	if(heavy > 0 && (unit == 'L' || unit == 'K')){
		myHeavy = heavy;
		myUnit = unit;
	}
	else
		cerr << "Invalid inputs"<<endl;
}

// Definition of operator<<()
ostream & operator<<(ostream & out, const Weight &w)
{
  w.display(out);
  return out;
}

//Definition of operator>>()
istream & operator>>(istream &in, Weight &w)
{
	w.read(in);
	return in;
}


// Definition of set function

void Weight::set(unsigned weight, char unit)
{   
//Check class invariants 
	if ((unit == 'L' || unit == 'K') && weight > 0){
		myHeavy = weight;
		myUnit = unit;
	} else
		cerr << "WARNING: Unable to set weight to these values. " <<endl;
}

// Relational Operators

bool operator<(const Weight &w1, const Weight &w2){
	return w1.getHeavy() < w2.getHeavy();
}

bool operator>(const Weight &w1, const Weight &w2){
	return w1.getHeavy() > w2.getHeavy();
}

bool operator!=(const Weight &w1, const Weight &w2){
	return w1.getHeavy() != w2.getHeavy();
}

bool operator==(const Weight &w1, const Weight &w2){
	return w1.getHeavy() == w2.getHeavy();
}

// Definitions of Ultility Functions
double toKilos(double weight, char unit){      //converts from pounds to Kilos
	myUnit = 'K';
	return (weight * .4536);
}


double toPounds(double weight, char unit){      //Converts from kilos to pounds
	myUnit = 'L';								//Changes unit to L for pounds
	return (weight / .4536);                    //Returns converted weight
}


weight.h
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
/*----weight.h-----------------------------------------------------------
	This header file defines the data type weight and contains 
	the following basic operations:

	Default constructor - sets "heavy" to a default value of 120.0 and unit to 'L'
	Explicit value constructor
	2 Acessors -
		getHeavy() - returns weight statistic
		getUnit() - returns unit of weight
	1 Mutator function -
		set - to set value of data members
	1 Display function -
		displays data
	Overload operator function - 
		overloads "<<" operator.
	1 Utility functions -
		Converts a unit of weight to the other.

-------------------------------------------------------------------------*/

#include <iostream>
using namespace std;

#ifndef WEIGHT
#define WEIGHT

class Weight
{
	public:
/******Function Members******
*******Class Constructors***/

	Weight();           //Default constructor

	Weight(double initHeavy, char initUnit);
 
/******ACESSORS********/

	unsigned getHeavy() const; //retrieves weight value for "Heavy"
	unsigned getUnit() const;  //retrieves unit

/*****MUTATOR**********/
	void set(unsigned weight, char unit);   //sets data members of the weight object to specified values

/*******DISPLAY********/
	void display(ostream & out) const;   //Displays weight using output stream out

/*******I/O************/
	void read(istream & in);
	
	private:
/********DATA MEMBERS******/
	double myHeavy;
	char myUnit;
}; //end of class declaration

ostream & operator<<(ostream & out, const Weight &w);  //overloads Output operator "<<"

bool operator<(const Weight &w1, const Weight &w2);  //Determines weather a weight is less than another

bool operator>(const Weight &w1, const Weight &w2);  //Determines weather a weight is greater than another

bool operator!=(const Weight &w1, const Weight &w2);  //Determines if one weight does not equal another

bool operator==(const Weight &w1, const Weight &w2);  //Determines if one weight is equal to another.

#endif


Also, should I worry about this?

weight.cpp: In member function `unsigned int Weight::getHeavy() const':
weight.cpp:37: warning: converting to `unsigned int' from `const double'
Ah, I see, well, myUnit is undefined in the functions toKilos and toPounds, perhaps you meant unit?
(And no, theres not really much to worry about in that warning)
yeahg/ Basically what I was trying to do was change "myUnit" so that in the main function, if I used the function, I would print out the correct answer. For example, in my main program, I would have something like this:

cout<< toKilos(120, 'L') << unit<< endl;

Although thinking about it now, I'd first get the unit using the getunit() member function, wouldn't I? Then somehow store it, and print it out?

getunit() should also return a reference to the char or you would end up changing a copy and throwing it away afterwards (this would also solve the storing problem since its being held by the weight object).
Topic archived. No new replies allowed.