在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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值