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 function
void setPoint(int a, int b)
{
X = a;
Y = b;
}
//getter functions
int getX(void)
{
return X;
}
int getY(void)
{
return Y;
}
};
//Main function
int main ()
{
//object
point p1, p2;
//set points
p1.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