#include"iostream" using namespace std; class Shape { public: virtual float area()const { return 0.0; } virtual float volume()const { return 0.0; } virtual void shapeName()const = 0; }; class Point :public Shape { protected: float x, y; public: Point(float = 0, float = 0); void setPoint(float, float); float getX()const { return x;} float getY()const { return y; } virtual void shapeName()const { cout << "Point: ";} friend ostream & operator<<(ostream&, const Point &); }; Point::Point(float a, float b) { x = a; y = b; } void Point::setPoint(float a, float b) { x = a; y = b; } ostream & operator<<(ostream &output, const Point &p) { return output; } class Circle:public Point { public: Circle(float x = 0,float y = 0,float r = 0); void setRadius(float); float getRadius() const; virtual float area() const; virtual void shapeName() const { cout << "Circle:"; } friend ostream &operator<<(ostream &, const Circle &); protected: float radius; }; Circle::Circle(float a, float b, float r) : Point(a, b),radius(r) {} void Circle::setRadius(float r) {radius = r;} float Circle::getRadius() const { return radius; } float Circle::area() const { return 3.14159 * radius * radius; } ostream &operator<<(ostream &output,const Circle &c) { output << "[" << c.x << "," << c.y << "],r=" << c.radius; return output; } class Cylinder : public Circle { public: Cylinder(float x = 0, float y = 0, float r = 0, float h = 0); void setHeight(float); virtual float area() const; virtual float volume() const; virtual void shapeName() const { cout<<"Cylinder:"; } friend ostream &operator<<(ostream &, const Cylinder&); protected: float height; }; Cylinder::Cylinder(float a, float b, float r, float h): Circle(a, b, r), height(h) {} void Cylinder::setHeight(float h) { height = h; } float Cylinder :: area() const{return 2 * Circle::area() + 2 * 3.14159 * radius * height;} float Cylinder :: volume() const { return Circle::area() * height; } ostream &operator << (ostream &output, const Cylinder& cy) { output << "[" << cy.x << "," << cy.y << "],r=" << cy.radius << ",h=" << cy.height; return output; } int main() { Point point(3.2, 4.5); Circle circle(2.4, 1.2, 5.6); Cylinder cylinder(3.5, 6.4, 5.2, 10.5); point.shapeName(); cout << point<<endl; circle.shapeName(); cout<< circle << endl; cylinder.shapeName(); cout << cylinder << endl << endl; Shape * pt; pt = &point; pt->shapeName(); cout << "x=" << point.getX() << ",y=" << point.getY() << "\narea=" << pt->area() << "\nvolume=" << pt->volume() << "\n\n"; pt = &circle; pt->shapeName(); cout << "x=" << circle.getX() << ",y=" << circle.getY() << "\narea=" << pt->area() << "\nvolume=" << pt->volume() << "\n\n"; pt=&cylinder; pt->shapeName(); cout << "x=" << cylinder.getX() << ",y=" << cylinder.getY() << "\narea=" << pt->area() << "\nvolume=" << pt->volume() << "\n\n"; return 0; }为什么第一个Point不能输出数据
时间: 2023-06-27 12:04:13 浏览: 202
在你的代码中,第一个Point对象的输出运算符重载函数operator<<没有实际的输出语句,所以调用该函数时没有任何输出。你需要在该函数中添加输出语句,例如:
```c++
ostream & operator<<(ostream &output, const Point &p) {
output << "[" << p.x << "," << p.y << "]";
return output;
}
```
这样就能正确输出第一个Point对象的数据了。
相关问题
帮我分析一下这段代码的优点和缺点:#includeiostream using namespace std; #define LENGTH 10000 int arr1[LENGTH], MinCost = 0, n; void MinInAi(int i) { for (int j = i + 1; j n; j++) { if (arr1[j] arr1[i]) { int temp = arr1[i]; arr1[i] = arr1[j]; arr1[j] = temp; } } } void sort(int arr1[], int n) { for (int i = 0; i n - 1; i++) { for (int j = 0; j n - 1 - i; j++) { if (arr1[j] arr1[j + 1]) { int temp = arr1[j + 1]; arr1[j + 1] = arr1[j]; arr1[j] = temp; } } } } int main() { cin n; for (int i = 0; i n; i++) { cin arr1[i]; } sort(arr1, n); for (int i = 1; i n; i++) { arr1[i] += arr1[i - 1]; MinCost += arr1[i]; MinInAi(i); MinInAi(i + 1); } cout MinCost endl; return 0; }
优点:
1. 代码实现简单,易于理解。
2. 代码中使用了常量定义和函数封装,提高了代码的可读性和可维护性。
3. 代码实现了数组排序和求最小花费的功能,且算法时间复杂度较小。
缺点:
1. 宏定义不够严谨,可能会导致程序错误。比如LENGTH定义为10000,但实际输入的数组大小可能会超过这个值。
2. 函数命名不够规范,不易理解。比如MinInAi函数命名不够清晰,不容易看出其作用。
3. 代码中缺少注释,不容易理解某些代码的作用。
4. 程序中使用了冒泡排序算法,算法时间复杂度较高,可能会导致程序效率低下。可以考虑使用更快的排序算法,如快速排序、归并排序等。
请帮我分析一下这段代码的时间复杂度和空间复杂度:#includeiostream using namespace std; #define LENGTH 10000 int arr1[LENGTH], MinCost = 0, n; void MinInAi(int i) { for (int j = i + 1; j n; j++) { if (arr1[j] arr1[i]) { int temp = arr1[i]; arr1[i] = arr1[j]; arr1[j] = temp; } } } void sort(int arr1[], int n) { for (int i = 0; i n - 1; i++) { for (int j = 0; j n - 1 - i; j++) { if (arr1[j] arr1[j + 1]) { int temp = arr1[j + 1]; arr1[j + 1] = arr1[j]; arr1[j] = temp; } } } } int main() { cin n; for (int i = 0; i n; i++) { cin arr1[i]; } sort(arr1, n); for (int i = 1; i n; i++) { arr1[i] += arr1[i - 1]; MinCost += arr1[i]; MinInAi(i); MinInAi(i + 1); } cout MinCost endl; return 0; }
这段代码的功能是计算一个数组的最小代价,其中最小代价的计算方式是将数组中的两个数进行合并,代价为合并后的和,并将合并后的结果放回数组中。然后再在数组中找出最小的两个数进行合并,直到整个数组合并成一个数为止。
时间复杂度分析:
1. 数组排序部分:使用了冒泡排序,时间复杂度为O(n^2)
2. 计算最小代价部分:有两个for循环,时间复杂度为O(n^2);并且在每次循环中还要调用MinInAi函数,时间复杂度为O(n),所以总时间复杂度为O(n^3)
空间复杂度分析:
1. 数组arr1:长度为10000,所以空间复杂度为O(10000)
2. 变量MinCost、n、i、j、temp等:空间复杂度为O(1)
综上,该代码的时间复杂度为O(n^3),空间复杂度为O(10000)。
阅读全文