题目描述
【问题描述】定义一个点类,该类包含整形坐标x,y以及用于设置坐标值的函数,名为setxy()参数自行确定,以及用于显示坐标的函数displayxy()参数自行设置。
合理编写主函数,能够实现(3,4)以及(5,6)固定两个点对象的参数设置,以及信息输出
【输入形式】无数据输入,请一定使用类的定义以及对象的创建的相关知识
【输出形式】输出两个固定点的相关信息
【样例输入】
【样例输出】
The first point is:(3,4)
The second point is:(5,6)
解题思路
根据题意编写类即可,可以使用构造函数定义类。
源代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class dot {
private:
int a, b;
public:
void setxy(int x, int y) {
a = x;
b = y;
}
void displayxy() {
cout << "(" << a << "," << b << ")" << endl;
}
};
int main(){
dot first, second;
first.setxy(3, 4);
second.setxy(5, 6);
cout << "The first point is:";
first.displayxy();
cout << "The second point is:";
second.displayxy();
return 0;
}
总结
比较基础的一道类题。