class member functions with pointer parameters

I have this homework assignment that is giving me a lot of trouble right now. My teacher is very vague in class and hard to communicate with. I will try very hard to articulate my thoughts here. Here is the assignment:

1. (3pts) Given the following class header file, write the class’ source code for each of the accessor and mutator functions listed. (Pay attention to how the functions have listed their parameters, varying between passing by reference and by value.) Don’t forget to comment your code – it counts!

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
class Album {
private:
    char * artist;  // band or singer’s name
    char * title;   // title of the album
    int year_released;  // year the album is released
    char * record_label;    // name of company produced album
    int num_songs;  // number of songs on the album
    int num_minutes_long;   // length (mins) of album
    char * genre;   // genre of artist (eg, rock, pop, …)
public:
    //constructors
    Album();
    Album(char *& a, char *& t);

    //deconstructor
    ~Album();

    //accessors and mutators
    bool set_artist(char * a);
    bool set_title(char * t);
    bool set_year_released(int value);
    bool set_record_label(char *& label);
    bool set_num_songs(int value);
    bool set_num_minutes_long(int value);
    bool set_genre(char * g);
    bool get_artist(char *& a);
    bool get_title(char *& t);
    int get_year_released();
    bool get_record_label(char *& label);
    int get_num_songs();
    int get_num_minutes_long();
    bool get_genre(char *& g);};


Here is my work so far:
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
bool Album::set_artist(char * a)
{
 *artist = a;
}

bool Album::set_title(char * t)
{
*title =  t;
}

bool Album::set_year_released(int value)
{
year_released = value;
}   

bool Album::set_record_label (char *& label)
{
*record_label = label;
}

bool Album::set_num_songs(int value)
{
num_songs = value;
}
bool Album::set_number_minutes_long(int value)
{
num_minutes_long = value;
}
bool Album::set_genre(char * g)
{
*genre = g;
}
bool Album::get_artist(char *& a)
{
return artist;
}
bool Album::get_title(char *& t)
{
return title;
}
int Album::get_year_released()
{
return year_released;
}

bool Album::get_record_label(char *& label)
{
return *record_label;
}


The input will be an array. My questions: First, am I on the right track?

When using (char * a) for a function, for example, this is passing the address of a, correct? so then *artist=a; changes what the address of a points to?

also, the functions are bool when I would expect void. Why? for all of the set_" " functions, the parameter is *... but for set_record_label it is *&. That appears to be a mistake to me. Is that right?

what is the difference between *& and * as parameters?

Thank you for your time. I know there is a lot here.
Last edited on
Firstly: when copying a string, DO NOT just say:
1
2
char* a="Hello";
char* b=a;


Instead, use strcpy:
1
2
3
4
#include <cstring>
char* a="Hello";
char* b;
strcpy(b,a); // strcpy(char* dest, char* source) copies the string source into dest 


Second: char *& is an example of a reference - in this case a reference to a char*. When you pass a value to a function, eg
1
2
3
4
5
6
7
8
9
int myFunc (int a)
{
    a+=5;
    return a;
}

int y=5;
int x=myFunc(y);
cout << x << "\n" << y;
10
5

the function gets given a copy of y - so if it changes its copy it doesn't change y. However, if myFunc was passed a reference: eg
1
2
3
4
5
6
7
8
9
int myFunc (int& a)
{
    a+=5;
    return a;
}

int y=5;
int x=myFunc(y);
cout << x << "\n" << y;
10
10

the function gets given a reference to y - when it does anything to its reference, that thing is happening to y. So when it calls a+=5, what's really happening is y+=5

In your case (using Album::get_record_label as an example), char *& label means that you are being given a reference to the variable that will store a copy of record_label which the user can then work with. What the function should do is
1
2
3
4
5
6
bool Album::get_record_label (char*& label)
{
    if (/* record_label is not set */) return false;
    strcpy(label,record_label);
    return true;
}


(edit):
Third: Your constructor should set all the data members to values which noone will ever use - set all the char*'s to NULL and the int's to -1, for example.

Then, if one of the set_"" functions tries to set a char* to NULL or an int to -1, it returns false, or if one of the get_"" tries to get a value which hasn't been set, it returns false.

That's why most of the functions return bool
Last edited on
theranga wrote:
Firstly: when copying a string, DO NOT just say:

1
2
char* a="Hello";
char* b=a;


Instead, use strcpy:

1
2
3
4
#include <cstring>
char* a="Hello";
char* b;
strcpy(b,a); // strcpy(char* dest, char* source) copies the string source into dest  

I think I would prefer the first. At least that way you're not clobbering memory you don't own. If you're going to copy data from one memory location to another, make sure the area of memory you're copying to is valid.


drj3122 wrote:

When using (char * a) for a function, for example, this is passing the address of a, correct? so then *artist=a; changes what the address of a points to?

No. a is a pointer. It is passed by value. Pointers contain addresses. The address of the pointer, however is different from the address the pointer contains.

*artist = a ; means you're trying to stuff the address contained in a into the first char at the (invalid) memory address contained by artist
Last edited on
Thank you so much for the reply. It's starting to make sense. I'm going to put in those changes now and I'll come back if I have more questions.
No. a is a pointer. It is passed by value. Pointers contain addresses. The address of the pointer, however is different from the address the pointer contains.

*artist = a ; means you're trying to stuff the address contained in a into the first char at the (invalid) memory address contained by artist


Ok I understand the issue there but then what is the proper syntax? I'm trying to think of it but I'm stuck right now. is it as simple as using strcpy(artist, a) ?
Last edited on
Ok I understand the issue there but then what is the proper syntax? I'm trying to think of it but I'm stuck right now. is it as simple as using strcpy(artist, a) ?


First you must ensure, as mentioned before, that artist points to memory you can copy to.

As you have the class definition, I would expect dynamic memory allocation.
Topic archived. No new replies allowed.