String Initialization in Constructor

Sep 9, 2015 at 11:31am
Hi guys, So now I've this class where I enter strings and int input. It is supposed to set this variables and print them out but for some reason (a bug somewhere), It cannot output anything. Please may you help me pick my error

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

class Module
    {
        public:

	 void setObj(string, string, string, int);//mutator
        void getObj(string&, string&, string&, int&)const;// accessor
	 void print() const;

	 Module();//default constructor
        Module(string , string ,string, int);



	 private:
        string moduleName;
        string moduleCode;
        string lecturer;
        int nrStudent;
    };

void print(Module);
void fill (Module);

int main()
    {
        Module moduleOne;
	  print(moduleOne);
        fill(moduleOne);
        print(moduleOne);

	return 0;

    }


void print(Module modOne)
{

 	cout<<"Module Information "<<endl;
	modOne.print();
	cout<< endl;
}


void fill(Module modOne)
	{


    string one, two, three;
    int four;
    cout<< "Enter Module name"<<endl;
    getline(cin, one, '\n');
    cout<< "Enter Module code"<<endl;
    getline(cin, two, '\n');
    cout<< "Enter Lecturer"<<endl;
    getline(cin, three, '\n');;
    cout<< "Enter Student Number"<<endl;
    cin>> four;

	modOne.setObj(one, two, three, four);
    cout<<endl;
}



Module::Module()
{


moduleName;
moduleCode;
lecturer;
nrStudent;
}



Module::Module(string one, string two, string three, int four)
{
	setObj(one, two, three, four);
}
void Module::setObj(string one, string two, string three, int four)
{
    moduleName=one;
    moduleCode=two;
    lecturer=three;
    nrStudent=four;
}

void Module::getObj(string &one, string &two, string &three, int &four) const
{
one =moduleName;
two = moduleCode;
three=lecturer;
four=nrStudent;
}

void Module::print()const
{

 cout<<"Module name is :";
 cout<<moduleName<<endl;

 cout<<"Module code :";
 cout<<moduleCode<<endl;

 cout<<"Lecturer :";
 cout<<lecturer<<endl;

  cout<<"Number of students :";
 cout<<nrStudent<<endl;

}


Sep 9, 2015 at 12:23pm
closed account (SECMoG1T)
void fill (Module);
adds information to a copy of module object which will be destroyed immediately the function is out of scope. advice: pass the object by reference or make 'fill' a member function.

You need to revisit your default constructor--it does nothing, in your second constructor calling a member function on line 84 adds unnecessary overhead, you might wanna use initializer list in your constructor instead.

1
2
3
4
Module::Module(string one, string two, string three, int four)
   :moduleName(one),moduleCode(two),lecturer(three), nrStudent(four)
{
}


Last edited on Sep 9, 2015 at 12:38pm
Sep 9, 2015 at 12:31pm
The problem is on line 26/49: You pass modOne by value so that it is a coyp within fill(...). This copy will be discarded at the end of fill(...).

You need to pass modOne by reference: void fill(Module &modOne). Note that you need to modify line 26 as well.
Sep 9, 2015 at 12:54pm
Ahaaaa.....thank you coder. Such a nice 'Gotcha..." It indeed needed to be passed by reference.

Thank so much. Will keep on improving
Last edited on Sep 9, 2015 at 12:55pm
Sep 9, 2015 at 1:45pm
@DesmondM
Module::Module()
{


moduleName;
moduleCode;
lecturer;
nrStudent;
}


this is not what I call initializing attributes.. string is a class , so it has also a constructor , do this instead plz :

1
2
3
4
5
6
Module::Module()
: moduleName("")
, moduleCode("")
, lecturer("")
, nrStudent(0)
{}
Sep 9, 2015 at 2:12pm
Thank you Ericool...noted....tried it and it's working.
Sep 10, 2015 at 11:01am
Ur welcome
Topic archived. No new replies allowed.