Functions / Get and set

void getName(char *name), void setName(char *name)--The getName() function shall be defined as a pointer function. A call to this function will copy the member variable m_sName into the character array pointed to by the function argument. The setName() function will copy the function argument into the member variable m_sName.
2.2.2.5 long getStockNum(), void setStockNum(long sn)--The getStockNum() function shall return the value stored in the member variable m_lStockNum. The setStockNum() function will copy the function argument into the member variable m_lStockNum.
2.2.2.6 void getClassification(int& cl), void setClassification(int cl)--The getClassification() function shall be defined as a reference function. A call to this function will copy the member variable m_iClassification into the interger variable referenced by the function argument. The setClassification() function will copy the function argument into the member variable m_iClassification.
2.2.2.7 void getCost(double *c), void setCost(double c)--The getCost() function shall be defined as a pointer function. A call to this function will copy the member variable m_dCost into the double variable pointed to by the function argument. The setCost() function will copy the function argument into the member variable m_dCost.
2.2.2.8 int getNumberInStock(), void setNumberInStock(int count)--The getNumberInStock() function shall return the value stored in the member variable m_iCount. The setNumberInStock() function will copy the function argument into the member variable m_iCount.

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
void getName(char *name)
{
	strcpy(m_sName, name);
}
	
void setName(char *name)
{
	strcpy(name, m_sName);
}
long getStockNum()
 {
	 return m_lStockNum;
 }
	void setStockNum(long sn)
	{
		m_lStockNum = sn;
	}
	void getClassification(int& cl)
	{
		cl=m_iClassification;
	}
	void setClassification(int cl)
	{
		m_iClassification= cl;
	}
void getCost(double *c)
{
	m_dCost = *c;
}
	void setCost(double c)
	{
		c = m_dCost;
	}
 int getNumberInStock()
 {
	 return m_iCount;
 }
 void setNumberInStock(int count)
 {
	 count = m_iCount;
 }




How does this look, I know its not correct but I am not sure how to fix it. Any tips would be helpful. Thanks
You could make the get* functions const, if allowed by instructions.

When you get a pointer ... what if I call foo.getName( 0 );? Likewise, you do know the size of m_sName, so you could guard against writing too much to it.

The *Cost ... setting or getting?


Edit: Why a repost, rather than continuing with http://www.cplusplus.com/forum/beginner/143082/
Last edited on
I am sorry, I was tired.

Original http://www.cplusplus.com/forum/beginner/143082/
Topic archived. No new replies allowed.