本文共 1279 字,大约阅读时间需要 4 分钟。
In the below program, we are creating a C++ program to create a class with setter and getter methods.
在下面的程序中,我们正在创建一个C ++程序,以使用setter和getter方法创建一个类 。
#includeusing namespace std;// class definitionclass Pofloat { private: // private data members float x, y; public: // public member functions void set_xy(float x, float y); float get_x(); float get_y();};// member functions definitionsvoid Pofloat::set_xy(float x, float y) { this -> x = x; this -> y = y;}float Pofloat::get_x() { return x;}float Pofloat::get_y() { return y;}// main functionint main() { // creating objects Pofloat objP; //setting the values objP.set_xy(36.0f, 24.0f); //getting the values cout << "objP is " << objP.get_x(); cout << ", " << objP.get_y() << endl; //setting the values again objP.set_xy(1.2f, 3.4f); //getting the values again cout << "objP is " << objP.get_x(); cout << ", " << objP.get_y() << endl; return 0;}
Output
输出量
objP is 36, 24objP is 1.2, 3.4
See the program – here method set_xy() which is a setter method that is using to set the value of x and y and get_x(), get_y() are the getter methods that are using to get the value of x and y.
请参见程序–这里的set_xy()方法是一个setter方法,用于设置x和y的值,以及get_x() , get_y()是用于获取x和y值的getter方法。
翻译自:
转载地址:http://hcozd.baihongyu.com/