#include<iostream> using namespace std; class Rectangle { public: double width; double height; double area() { return width*height; } }; int main() { Rectangle rect1; Rectangle *p; p->width=1.2; cin>>p->height; cout<<p->area(); }
时间: 2024-03-15 21:47:01 浏览: 75
这段代码存在一个问题,即指针p没有被初始化,所以无法访问它所指向的对象。应该先将p指向一个已经存在的Rectangle对象,例如:
```
Rectangle rect1;
Rectangle *p = &rect1;
p->width=1.2;
cin>>p->height;
cout<<p->area();
```
这样就能正确地计算并输出矩形的面积了。
相关问题
编写以下代码#include <iostream> #include <iomanip> #include <cmath> using namespace std; class area_c1{ protected: double height; double width; public: area_c1(double r, double s){ height = r; width = s; } //定义纯虚函数area/使得类area_c1成为抽象类 @-@ }; //定义类rectangle,构造函数和成员函数area //area=height * width @-@ //定义类isosceles,构造函数和成员函数area //area=(height * width)/2 @-@ int main(){ double r1,s1; double r2,s2; cin>>r1>>s1; rectangle r(r1, s1); cin>>r2>>s2; isosceles i(r2, s2); //定义指针基类a @-@ a = &r; cout<< "The area of rectangle:" <<fixed<<setprecision(4)<< a->area() << endl; a = &i; cout << "The area of isosceles:" <<fixed<<setprecision(4)<< a->area() <<endl; return 0; }
这段代码存在问题,因为在类area_c1中定义了纯虚函数area,使得类area_c1成为抽象类,但是没有定义子类rectangle和isosceles中的area函数,导致编译会报错。
另外,在main函数中定义指针a时,需要先定义a的类型,即指向哪个类的指针,否则也会报错。
以下是修改后的代码:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class area_c1{
protected:
double height;
double width;
public:
area_c1(double r, double s){
height = r;
width = s;
}
virtual double area()=0; //定义纯虚函数area/使得类area_c1成为抽象类
};
class rectangle: public area_c1{ //定义类rectangle,构造函数和成员函数area
public:
rectangle(double r, double s): area_c1(r, s){}
double area(){
return height * width;
}
};
class isosceles: public area_c1{ //定义类isosceles,构造函数和成员函数area
public:
isosceles(double r, double s): area_c1(r, s){}
double area(){
return (height * width) / 2;
}
};
int main(){
double r1,s1;
double r2,s2;
area_c1 *a; //定义指针基类a
cin>>r1>>s1;
rectangle r(r1, s1);
cin>>r2>>s2;
isosceles i(r2, s2);
a = &r;
cout<< "The area of rectangle:" <<fixed<<setprecision(4)<< a->area() << endl;
a = &i;
cout << "The area of isosceles:" <<fixed<<setprecision(4)<< a->area() <<endl;
return 0;
}
#include <iostream> #include <cmath> using namespace std; class figure { protected: double He; double Wi; public: figure(double H, double W); }; figure::figure(double H, double W) { cin >> W >> H; cout << W << " ### " << H << endl; He = H; Wi = W; } class rectangle :public figure { public: rectangle(double H, double W) :figure(H, W) { }; void area(); void display(); }; void rectangle::display() { cout << "W: " << Wi << "H: " << He << endl; } void rectangle::area() { cout << "S= " << Wi * He << endl; } class triangle :public figure { public: triangle(double H, double W) :figure(H, W) { }; void area(); void display(); }; void triangle::display() { cout << "W: " << Wi << "H: " << He << endl; } void triangle::area() { double n = 0.5; cout << n * Wi * He << endl; } int main() { double width, height; cin >> width >> height; triangle tri(width, height); rectangle rect(width, height); tri.display(); cout << ". The area is "; cout << tri.area() << endl; rect.display(); cout << ". The area is " << rect.area() << endl; return 0; }
这段代码中存在一些问题。具体问题如下:
1. `#include` 库文件时应该每个文件一行,而不是写在同一行。
2. 类 `figure` 的构造函数中的输入语句应该放在 `main` 函数中,而不是在构造函数中。
3. 类 `triangle` 和类 `rectangle` 中的 `area` 函数应该返回一个值,而不是 `void` 类型。
4. 在 `main` 函数中,输出 `tri.area()` 和 `rect.area()` 的语句应该分别放在 `tri.area()` 和 `rect.area()` 的函数调用后面。
下面是修改后的代码:
```
#include <iostream>
#include <cmath>
using namespace std;
class figure {
protected:
double He;
double Wi;
public:
figure(double H, double W) : He(H), Wi(W) {}
};
class rectangle : public figure {
public:
rectangle(double H, double W) : figure(H, W) {}
double area();
void display();
};
void rectangle::display() {
cout << "W: " << Wi << "H: " << He << endl;
}
double rectangle::area() {
return Wi * He;
}
class triangle : public figure {
public:
triangle(double H, double W) : figure(H, W) {}
double area();
void display();
};
void triangle::display() {
cout << "W: " << Wi << "H: " << He << endl;
}
double triangle::area() {
double n = 0.5;
return n * Wi * He;
}
int main() {
double width, height;
cin >> width >> height;
triangle tri(height, width);
rectangle rect(height, width);
tri.display();
cout << ". The area is " << tri.area() << endl;
rect.display();
cout << ". The area is " << rect.area() << endl;
return 0;
}
```
阅读全文