Pointer function

Pages: 12
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.


Can someone explain me, what I am suppose to be doing?
It's pretty clear already. You can use http://www.cplusplus.com/reference/cstring/strcpy/ to copy the string.
Am I suppose to be asking for a name?

Can someone please write this in code for me?
Write your own and post it here, then we can help you understand any problem you have.
This is what I have so far:

void getName(char *name)
{
return m_sName[128];
}

void setName(char *name)
{
name=m_sName;
}
Please remember to use code tags.

getName shouldn't return anything (and anyway, you're returning an element of the string, a char, not the string itself). It's just asking you to copy the argument into the member.
If you look at the link I posted you'll see strcpy copies one string into another.
In getName the source string is the member, and the destination is the argument. In setName the roles are reversed but the procedure is the same.

Your only concern is if the arrays aren't long enough to store all the characters, but I assume this exercise doesn't ask to deal with that. If you're curious you can look to strncpy which allows specifying a maximum of characters to copy.
I am sorry I don't know what code tags are

void getName(char *name)
{
strcpy(name, m_sName);
}

So am I coping name into m_sName?
1
2
3
4
void getName(char *name)
{
strcpy(name, m_sName);
}


Does this look good?

I really appreciate your help.
Last edited on
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
getName was correct on your first attempt. setName has to be corrected in turn.
*etCost fuctions have the same problem: you have the logic backwards.
The basic logic is:
set functions are meant to set the value of m_ variables.
get functions are meant to return the value of a m_ variable OR set the value of the argument (only in the case it is a pointer or reference though). Note that these two cases are not mutually exclusive.
strcpy is for strings to copy from source to destination but how do you do that for a double?

1
2
3
4
5
6
7
8
void getCost(double *c)
{
	strcpy(*c, m_dCost);
}
	void setCost(double c)
	{
		strcpy(m_dCost, c);
	}
2.2.1 The class shall contain the following private variables: (1) a character array called m_sName, (2) a long called m_lStockNum, (3) an integer called m_iClassification, (4) a double called m_dCost, and (5) an integer called m_iCount. Details of these variables are defined below.
2.2.1.1 m_sName--This character array shall be 128 bytes in length and will be used to store the name of a book or periodical.
2.2.1.2 m_lStockNum--This long shall be used to store a unique stock number for the book or periodical. This will be used as the key in any store, search, or retrieval operation.
2.2.1.3 m_iClassification--This integer will be used to code specific information about the book or periodical.
2.2.1.4 m_dCost--This double will be used to store the cost of this book or periodical.
2.2.1.5 m_iCount--This integer will be used to store the number of copies of this item currently in the inventory.
2.2.2 The class shall contain the following public functions: a constructor and destructor and get and set functions for each of the variables. These functions shall work as described in the following paragraphs.
2.2.2.1 BookRecord()--The default constructor shall set the member variables to the following initial values: m_sName = "", m_lStockNum = 0, m_iClassification = 0, m_dCost = 0.0, and m_iCount = 0.
2.2.2.2 BookRecord(char *name, long sn, int cl, double cost)--This constructor shall set the member variables to the values passed into the function and initialize the m_iCount variable to one (1).
2.2.2.3 ~BookRecord()--The destructor shall be an empty, place-holder function.



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
class BookRecord
{
private:
	char m_sName[128];
	long m_lStockNum;
	int m_iClassification;
	double m_dCost;
	int m_iCount;

public:
BookRecord()
{
	m_sName[128];
	m_lStockNum = 0;
	m_iClassification = 0;
	m_dCost = 0.0;
	m_iCount = 0;
}
BookRecord(char *name, long sn, int cl, double cost)
{
	m_iCount;
}
~BookRecord()
{
}



This is suppose to be my header file I am wondering if this is correct?
Both constructors have errors or are incomplete. You should prefer initializer lists in them.
Your compiler version affects your options. See http://www.informit.com/articles/article.aspx?p=1852519

Copying doubles:
This is correct: m_dCost = *c;
It changes the m_dCost by copying value from the memory location that the c is pointing to. Is that a "set" or a "get"?

String copy:
1. What if a pointer is null?
2. You got a hint about: http://www.cplusplus.com/reference/cstring/strncpy/
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.

This is my other file


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
# include <iostream>
# include <string>
# include "BookRecord.h"
using namespace std;

void getName(char *name)
{
	strcpy(name, m_sName);
}
	
void setName(char *name)
{
	strcpy(m_sName, name);
}
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;
 }
 void printRecord()
 {
 }



How do I initialize both constructor and destructor
The destructor is suppose to be empty.
How do I initialize both constructor and destructor

There is no "initialization" in destructors.

I did give an URL to informit site. Did you read?
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
class BookRecord
{
private:
	char m_sName[128];
	long m_lStockNum;
	int m_iClassification;
	double m_dCost;
	int m_iCount;

public:
BookRecord()
{
	m_sName[128];
	m_lStockNum = 0;
	m_iClassification = 0;
	m_dCost = 0.0;
	m_iCount = 0;
}
BookRecord(char *name, long sn, int cl, double cost)
{
	m_iCount=1;
	m_sName[128]=*name;
	m_lStockNum=sn;
	m_iClassification=cl;
	m_dCost=cost;
}
~BookRecord()
{
}
};


Ok there is no initialization in destructors, so does my header file look good?

If yes what about my cpp file.
No, it does not. For one, you don't initialize anything in the constructors. You only assign new values to already default initialized objects.

Line 13. You are dereferencing element that does not even exist in the array. That is an out-of-range error. You were supposed to ensure that the array contains a specific string, but you don't set any value.

Line 22 does not copy strings. Earlier parts of this thread were already discussing the art of copying C-strings. You should know the subject by know.

I did give an URL to informit site that shows initialization. Did you read any of it?
Last edited on
I cant print the name of the book my code but I can print everything else
My cpp file

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
#include "BookRecord.h"

BookRecord::BookRecord()
{
 	m_sName[128]=NULL;
	m_lStockNum=0;
	m_iClassification = 0;
	m_dCost = 0.0;
	m_iCount = 0;
}
BookRecord::BookRecord(char *name, long sn, int cl, double cost)
{
	m_iCount=1;
	m_sName[128]=*name;
	m_lStockNum=sn;
	m_iClassification=cl;
	m_dCost=cost;
}
BookRecord::~BookRecord()
{
}

void BookRecord::getName(char *name)
{
	strcpy(name, m_sName);
}
	
void BookRecord::setName(char *name)
{
	strcpy(m_sName, name);
}
long BookRecord::getStockNum()
 {
	 return m_lStockNum;
 }
void BookRecord::setStockNum(long sn)
{
	m_lStockNum = sn;
}
void BookRecord::getClassification(int& cl)
{
	cl=m_iClassification;
}
void BookRecord::setClassification(int cl)
{
	m_iClassification= cl;
}
void BookRecord::getCost(double *c)
{
	*c = m_dCost;
}
void BookRecord::setCost(double c)
	{ 
		m_dCost = c;
	}
int BookRecord::getNumberInStock()
 {
	 return m_iCount;
 }
 void BookRecord::setNumberInStock(int count)
 {
	 count = m_iCount;
 }
void BookRecord::printRecord()
 {
	 //cout << "Name: " <<m_sName <<"\n";
	 printf("Name: %s", m_sName);
	 cout<<endl;
	 cout<<"Stock Number: "<<m_lStockNum<<"\n";
	 cout<<"Class: " <<m_iClassification<<"\n";
	 cout<<"Cost: $"<<m_dCost<<"\n";
	 cout<<"In Stock: "<<m_iCount<<"\n";
 }


My header file

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
#pragma once
#include <iostream>>
#include <string>

using namespace std;

class BookRecord
{
private:
	char m_sName[128];
	long m_lStockNum;
	int m_iClassification;
	double m_dCost;
	int m_iCount;

public:
BookRecord();

BookRecord(char *name, long sn, int cl, double cost);

~BookRecord();

void BookRecord::getName(char *name);

	
void BookRecord::setName(char *name);

long getStockNum();
 
void setStockNum(long sn);

void getClassification(int& cl);

void setClassification(int cl);
void getCost(double *c);

void setCost(double c);

int getNumberInStock();

 void setNumberInStock(int count);

void printRecord();

};


My main test function, which I won't turn it

#include "BookRecord.h"

int main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
	char *bName="C Plus Plus";
	BookRecord *Ana = new BookRecord(bName, 1, 1, 50.9);
	/*char * bookName="";
	int clNo;
	double c;
	Ana->getClassification(clNo);
	Ana->getCost(&c);
	Ana->getName(bookName);*/
	cout << "Printing using printRecord() Function" << endl;
	Ana->printRecord();

	/*cout << endl << "Printing using getter properties" <<endl;
	cout << bookName << endl;
	cout<< clNo << endl;
	cout << c << endl;
	cout << Ana->getNumberInStock() << endl;*/
	return 0;
}
Don't get any errors when I build it but when I run it the window opens and closes really fast.
Pages: 12