Quick Question about Classes

This code is from the book. I just want to ask why do we have to add the
myGradeBook.getCourseName() in the main function. The program will work without it. It says in the comments that it displays the initial value of courseName. Can someone explain it to me in more detail?? Thank you

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

using namespace std;

class GradeBook

{
public:
	
	void setCourseName(string name)
	{
		courseName=name;

	}
	string getCourseName()
	{
		return courseName;
	}	
	
	void displayMessage()
	{
		cout<<"Welcome to the Grade Book for\n"<<getCourseName()<<endl;
	
	}

private:
	string courseName;

	};

int main ( )
{
	string nameOfCourse;
	
	GradeBook myGradeBook;
	
	cout<<"Initial course name is: "<<myGradeBook.getCourseName()<<endl; //this

	cout<<"Please enter the course name"<<endl;
	getline(cin, nameOfCourse);

	myGradeBook.setCourseName(nameOfCourse);
	cout<<endl;

	myGradeBook.displayMessage(); 
}



output:

Initial course name is:

Please enter the course name:
CS101 Introduction to C++ Programming

Welcome to the grade book for
CS101 Introduction to C++ Programming!
Line 38 is simply showing you that courseName is empty.
i.e. courseName is a string and string's default constructor was called when myGradeBook was instantiated which initialized courseName to an empty string.
Topic archived. No new replies allowed.