DongqianxiaoC++ day02
作者:互联网
C++构造函数
#include <iostream>
#include <string>
using namespace std;
class GradeBook
{
public:
// constructor initializes courseName with string supplied as argument
// 构造函数 用于这个类被创建时要做的事情
GradeBook( string name ,int money )
{
setCourseName( name ); // call set function to initialize courseName
setCourseMoney(money);
} // end GradeBook constructor
// function to set the course name
void setCourseName( string name )
{
courseName = name; // store the course name in the object
} // end function setCourseName
// function to get the course name
string getCourseName()
{
return courseName; // return object's courseName
} // end function getCourseName
// display a welcome message to the GradeBook user
void displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
} // end function displayMessage
void setCourseMoney(int money){
courseMoney = money;
}
int getCourseMoney(){
return courseMoney;
}
private:
string courseName;
int courseMoney;
};
// function main begins program execution
int main()
{
// create two GradeBook objects
GradeBook gradeBook1( "C++", 1000 );
GradeBook gradeBook2( "Python" , 2000);
// display initial value of courseName for each GradeBook
cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
<< "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
<< endl;
// gradeBook1.setCourseMoney(1000);
cout << "gradeBook1 CourseMoney: " << gradeBook1.getCourseMoney() << endl;;
cout << "gradeBook2 CourseMoney: " << gradeBook2.getCourseMoney() << endl;;
}
### 包含用户定义类的头文件
编写GradeBook.h头文件
#include <iostream>
#include <string> // class GradeBook uses C++ standard string class
using namespace std;
class GradeBook
{
public:
// constructor initializes courseName with string supplied as argument
GradeBook( string name )
{
setCourseName( name ); // call set function to initialize courseName
} // end GradeBook constructor
// function to set the course name
void setCourseName( string name )
{
courseName = name; // store the course name in the object
} // end function setCourseName
// function to get the course name
string getCourseName()
{
return courseName; // return object's courseName
} // end function getCourseName
// display a welcome message to the GradeBook user
void displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
} // end function displayMessage
private:
string courseName; // course name for this GradeBook
}; // end class GradeBook
编写测试cpp文件
注意引入 #include “GradeBook.h”
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
// create two GradeBook objects
GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
GradeBook gradeBook2( "CS102 Data Structures in C++" );
// display initial value of courseName for each GradeBook
cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
<< "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
<< endl;
} // end main
接口与实现分离
接口的作用:只做声明, 不做实现
编写接口 GradeBook.h
#include <string> // class GradeBook uses C++ standard string class
using namespace std;
// GradeBook class definition
class GradeBook
{
public:
GradeBook( string ); // constructor that initializes courseName
void setCourseName( string ); // function that sets the course name
string getCourseName(); // function that gets the course name
void displayMessage(); // function that displays a welcome message
private:
string courseName; // course name for this GradeBook
}; // end class GradeBook
实现类:做具体的实现
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;
// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
setCourseName( name ); // call set function to initialize courseName
} // end GradeBook constructor
// function to set the course name
void GradeBook::setCourseName( string name )
{
courseName = name; // store the course name in the object
} // end function setCourseName
// function to get the course name
string GradeBook::getCourseName()
{
return courseName; // return object's courseName
} // end function getCourseName
// display a welcome message to the GradeBook user
void GradeBook::displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
} // end function displayMessage
`
测试类将接口include即可
// Fig. 3.13: fig03_13.cpp
// GradeBook class demonstration after separating
// its interface from its implementation.
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;
// function main begins program execution
int main()
{
// create two GradeBook objects
GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
GradeBook gradeBook2( "CS102 Data Structures in C++" );
// display initial value of courseName for each GradeBook
cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
<< "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
<< endl;
} // end main
标签:function,name,Dongqianxiao,day02,C++,GradeBook,courseName,include,string 来源: https://blog.csdn.net/qq_39276337/article/details/117605626