在C ++中使用getter和setter函数创建具有X和Y轴的类Point
We have two declare a point class with two Axis X and Y and we have to create/design its getter and setter functions.
我們有兩個聲明帶有兩個Axis X和Y的點類,并且我們必須創建/設計其getter和setter函數。
As we know that a class has some data members and number functions. So, here we are going to declare two private data members X and Y of integer types. X will be used for the X-axis and Y will be used for the Y-axis.
眾所周知,一個類具有一些數據成員和數字函數。 因此,在這里我們將聲明兩個整數類型的私有數據成員X和Y。 X將用于X軸, Y將用于Y軸。
Now, let's understand what are getter and setter member functions?
現在,讓我們了解什么是getter和setter成員函數 ?
Setter functions are those functions which are used to set/assign the values to the variables (class's data members). Here, in the given class, the setter function is setPoint() - it will take the value of X and Y from the main() function and assign it to the private data members X and Y.
設置器函數是用于將值設置/分配給變量(類的數據成員)的那些函數。 在此,在給定的類中,setter函數為setPoint() -它將從main()函數中獲取X和Y的值并將其分配給私有數據成員X和Y。
Getter functions are those functions which are used to get the values. Here, getX() and getY() are the getter function and they are returning the values of private members X and y respectively.
Getter函數是用于獲取值的那些函數。 在這里, getX()和getY()是getter函數,它們分別返回私有成員X和y的值。
Program:
程序:
#include <iostream> using namespace std;// claas declaration class point {private:int X, Y;public://defualt constructor point () {X=0; Y=0;}//setter functionvoid setPoint(int a, int b){X = a;Y = b;}//getter functionsint getX(void) {return X;} int getY(void){return Y;}};//Main function int main () {//object point p1, p2;//set pointsp1.setPoint(5, 10);p2.setPoint(50,100);//get points cout<<"p1: "<<p1.getX () <<" , "<<p1.getY () <<endl;cout<<"p1: "<<p2.getX () <<" , "<<p2.getY () <<endl;return 0; }Output
輸出量
p1: 5 , 10 p1: 50 , 100翻譯自: https://www.includehelp.com/cpp-programs/point-class-having-x-and-y-axis-with-getter-and-setter-functionsin-cpp.aspx
總結
以上是生活随笔為你收集整理的在C ++中使用getter和setter函数创建具有X和Y轴的类Point的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php数据类型_PHP数据类型能力问题和
- 下一篇: wfm扩展_WFM的完整形式是什么?