ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

DongqianxiaoC++ day02

2021-06-05 21:29:53  阅读:196  来源: 互联网

标签:function name Dongqianxiao day02 C++ GradeBook courseName include string


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有