2

Just started coding in C++. I want to understand how class of C++ works when I want to define a variable and put it out with a different name.

Apologies if the question doesn't make sense. The code I have is from a well-known textbook. I'm not getting any error messages; I'm just trying to understand how works in C++. I have some experience in Python and this process confused me.

#include <iostream>
#include <string> //program uses C++ standard string class
using namespace std;

// Gradebook class definition
class GradeBook 
public: 
    void displayMessage( string courseName ) 
    {
        cout << "Welcome to the Grade Book for\n" << courseName << "!" 
            << endl;
    }
};

int main()
{
    string nameOfCourse;
    GradeBook myGradeBook; 
    // prompt for and input course name
    cout << "Please enter the course name: " << endl;
    getline( cin, nameOfCourse );  
    cout << endl; 
    myGradeBook.displayMessage( nameOfCourse ); 
}

The output comes out fine.

Notice two variables: courseName and nameOfCourse. The textbook says that courseName variable transformed into nameOfCourse variable on line 17, where it goes:

        string nameOfCourse;

Can you help me understand how this transformation occurs?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
ozengnr
  • 53
  • 4
  • 3
    If you don't mind me asking, what book are you using? – NathanOliver Jun 26 '19 at 16:28
  • 1
    One names your variable declared in `main()`, the other names the parameter of `displayMessage()`. There's no _"transformation"_. Isn't that working the same in Python? – πάντα ῥεῖ Jun 26 '19 at 16:28
  • 2
    Consider reading text-books from start to finish, not from arbitrary place. Or, alternatively, choose one from the community moderated list of [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). The variable names are only meant for the reader, and are not preserved in the compiled code. – Algirdas Preidžius Jun 26 '19 at 16:29
  • 1
    @NathanOliver I'm taking an online C++ course from a university. The textbook they use is Paul Deitel, Harvey Deitel, C++: How to Program – ozengnr Jun 26 '19 at 16:30
  • there is no _transformed_ variable, is just name of variable/parameter, and both are typed _string_ so not even type conversion. The names you use is only _syntactic sugar_. You also have var in Python. If you replace all _courseName_ by _turlututu_ in the source you will have the same result – bruno Jun 26 '19 at 16:36
  • I've read said textbook and I don't remember this terminology being in there. Granted it's been a good while. – chris Jun 26 '19 at 16:41
  • Thank you all for your help & patience. Hopefully I'll come up with better questions in the future when I get better at this. – ozengnr Jun 26 '19 at 16:45
  • @πάνταῥεῖ I think in Python it is always like passing by reference – Aykhan Hagverdili Jun 26 '19 at 16:54
  • @Ayxan How does that affect the naming? – πάντα ῥεῖ Jun 26 '19 at 16:55
  • @πάνταῥεῖ it doesn't. Naming is exactly like in C++; functions have different scopes. But "transforms" may be more accurate Python's case because nothing is copied unless you specifically do copying – Aykhan Hagverdili Jun 26 '19 at 19:54

3 Answers3

3

If courseName was a reference (std::string&) then it would be correct to say that in line 17, courseName is bound to nameOfCourse (in other words, for the duration of that displayMessage call, courseName would be just another name for nameOfCourse).

But 1. that is called "reference binding", not "variable transformation", and 2. that's not what is happening here - nameOfCourse is copied to courseName when displayMessage is called: The copy constructor of std::string is used to create a copy of nameOfCourse.

Variables (and/or variable names) are not "transformed" in C++ (especially not in the context of calling by value), so unless there is a translation/transcription/general communication problem interfering here, the book is simply wrong.

Max Langhof
  • 23,383
  • 5
  • 39
  • 72
3

Let's break apart this code to understand what it's doing:

#include <iostream>
#include <string> //program uses C++ standard string class
using namespace std;

// Gradebook class definition
class GradeBook 
public: 
    void displayMessage( string courseName ) 
    {
        cout << "Welcome to the Grade Book for\n" << courseName << "!" 
            << endl;
    }
};

int main()
{
    string nameOfCourse;
    GradeBook myGradeBook; 
    // prompt for and input course name
    cout << "Please enter the course name: " << endl;
    getline( cin, nameOfCourse );  
    cout << endl; 
    myGradeBook.displayMessage( nameOfCourse ); 
}

Starting in the main() method, when your program runs the following things happen:

int main()
{
    string nameOfCourse;

At this point, the program has begun, and it's initialized a variable of type string. This invokes the default constructor for string, which creates a string with size 0 (so an empty string).

In order to create the string, the program allocates space for the string on the program's stack. This space is what the default constructor uses when initializing the string.

Once the program is running, the names chosen for the variables don't matter at all, and nowhere does it keep track of what you named a variable.

    GradeBook myGradeBook;

Here, you declare that a GradeBook object exists. Because GradeBook is empty, in an optimized program nothing actually happens. It's never "created", because there's nothing to create. The compiler might direct the program to assign GradeBook an address based on the current stack location, but because your code doesn't use the address of myGradeBook anywhere, that might not happen, either.

    cout << "Please enter the course name: " << endl; 

The first part (cout << "Please enter the course name: ") sends that message to standard output. The << endl portion adds a newline, and flushes the buffer used by standard output. endl is actually a function, and so this would be equivalent to writing endl(cout << "Please enter the course name: ").

    getline(cin, nameOfCourse);

This reads data from standard input until the line ends. It puts that data in nameOfCourse. In order to put that data in nameOfCourse, the program modifies the memory location it assigned to nameOfCourse by calling the appropriate string member functions (probably the append member function, which appends text to a string).

    cout << endl; 

Adds a newline and flushes the buffer again.

    myGradeBook.displayMessage( nameOfCourse ); 

The following things happen in order to call displayMessage: - If there's any data in the registers, the program saves it to the stack (so that the registers are freed up) - The program makes space on the stack for courseName - The program calls the string type's copy constructor, creating a new string in the space allocated for courseName - The program jumps to the location where the machine code for displayMessage begins - displayMessage runs - displayMessage returns - the main() method resumes

At that point, the program exits because there's nothing else to do.

Alecto Irene Perez
  • 10,321
  • 23
  • 46
2

A function parameter can have any name.

int display_int(int the_int) { cout << the_int << endl; }

You can call this function in any of the following ways:

display_int(5);
int hardyharhar = 42069;
display_int(hardyharhar);
display_int(999-333);
MPops
  • 355
  • 3
  • 8