使用引用该对象的基类指针向量打印派生类对象

2024-03-13

如何制作这个指针向量,然后正确打印 Derived1 对象和 Derived2 对象?

即使我在派生类中包含“

我该怎么做才能使程序从 Derived1 类或 Derived2 类中获取“

out << (Base&)temp;

在我的操作员的定义中。

#include<iostream>
#include<fstream>
using namespace std;

class Base {
private:
    int base;
public:
    Base() :base(10){}
    friend ostream& operator<<(ostream& out, const Base& temp)
    {
        out << "======== BASE ========" << endl;
        out << temp.base << endl;
        out << "======== BASE ========" << endl;
        return out;

    }

    friend ofstream& operator<<(ofstream& out, const Base& temp)
    {
        out << "======== BASE ========" << endl;
        out << temp.base << endl;
        out << "======== BASE ========" << endl;
        return out;

    }

};

class Derived1 :public Base {
private :
    int derived1;
public:
    Derived1() :derived1(5){}
        friend ostream& operator<<(ostream& out, const Derived1& temp)
    {
        out << (Base&)temp;
        out << "======== DERIVED1 ========" << endl;
        out << temp.derived1 << endl;
        out << "======== DERIVED1 ========" << endl;
        return out;

    }
    friend ofstream& operator<<(ofstream& out, const Derived1& temp)
    {
        out << (Base&)temp;
        out << "======== DERIVED1 ========" << endl;
        out << temp.derived1 << endl;
        out << "======== DERIVED1 ========" << endl;
        return out;

    }
};

class Derived2 :public Base {
private:
    int derived2;
 public:
    Derived2() :derived2(5) {}
    friend ostream& operator<<(ostream& out, const Derived2& temp)
    {
        out << (Base&)temp;
        out << "======== DERIVED2 ========" << endl;
        out << temp.derived2 << endl;
        out << "======== DERIVED2 ========" << endl;
        return out;
    }
    friend ofstream& operator<<(ofstream& out, const Derived2& temp)
    {
        out << (Base&)temp;
        out << "======== DERIVED2 ========" << endl;
        out << temp.derived2 << endl;
        out << "======== DERIVED2 ========" << endl;
        return out;
    }
};

void main()
{
    Derived1 d1;
    Derived2 d2;
    Base* v[2];
    v[0] = &d1;
    v[1] = &d2;
    cout << *v[0] << endl;
    ofstream f("fis.txt");
    f << *v[0];
}

显示的代码有几个问题

1)没有必要为 std::ostream 和 std::ofstream 定义单独的运算符(这一点在评论之一中指出)

Since std::ostream是一个超类std::ofstream,只需要实现一个运算符std::ostream.

2)“使用命名空间std”总是做错事 https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice.

3)main()函数应该返回一个 int https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c.

现在,要回答这里的主要问题,这里需要做的就是使用虚拟继承。这就是虚拟继承的用途。你懂的。现在,正如您所指出的,仅仅因为运算符不能是虚拟的,绝对没有法律可以阻止他们自己调用虚拟方法。

Base's operator<<应该只是一些适当命名的函数的外观,例如 format(),它将作为类中的虚拟方法实现。

最终结果看起来像这样:

#include<iostream>
#include<fstream>

class Base {
private:
    int base;
public:
    Base() :base(10){}

    friend std::ostream& operator<<(std::ostream& out, const Base& temp)
    {
        temp.format(out);
        return out;
    }

    virtual void format(std::ostream &out) const
    {
        out << "======== BASE ========" << std::endl;
        out << base << std::endl;
        out << "======== BASE ========" << std::endl;
    }

};

class Derived1 :public Base {
private :
    int derived1;
public:
    Derived1() :derived1(5){}

    void format(std::ostream &out) const override
    {
        out << "======== DERIVED1 ========" << std::endl;
        out << derived1 << std::endl;
        out << "======== DERIVED1 ========" << std::endl;

    }
};

class Derived2 :public Base {
private:
    int derived2;
public:
    Derived2() :derived2(5) {}

    void format(std::ostream &out) const override
    {
        out << "======== DERIVED1 ========" << std::endl;
        out << derived2 << std::endl;
        out << "======== DERIVED1 ========" << std::endl;
    }
};

int main()
{
    Derived1 d1;
    Derived2 d2;
    Base* v[2];
    v[0] = &d1;
    v[1] = &d2;
    std::cout << *v[0] << std::endl;
    std::ofstream f("fis.txt");
    f << *v[0];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用引用该对象的基类指针向量打印派生类对象 的相关文章

  • 编写此代码片段的有效方法是什么? [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 更有效和 或更短地重写此代码以节省字节并显得不那么冗长的方法 if N 2 0 N 6 N 8 N 10 N 12 N 14 N 16 N
  • 无法将 std::min 传递给函数,std::min 的副本有效

    Passing std min函数无法编译 我复制了 libcpp 声明std min进入我的源文件并且它可以工作 std 版本有什么问题 clang 和 gcc 也会发生同样的情况 在 Godbolt 上测试 https godbolt
  • XPATH 查询、HtmlAgilityPack 和提取文本

    我一直在尝试从名为 tim new 的类中提取链接 我也得到了解决方案 给出了解决方案 片段和必要的信息here https stackoverflow com questions 2982862 extracting a table ro
  • 进程退出后 POSIX 名称信号量不会释放

    我正在尝试使用 POSIX 命名信号量进行跨进程同步 我注意到进程死亡或退出后 信号量仍然被系统打开 在进程 打开它 死亡或退出后是否有办法使其关闭 释放 早期的讨论在这里 当将信号量递减至零的进程崩溃时 如何恢复信号量 https sta
  • 司机和提供商之间的区别

    数据库中的驱动程序和提供程序有什么区别 有没有解释一下 不胜感激 样本 ADO NET driver for MySQL vs providerName System Data EntityClient 来自 MSDN 论坛 驱动程序是安装
  • 将下拉列表与字典绑定

    我将字典绑定到下拉列表 举例来说 我的字典中有以下项目 Test1 123 Test2 321 我希望下拉文本采用以下格式 Test1 Count 123 Test2 Count 321 我沿着以下路径走 但没有运气 MyDropDown
  • 静态类与类的实例

    我有一个静态类 用于访问我的公共属性 整个应用程序的全局属性 和我在应用程序运行期间使用的方法 例如 我在静态类中设置了一些属性 并且在应用程序运行时我可以从属性中获取值 但我可以使用单例模式创建非静态类并以相同的方式使用它 问题 对于我的
  • 在 C# 中何时使用 ArrayList 而不是 array[]?

    我经常使用一个ArrayList而不是 正常 array 当我使用时 我感觉好像我在作弊 或懒惰 ArrayList 什么时候可以使用ArrayList在数组上 数组是强类型的 并且可以很好地用作参数 如果您知道集合的长度并且它是固定的 则
  • 如何在 C# 中获取 Json 数组?

    我有一个像这样的 Json 字符串 我想将它加载到 C 数组中 当我尝试这样做时 我收到异常 我的字符串 customerInformation customerId 123 CustomerName Age 39 Gender Male
  • 使用多线程进行矩阵乘法?

    我应该使用线程将两个矩阵相乘 有两件事 当我运行程序时 我不断得到 0 我还收到消息错误 对于每个错误 它在粗体行上显示 警告 从不兼容的指针类型传递 printMatrix 的参数1 我尝试打印输出 还要注意 第一个粗体块 这是我解决问题
  • doxygen c++:记录由“using”声明公开的私有继承成员

    作为一个例子 我有以下课程 class A public void methodOne class B private A public Brief description using A methodOne 我还没有找到强制 doxyge
  • ALTER TABLE ... ADD CONSTRAINT 失败时将事务回滚到保存点

    有没有办法在事务中添加检查约束and如果失败回滚到以前的保存点 而不是回滚整个事务 就我而言 当 ALTER TABLE ADD CONSTRAINT 命令失败时 事务无法回滚到保存点 尝试这样做会引发 InvalidOperationEx
  • EnumDisplayDevices 与 WMI Win32_DesktopMonitor,如何检测活动监视器?

    对于我当前的 C 项目 我需要为在大量计算机上连接并处于活动状态的每个监视器检测一个唯一的字符串 研究指出了两种选择 使用 WMI 并查询 Win32 DesktopMonitor 以获取所有活动监视器 使用 PNPDeviceID 来唯一
  • 从 NumPy 数组到 Mat 的 C++ 转换 (OpenCV)

    我正在围绕 ArUco 增强现实库 基于 OpenCV 编写一个薄包装器 我试图构建的界面非常简单 Python 将图像传递给 C 代码 C 代码检测标记并将其位置和其他信息作为字典元组返回给 Python 但是 我不知道如何在 Pytho
  • 有没有办法直接在函数参数中格式化字符串而不是使用临时字符串?

    我有一个接受字符串 字符数组 作为参数的函数 void enterString char my string 当使用这个函数时 我经常发现自己想要输入格式化的字符串 我使用 sprintf 来做到这一点 然而 我每次都必须创建一个临时字符串
  • “必须声明标量变量”错误[重复]

    这个问题在这里已经有答案了 必须声明标量变量 Id SqlConnection con new SqlConnection connectionstring con Open SqlCommand cmd new SqlCommand cm
  • 无法识别解决方案文件夹中的 Visual Studio 2017 Nuget.config

    我在使用 Visual Studio 2017 时遇到问题 新的解决方案不断引用 C Users yopa AppData Roaming NuGet Nuget config 中意外位置的 Nuget config 文件 我已将 nuge
  • 如果“嵌入式”SQL 2008 数据库文件不存在,如何创建它?

    我使用 C ADO Net 和在 Server Management Studio 中创建的嵌入式 MS SQL 2008 数据库文件 附加到 MS SQL 2008 Express 创建了一个数据库应用程序 有人可以向我指出一个资源 该资
  • 将 Swagger 与命名空间版本的 WebApi 结合使用

    我已经找到了如何使用基于名称空间的 WebAPI 版本这个班 https aspnet codeplex com SourceControl changeset view dd207952fa86 Samples WebApi Namesp
  • 将 char 绑定到枚举类型

    我有一段与此非常相似的代码 class someclass public enum Section START MID END vector section Full void ex for int i 0 i section

随机推荐