先建立一个Point(点)类,包含数据成员x, y(坐标点),重载运算符“ << ”和“ >> ”
时间: 2024-03-09 10:49:32 浏览: 58
好的,以下是Point(点)类的定义和重载运算符的实现:
```c++
#include <iostream>
using namespace std;
class Point {
private:
double x;
double y;
public:
Point() : x(0), y(0) {}
Point(double x, double y) : x(x), y(y) {}
friend istream& operator>>(istream& input, Point& p) {
input >> p.x >> p.y;
return input;
}
friend ostream& operator<<(ostream& output, const Point& p) {
output << "(" << p.x << ", " << p.y << ")";
return output;
}
};
```
上面的代码中,我们定义了一个Point类,其中包含了数据成员x和y,表示点的坐标。类中还重载了输入和输出运算符,使得我们可以方便地输入和输出点的坐标。在输入运算符中,我们使用了istream类,表示输入流,而在输出运算符中,我们使用了ostream类,表示输出流。同时,我们在输入运算符和输出运算符中都使用了friend关键字,表示这两个运算符是Point类的友元函数,可以访问Point类的私有成员。
相关问题
先建立一个Point(点)类,包含数据成员x,y(坐标点)。以它为基类,派生出一个Circle(圆)类,增加数据成员r(半径),再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,在增加数据成员h(高)。要求编写程序,重载运算符“<<”和“>>”,求解圆的面积和圆柱体体积。
好的,我会为您解答这个问题。
首先,我们可以先定义一个Point类,包含数据成员x和y,以及重载运算符">>"和"<<",用于输入和输出坐标点的信息。
```C++
#include<iostream>
using namespace std;
class Point{
protected:
double x, y;
public:
Point(double _x=0, double _y=0): x(_x), y(_y){}
friend istream& operator>>(istream& in, Point& p){
cout<<"请输入坐标点(x,y): ";
in>>p.x>>p.y;
return in;
}
friend ostream& operator<<(ostream& out, const Point& p){
out<<"("<<p.x<<","<<p.y<<")";
return out;
}
};
```
接下来,我们可以定义一个Circle类,继承自Point类,并增加数据成员r,以及重载运算符">>"和"<<",用于输入和输出圆的信息。
```C++
class Circle: public Point{
protected:
double r;
public:
Circle(double _x=0, double _y=0, double _r=0): Point(_x, _y), r(_r){}
friend istream& operator>>(istream& in, Circle& c){
in>>dynamic_cast<Point&>(c);
cout<<"请输入圆的半径: ";
in>>c.r;
return in;
}
friend ostream& operator<<(ostream& out, const Circle& c){
out<<"圆心坐标为"<<dynamic_cast<const Point&>(c)<<",半径为"<<c.r;
return out;
}
double area(){ //计算圆的面积
return 3.1415926*r*r;
}
};
```
最后,我们可以定义一个Cylinder类,继承自Circle类,并增加数据成员h,以及重载运算符">>"和"<<",用于输入和输出圆柱体的信息。
```C++
class Cylinder: public Circle{
private:
double h;
public:
Cylinder(double _x=0, double _y=0, double _r=0, double _h=0): Circle(_x, _y, _r), h(_h){}
friend istream& operator>>(istream& in, Cylinder& cy){
in>>dynamic_cast<Circle&>(cy);
cout<<"请输入圆柱体的高: ";
in>>cy.h;
return in;
}
friend ostream& operator<<(ostream& out, const Cylinder& cy){
out<<"圆柱体的底面信息为:"<<dynamic_cast<const Circle&>(cy)<<",高为"<<cy.h;
return out;
}
double volume(){ //计算圆柱体的体积
return Circle::area()*h;
}
};
```
现在,我们可以在主函数中输入圆柱体的信息,并计算圆的面积和圆柱体的体积。
```C++
int main(){
Cylinder cy;
cin>>cy;
cout<<cy<<endl;
cout<<"圆的面积为:"<<cy.area()<<endl;
cout<<"圆柱体的体积为:"<<cy.volume()<<endl;
return 0;
}
```
这样,程序就完成了。
定义类有x,y两个成员变量对Point类重载运算符实现对坐标的改变
下面是一个示例代码,演示如何定义Point类并重载运算符实现对坐标的改变:
```cpp
#include <iostream>
using namespace std;
class Point {
public:
int x;
int y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
Point operator+(const Point& p) {
return Point(x + p.x, y + p.y);
}
Point operator-(const Point& p) {
return Point(x - p.x, y - p.y);
}
Point operator*(const int n) {
return Point(x * n, y * n);
}
};
int main() {
Point p1(1, 2), p2(3, 4);
Point p3 = p1 + p2;
Point p4 = p1 - p2;
Point p5 = p1 * 2;
cout << "p1: (" << p1.x << ", " << p1.y << ")" << endl;
cout << "p2: (" << p2.x << ", " << p2.y << ")" << endl;
cout << "p1 + p2: (" << p3.x << ", " << p3.y << ")" << endl;
cout << "p1 - p2: (" << p4.x << ", " << p4.y << ")" << endl;
cout << "p1 * 2: (" << p5.x << ", " << p5.y << ")" << endl;
return 0;
}
```
在上面的代码中,我们定义了一个Point类,包含x和y两个成员变量。然后,我们重载了运算符+、-和*,分别实现了两个点的相加、相减和与一个整数相乘的操作。在main函数中,我们创建了两个Point对象p1和p2,并使用重载的运算符对它们进行了操作,得到了新的Point对象p3、p4和p5,并将它们的坐标打印出来。