I tried to pass to IDE a code I found in a book and some doubts came up.
Code:
newApplication.cpp
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
#include "GradeBook.h"
int main()
{
GradeBook gradeBook1("CS101 Introduction to C++ Programming");
GradeBook gradeBook2("CS102 Data Structures in C++");
cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
<< "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
<< endl;
return 0;
}
GradeBook.h
#pragma once
#include <string>
using std::string;
class GradeBook
{
public:
GradeBook(string name);
void setCourseName(string name);
string getCourseName();
void displayMessage();
private:
string courseName;
};
GradeBook.cpp
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
#include "GradeBook.h"
GradeBook::GradeBook(string name)
{
setCourseName(name);
}
void GradeBook::setCourseName(string name)
{
courseName = name;
}
string GradeBook::getCourseName()
{
return courseName;
}
void GradeBook::displayMessage()
{
cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;
}
1) How does the compiler "know" that the method implementation is in GradeBook.cpp
and that in main()
there is only the GradeBook.h
?
2) The application only worked after I included #include "stdafx.h"
in the file
GradeBook.cpp
. Why does it happen?
Visual Studio 2017 folder structure
-Dependencias Externas
-Arquivos de Cabeçalho
*GradeBook.cpp
*GradeBook.h
*resource.h
*stdafx.h
*targetver.h
-Arquivos de Origem
*newApplication.cpp
*stdafx.cpp
-Arquivos de Recurso
*newApplication.rc