Foreword
Today, we will continue to learn the C++ Programming. I believe in you already study it long times. I think Learning is one thing of yourself. Next, we start new travel about C++.
Text
The header file is Book.h, contents like the below codes.
#include<iostream>
#include<string>
using namespace std;
class Book{
public:
Book(string name){//constractor function
setSubject(name);
}
void setSubject(string name){
SubjectName = name;
}
string getSubjectName(){
return SubjectName;
}
void displayMessage(){
cout << "The Subject is: " << getSubjectName() << endl;
}
private:
string SubjectName;
};// end class Book
The day6.cpp file, Codes like the below contents:
#include<iostream>
#include "Book.h"
using namespace std;
int main(){
cout << "C++ is make me happy!!" << endl;
Book SubjectBook1( "Introduction to C++ Programming");
Book SubjectBook2("Scala Programming");
cout << "SubjectBook1 created for course: " << SubjectBook1.getSubjectName()
<< "\nSubjectBook2 created for course: " << SubjectBook2.getSubjectName()
// << SubjectBook1.displayMessage()
<< endl;
SubjectBook1.displayMessage();
}//end main
Running the day6.cpp,gain the below result:
From the above program, we know the Book.h file create one Constructor function when the Class is created. The day6.cpp file load the Book.h file contents. If we want change the variable SubjectName is failed. It’s like this tip:
error: 'std::string Book::SubjectName' is private within this context
. Because the private type couldn’t change from outsides Codes, it’s only called by Book.h
file. SubjectBook1.displayMessage()
, the function include cout Code blocks
, so it need singlely called that it couldn’t make Error. Before function getSubjectName()
, Constructor function is created when Instantiation Object, and the function has agrument, it accepted String, so the function getSubjectName()
could output this result.
At Last
In the End, I gratitude for your watch my articles, though I know life is hard, you must understand insist to last that you could become winner. If you are a truely love fans that you know I usually code one sentence at first: std::cout << "C++ is make me happy!!" << std::endl;
In my opinion, you want learn C++, you need like it, then you could learn greatly.
Your Pan.[One low-quality man of human]