为什么我们在这个程序中需要虚拟(Turbo C++)?

基兰深

我一直在使用 Turbo C++ 编写关于多重继承的代码,代码的目的是只获取输入并在控制台打印输入。我最初完成了以下代码,

// Program to demonstrate multiple inheritance

#include <conio.h>
#include <iomanip.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>>
#include <string.h>

class Company1
{
protected:
  int productID;
  char *productName;
  char *CompanyName;

public:
  void input_c1()
  {
    cout << "Provide Company 1 Details : " << endl;
    cout << setw(29) << setfill('-') << "" << endl;
    cout << "Enter the Product ID : ";
    cin >> productID;
    cout << "Enter the Product Name : ";
    gets(productName);
    cout << "Enter the Company Name : ";
    gets(CompanyName);
  }
};

class Company2
{
protected:
  int productID;
  char *productName;
  char *CompanyName;

public:
  void input_c2()
  {
    cout << endl << "Provide Company 2 Details : " << endl;
    cout << setw(29) << setfill('-') << "" << endl;
    cout << "Enter the Product ID : ";
    cin >> productID;
    cout << "Enter the Product Name : ";
    gets(productName);
    cout << "Enter the Company Name : ";
    gets(CompanyName);
  }
};

class SuperMarket : public Company2, public Company1
{
public:
  void Company_details()
  {
    cout << endl << "Company 1 details : " << endl;
    cout << setw(20) << setfill('-') << "" << endl;
    cout << "Product ID   : " << Company1::productID << endl;
    cout << "Product Name : ";
    print_string(Company1::productName);
    cout << endl;
    cout << "Company Name : ";
    print_string(Company1::CompanyName);
    cout << endl << endl;

    cout << "Company 2 details : " << endl;
    cout << setw(20) << setfill('-') << "" << endl;
    cout << "Product ID   : " << Company2::productID << endl;
    cout << "Product Name : ";
    print_string(Company2::productName);
    cout << endl;
    cout << "Company Name : ";
    print_string(Company2::CompanyName);
  }
  void print_string(char *ptr)
  {
    for (int i = 0; i != strlen(ptr); i++)
      cout << ptr[i];
  }
};

void main()
{
  char choice;
  clrscr();
  SuperMarket s;
  s.input_c1();
  s.input_c2();
  s.Company_details();
  cout << endl << "Do you want to exit ? (Y/N) : ";
  cin >> choice;
  if (choice == 'Y' || choice == 'y')
    exit(0);
  getch();
}

并在执行时得到如下输出,

单击此处查看输出

But that wasn't the output which is expected because the Company Name of Company1 is dklm instead it was expected to be def. I further on tried to rectify it and made the derivation type of Company1 as virtual public and this resulted in correct output. I heard that virtual is used mostly in the cases of Diamond problem. Despite not being a Diamond problem it has resulted in the correct output. Can anyone please explain why did I get such kind of wrong output in the mentioned program and how did the virtual made to work the program properly in this case ?

aschepler

指针productNameCompanyName未初始化,因此您不能使用它们,就好像它们指向某些字符的有效存储一样。你应该只使用std::string而不是char*指针,以避免这个问题和许多其他问题。

尽管 Turbo C++ 没有std::string,但如果您必须使用该编译器,也许您可​​以为它找到一个好的字符串类。我对此无能为力,因为此时 Turbo C++ 甚至都算不上 C++。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我们需要C ++中的纯虚拟析构函数?

为什么我们需要C ++中的虚函数?

为什么我们需要对象发送者作为 c# 中的参数事件处理程序?

为什么我们需要声明枚举变量?(C)

为什么我们需要在此脚本中调用“ c.set.bind(c)”?

为什么我们在C ++中需要飞船<=>运算符?

为什么我们需要C中的终止符

为什么我们需要在C#中锁定并反对?

为什么在C#方法中我们需要多个`await`语句?

为什么我们需要C ++中的基本数据类型

为什么我们需要指针为Go / C中的变量分配值,而不是C#/ Java中的指针

为什么在这个方法中声明这个变量会覆盖我的类成员(C++)?

为什么在C语言的套接字编程中我们需要服务器本身的IP地址

为什么我们需要在Windows C ++中链接kernel32.dll,user32.dll等?

为什么我们需要在 C++20 中的函数概念参数之后使用 auto?

为什么我们需要一个默认的构造函数来在C ++中通过引用传递对象?

为什么我们需要在C ++头文件中使用“ #if defined Identifier”?

如果C ++编译为机器代码,为什么我们需要安装“运行时”?

为什么我们需要在C#中装箱和拆箱?

在 C 中使用指针时,我们何时以及为什么需要使用 malloc?

C 为什么我们需要`execv()` 进行重定向?

为什么我们需要进行软件复位,I2C 协议?

在我们的C / C ++程序中,使用espeak需要哪些行?

为什么 crtl-C 或 ctrl-Z 在这个程序中不起作用?

为什么我在这个 C 代码中得到了错误的答案?

本学期刚开始学习C,但是我似乎无法理解教授在这个问题上向我们要什么

在C ++中我们需要双重调度/访问者的情况究竟发生了什么

为什么我们不能在C#中的结构中定义数组

请说明条件变量在c ++线程中的用法,以及为什么我们需要与此一起使用`unique_lock`和`mutex`