Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question