- 我可以分别定义基类和派生类的流插入和提取操作符吗?
- 如果我们从基类派生类,那么我怎样才能投射和超载流插入和提取操作符?
我创建了一个类VehicleType
和bikeType
并想重载流插入和提取运算派生类,因为我需要从文件中读取数据,因为当我从文件,类变量,这样读取数据时,我会失去更多时间。我的问题是,我如何将派生类bikeType
投射到vehicleType
。派生类插入和提取操作符重载以及基类到派生类之间的转换
#pragma once
#ifndef vehicleType_H
#define vehicleType_H
#include<string>
#include"recordType.h"
using namespace std;
class vehicleType
{
private:
int ID;
string name;
string model;
double price;
string color;
float enginePower;
int speed;
recordType r1;
public:
friend ostream& operator<<(ostream&, const vehicleType&);
friend istream& operator>>(istream&, vehicleType&);
vehicleType();
vehicleType(int id,string nm, string ml, double pr, string cl, float
enP, int spd);
void setID(int id);
int getID();
void recordVehicle();
void setName(string);
string getName();
void setModel(string);
string getModel();
void setPrice(double);
double getPrice();
void setColor(string);
string getColor();
void setEnginePower(float);
float getEnginePower();
void setSpeed(int);
int getSpeed();
void print();
};
#endif
#pragma once
#ifndef bikeType_H
#define bikeType_H
#include"vehicleType.h"
#include<iostream>
using namespace std;
class bikeType :public vehicleType
{
private:
int wheels;
public:
bikeType();
bool operator<=(int);
bool operator>(bikeType&);
bool operator==(int);
bikeType(int wls, int id, string nm, string ml, double pr, string
cl,float enP, int spd);
void setData(int id, string nm, string ml, double pr, string cl,
float enP, int spd);
void setWheels(int wls);
int getWheels();
friend istream& operator>>(istream&, bikeType&);
friend ostream& operator<<(ostream&, const bikeType&);
void print();
};
#endif
我已经定义了基类和派生类的所有函数,但只有流插入和流提取操作符无法定义。
'的ostream&运算<<(ostream的&OSObject的,常量busType& 总线) { \t OSObject对象<<的static_cast(总线); \t osObject <<“\ t \ t”<< bus.seats <<“\ t \ t”<< bus.wheels << endl; \t return osObject; }'我做了,但编译器给我错误“无效类型 转换”我不明白这种类型的错误。 –