运算符重载>> CIN

时间:2017-04-30 17:52:06

标签: c++ class inheritance operator-overloading

我需要重载<<和>>运营商,但我没有看到任何错误。 当我仅重载运算符<<并直接在main中初始化对象。 我没有收到任何错误。但是当我重载运算符>>时,我没有看到任何弹出来输入对象的变量。 这是代码,我似乎没有发现它有什么问题

#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;
}

1 个答案:

答案 0 :(得分:0)

您好,您可能希望学习此代码,该代码使用GCC HEAD 8编译器在Wandbox中编译时没有错误和警告。我会及时回答有关我的代码的任何问题。

问候,Micha

#include <iostream>

// Don't use <blah.h>, prefer using <cblah>.
#include <cstring>
#include <cassert>

// extra includes.
#include <string>
#include <sstream>

using namespace std;

class vehicle
{
    friend ostream& operator<< (ostream& stream, vehicle const& v);
    friend istream& operator>> (istream& stream, vehicle& v);

protected:
    int number, year, cost;
    char *make, *model, *bodyStyle, *color;

public:
    vehicle(int = 0, 
            int = 0, 
            char const* = "-",    // Don't choose NULL here - prevents segmentation fault in main!
            char const* = "-", 
            char const* = "-", 
            char const* = "-", 
            int = 0);

    ~vehicle();
};

vehicle::vehicle(int a, 
                 int b, 
                 char const* ma1, 
                 char const* mod1, 
                 char const* bs1, 
                 char const* 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()
{
    if (make)      delete[] make;
    if (model)     delete[] model;
    if (bodyStyle) delete[] bodyStyle;
    if (color)     delete[] color;
}

ostream& operator<< (ostream& stream, vehicle const& 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;
}

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;
}



class Truckvehicle : public vehicle
{
    friend ostream& operator<< (ostream& stream, Truckvehicle const& tv);
    friend istream& operator>> (istream& stream, Truckvehicle& tv);

    int  passengers, mileage, grossWeight, tempGross;
    char *poweredBy;

public:
    Truckvehicle(int = 0, 
                 int = 0, 
                 char const* = "-", 
                 char const* = "-", 
                 char const* = "-", 
                 char const* = "-", 
                 int = 0, 
                 int = 0, 
                 int = 0, 
                 int = 0, 
                 char const* = "-", 
                 int = 0);

    ~Truckvehicle();
};

Truckvehicle::Truckvehicle(int a, 
                           int b, 
                           char const* ma1, 
                           char const* mod1, 
                           char const* bs1, 
                           char const* c1, 
                           int cos, 
                           int pass1, 
                           int mil, 
                           int gross, 
                           char const* pb, 
                           int tg)
    :
    vehicle(a, b, ma1, mod1, bs1, c1, cos)
{
    passengers  = pass1;
    mileage     = mil;
    grossWeight = gross;

    poweredBy = new char[strlen(pb) + 1];
    assert(poweredBy);
    strcpy(poweredBy, pb);

    tempGross = tg;
}

Truckvehicle::~Truckvehicle()
{
    delete[] poweredBy;
}

ostream& operator<< (ostream& stream, Truckvehicle const& tv)
{
    // vehicle part.
    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;

    // extra truck part.
    stream <<tv.passengers  <<endl;
    stream <<tv.mileage     <<endl;
    stream <<tv.grossWeight <<endl;
    stream <<tv.poweredBy   <<endl;
    stream <<tv.tempGross   <<endl;

    return stream;
}

istream& operator>> (istream& stream, Truckvehicle& tv)
{
    // vehicle part.
    stream >>tv.number;
    stream >>tv.year;
    stream >>tv.make;
    stream >>tv.model;
    stream >>tv.bodyStyle;
    stream >>tv.color;
    stream >>tv.cost;

    // extra truck part.
    stream >>tv.passengers;
    stream >>tv.mileage;
    stream >>tv.grossWeight;
    stream >>tv.poweredBy;
    stream >>tv.tempGross;

    return stream;
}


// Simulates usage of std::operator>> ().
void initializeTruckvehicle(Truckvehicle& TV, 
                           int a, 
                           int b, 
                           char const* ma1, 
                           char const* mod1, 
                           char const* bs1, 
                           char const* c1, 
                           int cos, 
                           int pass1, 
                           int mil, 
                           int gross, 
                           char const* pb, 
                           int tg)
{
    string s;

    s += static_cast <ostringstream *> (&( ostringstream() << a )) -> str();
    s += " ";

    s += static_cast <ostringstream *> (&( ostringstream() << b )) -> str();
    s += " ";

    s += ma1;
    s += " ";

    s += mod1;
    s += " ";

    s += bs1;
    s += " ";

    s += c1;
    s += " ";

    s += static_cast <ostringstream *> (&( ostringstream() << cos   )) -> str();
    s += " ";

    s += static_cast <ostringstream *> (&( ostringstream() << pass1 )) -> str();
    s += " ";

    s += static_cast <ostringstream *> (&( ostringstream() << mil   )) -> str();
    s += " ";

    s += static_cast <ostringstream *> (&( ostringstream() << gross )) -> str();
    s += " ";

    s += pb;
    s += " ";

    s += static_cast <ostringstream *> (&( ostringstream() << tg    )) -> str();
    s += " ";

    istringstream in(s);
    in >> TV;
}



int main()
{
    cout << "Truck 1>" << endl;

    Truckvehicle TV1 (111111, 
                      2014, 
                      "Toyota", 
                      "Tacoma", 
                      "4X4", 
                      "Black", 
                      25000, 
                      2, 
                      15000, 
                      333333, 
                      "Gas", 
                      222222);
    cout << TV1;


    cout << "Truck 2>" << endl;

    Truckvehicle TV2;
    cout << TV2;


    cout << "Truck 3>" << endl;

    Truckvehicle TV3;
    initializeTruckvehicle (TV3, 
                            111111, 
                            2014, 
                            "Toyota", 
                            "Tacoma", 
                            "4X4", 
                            "Black", 
                            25000, 
                            2, 
                            15000, 
                            333333, 
                            "Gas", 
                            222222);
    cout << TV3;
}