如何理解set和get函数

发布时间:2021-10-14 13:52:22 作者:柒染
来源:亿速云 阅读:116

如何理解set和get函数,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

文章目录  

一个Set函数和一个 Get函数的GradeBook类 .而UML图如下所示:

如何理解set和get函数

参考程序

// Define class GradeBook that contains a courseName data member
// and member functions to set and get its value; 
// Create and manipulate a GradeBook object.
#include <iostream>
using std::cout; 
using std::cin;
using std::endl;

#include <string> // program uses C++ standard string class
using std::string;
using std::getline;

// GradeBook class definition
class GradeBook
{
public:
   // function that sets the course name
   void setCourseName( string name )
   {      
      courseName = name; // store the course name in the object
   } // end function setCourseName
   
   // function that gets the course name
   string getCourseName() 
   {
      return courseName; // return the object's courseName
   } // end function getCourseName

   // function that displays a welcome message
   void displayMessage()
   {
      // this statement calls getCourseName to get the 
      // name of the course this GradeBook represents
      cout << "Welcome to the grade book for\n" << getCourseName() << "!" 
         << endl;
   } // end function displayMessage
private:
   string courseName; // course name for this GradeBook
}; // end class GradeBook  

// function main begins program execution
int main()
{
   string nameOfCourse; // string of characters to store the course name
   GradeBook myGradeBook; // create a GradeBook object named myGradeBook
   
   // display initial value of courseName
   cout << "Initial course name is: " << myGradeBook.getCourseName() 
      << endl;

   // prompt for, input and set course name
   cout << "\nPlease enter the course name:" << endl;
   getline( cin, nameOfCourse ); // read a course name with blanks
   myGradeBook.setCourseName( nameOfCourse ); // set the course name

   cout << endl; // outputs a blank line
   myGradeBook.displayMessage(); // display message with new course name
   return 0; // indicate successful termination
} // end main
输出结果

如何理解set和get函数

看完上述内容,你们掌握如何理解set和get函数的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Memcached 设置值set和取值get用法
  2. PHP 特殊方法 __set()、__get()

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

set get

上一篇:怎么使用golang仿spring ioc/aop基于注解的静态代码增强器/生成器

下一篇:什么是二叉树

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》