【发布时间】:2017-08-01 09:43:50
【问题描述】:
- 能否分别定义基类和派生类的流插入和提取运算符?
- 如果我们从基类派生类,那么如何强制转换和重载流插入和提取运算符?
我创建了一个类 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
我已经定义了基类和派生类的所有函数,但只有流插入和流提取运算符无法定义。
【问题讨论】:
标签: c++