在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.
我們有兩個(gè)聲明帶有兩個(gè)Axis X和Y的點(diǎn)類(lèi),并且我們必須創(chuàng)建/設(shè)計(jì)其getter和setter函數(shù)。
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.
眾所周知,一個(gè)類(lèi)具有一些數(shù)據(jù)成員和數(shù)字函數(shù)。 因此,在這里我們將聲明兩個(gè)整數(shù)類(lèi)型的私有數(shù)據(jù)成員X和Y。 X將用于X軸, Y將用于Y軸。
Now, let's understand what are getter and setter member functions?
現(xiàn)在,讓我們了解什么是getter和setter成員函數(shù) ?
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.
設(shè)置器函數(shù)是用于將值設(shè)置/分配給變量(類(lèi)的數(shù)據(jù)成員)的那些函數(shù)。 在此,在給定的類(lèi)中,setter函數(shù)為setPoint() -它將從main()函數(shù)中獲取X和Y的值并將其分配給私有數(shù)據(jù)成員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函數(shù)是用于獲取值的那些函數(shù)。 在這里, getX()和getY()是getter函數(shù),它們分別返回私有成員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
總結(jié)
以上是生活随笔為你收集整理的在C ++中使用getter和setter函数创建具有X和Y轴的类Point的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php数据类型_PHP数据类型能力问题和
- 下一篇: wfm扩展_WFM的完整形式是什么?