帮我看看我的代码有什么问题:#include<iostream> #include<string> using namespace std; class Vehicle { protected: string no; //编号 public: Vehicle(string no) { this->no = no; } virtual void display() = 0; //应收费用 }; class Car :public Vehicle { public: int num; float weight; Car(string no, int num, float weight) :Vehicle(no) { this->num = num; this->weight = weight; } void display() { cout << no << " " << num * 8 + weight + 2 << endl; } }; class Bus:public Vehicle { public: int num; Bus(string no, int num) :Vehicle(no) { this->num = num; } void display() { cout << no << " " << num*30 << endl; } }; class Truck :public Vehicle { public: float weight; Truck(string no, int weight) :Vehicle(no) { this->weight = weight; } void display() { cout << no << " " << weight* 5 << endl; } }; int main() { int t; cin >> t; int type; string no; while (t--) { if (type == 1){ Car car; Vehicle* pv = &car; } else if (type == 2) { Truck truck; Vehicle* pv = &truck; } else if (type == 3) { Bus bus; Vehicle* pv = &bus; } pv.display(); } return 0; }
时间: 2023-11-27 09:53:18 浏览: 40
你的代码存在几个问题:
1. 在主函数里,你没有读取每个车辆的类型和编号。你需要添加以下代码:
```
cin >> type >> no;
```
在读取完车辆类型和编号后,你需要判断车辆类型,然后创建相应的对象,并将指针指向该对象。
2. 在创建车辆对象时,你需要传入相应的参数。例如,对于Car,你需要传入num和weight。
3. 在创建车辆对象时,你需要使用new运算符。例如,对于Car,你需要使用以下代码:
```
Vehicle* pv = new Car(no, num, weight);
```
4. 在主函数里,你需要调用display函数来输出每个车辆的应收费用。你需要将指针解引用后再调用display函数。例如,对于Car,你需要使用以下代码:
```
pv->display();
```
修改后的代码如下所示:
相关问题
帮我看看我修改后的代码有什么问题:#include<iostream> #include<string> using namespace std; class Vehicle { protected: string no; //编号 public: Vehicle(string no) { this->no = no; } virtual void display() = 0; //应收费用 }; class Car :public Vehicle { public: int num; float weight; Car(string no, int num, float weight) :Vehicle(no) { this->num = num; this->weight = weight; } void display() { cout << no << " " << num * 8 + weight + 2 << endl; } }; class Bus:public Vehicle { public: int num; Bus(string no, int num) :Vehicle(no) { this->num = num; } void display() { cout << no << " " << num*30 << endl; } }; class Truck :public Vehicle { public: float weight; Truck(string no, int weight) :Vehicle(no) { this->weight = weight; } void display() { cout << no << " " << weight* 5 << endl; } }; int main() { int t; cin >> t; int type; string no; int num; float weight; while (t--) { cin >> type >> no; if (type == 1){ cin >> num >> weight; Vehicle* pv = new Car(no,num,weight);; } else if (type == 2) { cin >> weight; Vehicle* pv = new Truck (no, weight);; } else if (type == 3) { cin >> num ; Vehicle* pv =new Bus(no,num);; } pv->display(); } return 0; }
在代码中,对象指针 `pv` 在条件语句中被定义并初始化,但是在条件语句块之外被使用。这会导致编译错误,因为在使用它时,它可能没有被初始化。要解决这个问题,可以将对象指针的定义移动到条件语句块之前。修改后的代码如下所示:
```
#include<iostream>
#include<string>
using namespace std;
class Vehicle {
protected:
string no; //编号
public:
Vehicle(string no)
{
this->no = no;
}
virtual void display() = 0; //应收费用
};
class Car :public Vehicle {
public:
int num;
float weight;
Car(string no, int num, float weight) :Vehicle(no)
{
this->num = num;
this->weight = weight;
}
void display()
{
cout << no << " " << num * 8 + weight + 2 << endl;
}
};
class Bus :public Vehicle {
public:
int num;
Bus(string no, int num) :Vehicle(no)
{
this->num = num;
}
void display()
{
cout << no << " " << num * 30 << endl;
}
};
class Truck :public Vehicle {
public:
float weight;
Truck(string no, float weight) :Vehicle(no)
{
this->weight = weight;
}
void display()
{
cout << no << " " << weight * 5 << endl;
}
};
int main() {
int t;
cin >> t;
int type;
string no;
int num;
float weight;
while (t--)
{
cin >> type >> no;
Vehicle* pv;
if (type == 1) {
cin >> num >> weight;
pv = new Car(no, num, weight);
}
else if (type == 2) {
cin >> weight;
pv = new Truck(no, weight);
}
else if (type == 3) {
cin >> num;
pv = new Bus(no, num);
}
pv->display();
}
return 0;
}
```
另外,还有一个小错误:在 `Truck` 类中的构造函数中,`weight` 参数应该为 `float` 类型而不是 `int` 类型。已经在上面的代码中进行了修正。
优化这段代码并消除bug:#include<iostream> #include<string> using namespace std; class Vehicle{ protected: string name; string color; public: Vehicle(string n,string c):name(n),color(c){}; virtual void display(); }; class Car : public Vehicle{ private: int pas; public: Car(string n,string c,int p):Vehicle(n,c),pas(p){}; void display(){ cout << name << " " << color << " " << pas << endl; }; }; class Truck : public Vehicle{ private: double weight; public: Truck(string n,string c,double d):Vehicle(n,c),weight(d){}; void display(){ cout << name << " " << color << " " << weight << endl; }; }; int main() { Vehicle *p; char type; char name[110],color[110]; int pas; double cap; while(cin>>type) { cin>>name>>color; if(type == 'C') { cin>>pas; Car car(name,color,pas); p = &car; p->display(); } else if(type == 'T') { cin>>cap; Truck truck(name,color,cap); p = &truck; p->display(); } } return 0; }
优化后的代码如下,主要是将类的成员变量改为指针类型,避免在栈上分配内存导致指针失效:
```cpp
#include<iostream>
#include<string>
using namespace std;
class Vehicle{
protected:
string name;
string color;
public:
Vehicle(string n, string c): name(n), color(c){};
virtual void display() = 0;
virtual ~Vehicle(){} // 添加虚析构函数
};
class Car : public Vehicle{
private:
int *pas; // 改为指针类型
public:
Car(string n, string c, int p): Vehicle(n, c){
pas = new int(p); // 动态分配内存
};
void display(){
cout << name << " " << color << " " << *pas << endl;
};
~Car(){ // 添加析构函数,释放内存
delete pas;
}
};
class Truck : public Vehicle{
private:
double *weight; // 改为指针类型
public:
Truck(string n, string c, double d): Vehicle(n, c){
weight = new double(d); // 动态分配内存
};
void display(){
cout << name << " " << color << " " << *weight << endl;
};
~Truck(){ // 添加析构函数,释放内存
delete weight;
}
};
int main() {
Vehicle *p;
char type;
char name[110], color[110];
int pas;
double cap;
while(cin>>type) {
cin>>name>>color;
if(type == 'C') {
cin>>pas;
p = new Car(name, color, pas); // 动态分配内存
p->display();
delete p; // 释放内存
}
else if(type == 'T') {
cin>>cap;
p = new Truck(name, color, cap); // 动态分配内存
p->display();
delete p; // 释放内存
}
}
return 0;
}
```
此外,还需要添加虚析构函数,在子类析构时能够自动调用父类析构函数,释放父类的内存。并且在动态分配内存后需要在适当的时候释放内存,避免内存泄漏。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)