【问题标题】:Derived Class Insertion and extraction operator overloading and Casting between base to derived class派生类插入和提取运算符重载以及基类到派生类之间的强制转换
【发布时间】:2017-08-01 09:43:50
【问题描述】:
  1. 能否分别定义基类和派生类的流插入和提取运算符?
  2. 如果我们从基类派生类,那么如何强制转换和重载流插入和提取运算符?

我创建了一个类 VehicleTypebikeType 并希望为派生类重载流插入和提取运算符,因为我需要从文件中读取数据,因为当我使用类变量从文件中读取数据时,我会失去了更多的时间。我的问题是如何将派生类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++


    【解决方案1】:
    1. 能否分别定义基类和派生类的流插入和提取运算符?

    是的。正如你所做的那样。

    1. 如果我们从基类派生类,那么如何强制转换和重载流插入和提取运算符?

    如果您想调用 base 的提取运算符,您可以使用静态强制转换来引用 base。

    【讨论】:

    • ostream&amp; operator&lt;&lt;(ostream&amp; osObject, const busType&amp; bus) { osObject &lt;&lt; static_cast&lt;vehicleType&amp;&gt;(bus); osObject &lt;&lt; "\t\t" &lt;&lt; bus.seats &lt;&lt; "\t\t" &lt;&lt; bus.wheels &lt;&lt; endl; return osObject; } 我做了,但是编译器给了我“无效类型转换”的错误,我不明白这种类型的错误。
    【解决方案2】:

    在基类中添加虚函数并在派生类中定义。

    class vehicleType
    {
        // ...
        virtual ostream& output(ostream& os) = 0;
    };
    
    class bikeType :public vehicleType
    {
        // ...
        ostream& output(ostream& os) override
        {
            return os << "I am a bike!";
        }
    };
    

    像这样定义输出运算符:

    ostream& operator<<(ostream& os, const vehicleType& v)
    {
        return v.ouput(os);
    }
    

    【讨论】:

    • 我已经为基类定义了函数。我想为派生类定义函数。
    • 不清楚你在问什么。请提供一个可验证的最小示例。
    • 我想告诉你,我们不能把朋友函数变成虚函数。
    • @SarmadAli,我仍然没有得到你对社区的期望。 user2079303 已经为您的问题提供了准确的答案。我的回答显示了实现相同结果的不同方法。但是,您尚未接受其中任何一项。
    猜你喜欢
    • 2018-12-01
    • 1970-01-01
    • 2011-08-03
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多