【发布时间】:2017-04-30 17:52:06
【问题描述】:
我需要重载 > 运算符,但我没有看到任何错误。 当我仅重载运算符 >时,我看不到任何弹出来输入对象的变量。 这是代码,我似乎没有发现它有什么问题
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
class vehicle
{
protected:
int number, year, cost;
char *make, *model, *bodyStyle, *color;
public:
vehicle(int = 0, int = 0, char* = NULL, char* = NULL, char* = NULL, char* = NULL, int = 0);
~vehicle();
friend istream &operator>>(istream& stream, vehicle& v);
friend ostream &operator<<(ostream& stream, const vehicle& v);
};
vehicle::vehicle(int a, int b, char* ma1, char* mod1, char* bs1, char* c1, int cos)
{
number = a;
year = b;
make = new char[strlen(ma1) + 1];
assert(make);
strcpy (make,ma1);
model = new char[strlen(mod1) + 1];
assert(model);
strcpy(model,mod1);
bodyStyle = new char[strlen(bs1) + 1];
assert(bodyStyle);
strcpy(bodyStyle,bs1);
color = new char[strlen(c1) + 1];
assert(color);
strcpy(color,c1);
cost = cos ;
}
vehicle::~vehicle()
{
delete[] make;
delete[] model;
delete[] bodyStyle;
delete[] color;
}
istream& operator>>(istream& stream, vehicle& v)
{
stream>>v.number;
stream>>v.year;
stream>>v.cost;
stream>>v.make;
stream>>v.model;
stream>>v.bodyStyle;
stream>>v.color;
return stream;
}
ostream& operator<<(ostream& stream, const vehicle& v)
{
stream<<v.number<<endl;
stream<<v.year<<endl;
stream<<v.cost<<endl;
stream<<v.make<<endl;
stream<<v.model<<endl;
stream<<v.bodyStyle<<endl;
stream<<v.color<<endl;
return stream;
}
class Truckvehicle : public vehicle
{
int passengers, mileage, grossWeight, tempGross;
char *poweredBy;
public:
Truckvehicle(int = 0, int = 0, char* = NULL, char* = NULL, char* = NULL, char* = NULL, int = 0, int = 0, int = 0, int = 0, char * = NULL, int = 0);
~Truckvehicle();
friend ostream & operator<<(ostream& stream, const Truckvehicle& tv);
friend istream & operator>>(istream& stream, Truckvehicle& tv);
};
Truckvehicle::Truckvehicle(int a, int b, char* ma1, char* mod1, char* bs1, char* c1, int cos, int pass1, int mil, int gross, char* pb, int tg) :
vehicle(a, b, ma1, mod1, bs1, c1, cos), passengers(pass1), mileage(mil), grossWeight(gross), poweredBy(pb), tempGross(tg)
{
passengers = pass1;
mileage = mil;
grossWeight = gross;
poweredBy = new char[strlen(pb) + 1];
assert(poweredBy);
strcpy(poweredBy, pb);
tempGross = tg;
}
Truckvehicle :: ~Truckvehicle()
{
delete[] poweredBy;
}
istream& operator>>(istream& stream, Truckvehicle& tv)
{
stream>>tv.number;
stream>>tv.year;
stream>>tv.make;
stream>>tv.model;
stream>>tv.bodyStyle;
stream>>tv.color;
stream>>tv.cost;
stream>>tv.passengers;
stream>>tv.mileage;
stream>>tv.grossWeight;
stream>>tv.poweredBy;
stream>>tv.tempGross;
return stream;
}
ostream & operator<<(ostream& stream, const Truckvehicle& tv)
{
stream<<tv.number<<endl;
stream<<tv.year<<endl;
stream<<tv.make<<endl;
stream<<tv.model<<endl;
stream<<tv.bodyStyle<<endl;
stream<<tv.color<<endl;
stream<<tv.cost<<endl;
stream<<tv.passengers<<endl;
stream<<tv.mileage<<endl;
stream<<tv.grossWeight<<endl;
stream<<tv.poweredBy<<endl;
stream<<tv.tempGross<<endl;
return stream;
}
int main()
{
Truckvehicle TV;
//Read in the following values for TV (111111, 2014,"Toyota","Tacoma", "4X4", "Black",25000, 2, 15000,333333,"Gas", 222222);
cin>>TV;
cout<<TV;
return 0;
}
【问题讨论】:
-
“弹出”?使用
std::cin时,只需在控制台中输入即可。 -
它只是让我 .cpp 停止工作错误
-
看在上帝的份上,请使用 std::string。
-
你知道如何使用调试器吗?
标签: c++ class inheritance operator-overloading