A
A
Alexey Zus2015-08-26 18:50:24
Programming
Alexey Zus, 2015-08-26 18:50:24

Calling a function with arguments?

I call the displayMessage function element of the myGradeBook object with arguments.
Why is it necessary to enter the string nameOfCourse into the displayMessage function of the GradeBook class, if we already called it in main with such arguments? In theory, the arguments should be passed when called from main. And why string nameOfCourse and not nameOfCourse(class object name in main)?
I recently started learning C++ and it's a matter of my logic. Thanks in advance!

#include <iostream> //	Директива препроессора	ввод\вывод данных
using std::cout;	
using std::cin;
using std::endl;

#include <string>	//	программа использует класс string из стандартной библиотеки C++
using std::string;
using std::getline;

class GradeBook
{
  public:
    
    void displayMessage(string nameOfCourse)	//	почему string + nameOfCourse?
  {

    cout << "Welcome to GradeBook for\n" << nameOfCourse << "!" << endl;
  
  }

};

int main()
{
  
  string nameOfCourse;	
  GradeBook myGradeBook;
    
    cout << "Please enter the course name:" << endl;
    getline(cin, nameOfCourse);	//	функция с аргументами
    cout << endl;

  myGradeBook.displayMessage(nameOfCourse);	//	вызываем функцию и передаем аргументы

return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sitev_ru, 2015-08-26
@lexzus07

>>Why do you need to enter string nameOfCourse in the displayMessage function of the GradeBook class
Because this is a function description, we set the string type of the parameter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question