Hi there,
I am trying to get my head around this simple program, but there are things that really don't make sense...

here's the program:

// Fig. 3.5: fig03_05.cpp
// Define class GradeBook that contains a courseName data member
// and member functions to set and get its value; 
// Create and manipulate a GradeBook object.
#include <iostream>
using std::cout; 
using std::cin;
using std::endl;

#include <string> // program uses C++ standard string class
using std::string;
using std::getline;

// GradeBook class definition
class GradeBook
{
public:
   // function that sets the course name
   void setCourseName( string name )
   {      
      courseName = name; // store the course name in the object
   } // end function setCourseName
   
   // function that gets the course name
   string getCourseName() 
   {
      return courseName; // return the object's courseName
   } // end function getCourseName

   // function that displays a welcome message
   void displayMessage()
   {
      // this statement calls getCourseName to get the 
      // name of the course this GradeBook represents
      cout << "Welcome to the grade book for\n" << getCourseName() << "!" 
         << endl;
   } // end function displayMessage
private:
   string courseName; // course name for this GradeBook
}; // end class GradeBook  

// function main begins program execution
int main()
{
   string nameOfCourse; // string of characters to store the course name
   GradeBook myGradeBook; // create a GradeBook object named myGradeBook
   
   // display initial value of courseName
   cout << "Initial course name is: " << myGradeBook.getCourseName() 
      << endl;

   // prompt for, input and set course name
   cout << "\nPlease enter the course name:" << endl;
   getline( cin, nameOfCourse ); // read a course name with blanks
   myGradeBook.setCourseName( nameOfCourse ); // set the course name

   cout << endl; // outputs a blank line
   myGradeBook.displayMessage(); // display message with new course name
   return 0; // indicate successful termination
} // end main

Now:
1) Line 19-22: why do we need in line 21

courseName = name;

?
2) line 49:why do we need

<< myGradeBook.getCourseName()

??

In general, I have been looking at this program for over3 hours and I still don't seem to get it completely. Is there any good soul who is willing to explain it to me line by line? I know it might sound as a waste of time for you but I am trying to "get it"! :-/
I think I need some sleep now :zzz:, thanks

Your class Gradebook has a private value named courseName. The only way to put a value in it is to call setCourseName( name ) to load the value name into it.

And since it's a private value you must call getCourseName() to retrieve it.

I see, but I think the problem is more general for me, as in I don't get how the the calls work. Let me get this straight:
Lines 53-55 call the function on lines 19-22 and in particular they assign the string variable "nameOfCourse" - declared in line 45 - to line 19 so that line 19 "becomes"

#
void setCourseName( string nameOfCourse)

, but then...I get lost. Since in line 21 - which I guess is an assignment statement -

courseName = name

line 19 becomes what?

Then let's jump to lines 49-50. You said that I need that line because the data member courseName is private: now, private values cannot be called outside the class they are declared in, so the function "main" cannot call that private value and uses the "get" function to call it, isn't it correct? But what happens when line 49 calls the "get" function (I mean in the code) apart from getting the name of the course to be displayed on the screen?

I think I am failing to understand the interrelashionships between functions which is why I might need a detailed explanation of what the the above mentioned functions do in this program...
Thanks a lot

First, I would like to emphasize that program lines don't change. What changes is the information contained within the variables used/defined by those lines. Your code tells the computer how to use and change that information.

Second, I think it might be advisable for you to read some tutorials:

Functions

Classes

Edited 6 Years Ago by Fbody: n/a

Maybe an easier class will help see below :

class Integer{
private:
  int myInt; //an integer variable
public:
 Integer(){ myInt = 0; } //a constructor
 Integer(const int initValue){ myInt = initValue; } //another constructor
 void setMyInt(int value){ myInt = value; } //set myInt to some value
 int getMyInt(){ return myInt; } //get the current value that myInt has
};

The getMyInt and setMyInt functions are called getters and setters.
What they do is get the current value of myInt, and set the new value
to myInt. The reason why we make get and set function is because
it enables us to control what happens when they use the method
getMyInt and setMyInt. As you gain experience, you will know what that
means.

Thanks for that guys. I will certainly read the tutorials, maybe they will clarify the issue. I think I used the wrong choice of words when I said that the code changes. You're quite right, I think I get confused with how the variables change.
Is there any chance you guys could quickly explain how the variables change in that little program I got from my book (Deitel and Deitel C++ how to program), because it is not really clear from the book, or do you think I should read the tutorials first?

I'm going to break the code into sections for you. Maybe it will help. But read the tuts first.

First, the class definition. This says what the parts of a GradeBook are and what it is capable of doing:

// GradeBook class definition
class GradeBook
{
public:
   // function that sets the course name
   void setCourseName( string name )
   {      
      courseName = name; // store the course name in the object
   } // end function setCourseName
   
   // function that gets the course name
   string getCourseName() 
   {
      return courseName; // return the object's courseName
   } // end function getCourseName

   // function that displays a welcome message
   void displayMessage()
   {
      // this statement calls getCourseName to get the 
      // name of the course this GradeBook represents
      cout << "Welcome to the grade book for\n" << getCourseName() << "!" 
         << endl;
   } // end function displayMessage
private:
   string courseName; // course name for this GradeBook
}; // end class GradeBook

The functions setCourseName (lines 6-9), getCourseName (lines 12-15), and displayMessage (lines 18-24) are all functions that a GradeBook object can execute. On line 26 a variable courseName is defined, it holds information about the individual GradeBook object. Since courseName is in the "private:" section of the class, it can only be used directly by functions that are part of GradeBook, it can not be used directly by, for example, your program's main() function.

Second, the definition of main(). This is the part that begins, manages, and ends program execution.

// function main begins program execution
int main()
{
   string nameOfCourse; // string of characters to store the course name
   GradeBook myGradeBook; // create a GradeBook object named myGradeBook
   
   // display initial value of courseName
   cout << "Initial course name is: " << myGradeBook.getCourseName() 
      << endl;

   // prompt for, input and set course name
   cout << "\nPlease enter the course name:" << endl;
   getline( cin, nameOfCourse ); // read a course name with blanks
   myGradeBook.setCourseName( nameOfCourse ); // set the course name

   cout << endl; // outputs a blank line
   myGradeBook.displayMessage(); // display message with new course name
   return 0; // indicate successful termination
} // end main

On line 5, a variable is declared called "myGradeBook". This variable is of type GradeBook and holds inside itself the variables that are part of the GradeBook class defined above. It also has access to the functions that are part of the class. If main() needs to either change or use courseName it must call setCourseName() or getCourseName() respectively. These functions allow main() indirect access to courseName because it can't access it directly.

On line 14, main() calls setCourseName. When it does this, the information contained in the variable "nameOfCourse" is copied into the variable "name", which is part of the function. Once this is done, the command(s) in setCourseName copy the information in "name" into "courseName". After that, setCourseName is done and main continues to go about its business.

Edited 6 Years Ago by Fbody: n/a

Comments
Full marks for the effort !!

Great thanks for that, it is much clearer now, and I will definitely read the tutorials, I am sure they will help.
One more thing: you described line 14 in main() really clearly, what happens instead with line 8 in main()? What's the effect of that call?
Thanks

Great thanks for that, it is much clearer now, and I will definitely read the tutorials, I am sure they will help.
One more thing: you described line 14 in main() really clearly, what happens instead with line 8 in main()? What's the effect of that call?
Thanks

It's very similar to line 14 except it works the other direction. When you call getCourseName() it uses a return statement to give you the information currently contained in courseName instead of putting new information into it. That particular line is an output statement (because of the cout << ... ) rather than an assignment statement so instead of storing the name in a variable, it displays it on your screen.
You could just as easily do:

string currentCourseName = myGradeBook.getCourseName();
cout << "Initial course name is: " << currentCourseName << endl;

This version is less efficient, but it accomplishes the same task. This version would store the courseName variable to currentCourseName then print it on your screen instead of printing it directly to your screen.

Wicked, thanks a lot, now I think it is clearer.
And now, tutorial time!!
Cheers

This article has been dead for over six months. Start a new discussion instead.