C++(Package Class to header file)

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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值