日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

在C ++中使用getter和setter函数创建具有X和Y轴的类Point

發(fā)布時(shí)間:2025/3/11 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在C ++中使用getter和setter函数创建具有X和Y轴的类Point 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。