#include<iostream> using namespace std; class CPoint { private: double x,y; public : CPoint (double i,double j); double Getx(); double Gety(); double Setpoint(double i,double j); }; class CShape { public: CPoint point; virtual double Area() { return 0.0; } }; class CCircle:public CShape { public: float r; CCircle(CPoint p1,float r=2):r(r){} double Area() { return 3.14*r*r; } }; class CRect:public CShape { public: double x,y; CRect(CPoint p1,double x=2,double y=1):x(x),y(y){} double Area() { return x*y; } }; class CEll:public CShape { public: double x,y; CEll(CPoint p1,double x=2,double y=1); double Area() { return 3.14*x*y; } }; int main() { return 0; }error: use of deleted function 'CShape::CShape()' CCircle(CPoint p1,float r=2):r(r){} ^ compilation terminated due to -Wfatal-errors.
时间: 2024-04-05 22:34:00 浏览: 14
这段代码存在一个错误,编译时会提示 use of deleted function 'CShape::CShape()' 。这是因为 CShape 类中没有默认构造函数,所以在 CCircle 和 CRect 的构造函数中需要显式调用 CPoint 的构造函数来初始化 point 成员变量。可以修改 CCircle 和 CRect 的构造函数如下:
CCircle(CPoint p1, float r = 2) : CShape(), r(r) {}
CRect(CPoint p1, double x = 2, double y = 1) : CShape(), x(x), y(y) {}
同时,CEll 的构造函数在代码中没有实现,也需要实现并显式调用 CShape 的构造函数来初始化 point 成员变量。
相关问题
#include<iostream>using namespace std;
这是一个C++的代码片段,它包含了iostream头文件和使用了命名空间std。iostream头文件提供了输入输出流的功能,而using namespace std;语句则是为了方便使用std命名空间中的函数和对象,避免每次都需要写std::。
以下是一个简单的示例代码,展示了如何使用iostream头文件和命名空间std来输出一句话:
```cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
```
这段代码会输出"Hello, World!",endl是一个特殊的控制符,用于在输出流中插入一个换行符。
补全程序,并写出程序运行结果。 #include <iostream> using namespace std; class Class { public : Class ( int...
补全程序如下:
```
#include <iostream>
using namespace std;
class Class {
public:
Class(int a, int b, int c) {
x = a;
y = b;
z = c;
}
void print() {
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "z: " << z << endl;
}
private:
int x;
int y;
int z;
};
int main() {
Class obj(1, 2, 3);
obj.print();
return 0;
}
```
程序运行结果为:
```
x: 1
y: 2
z: 3
```
程序定义了一个名为 Class 的类,该类包括有参构造函数和一个 print() 函数,构造函数用于对类的私有成员变量进行初始化,print() 函数用于输出类的私有成员变量。在主函数 main() 中,创建了一个名为 obj 的 Class 类对象,构造函数传入三个整数参数 1、2 和 3。最后调用 obj 对象的 print() 函数,输出 obj 对象的私有成员变量 x、y 和 z 的值。