I have been coding in Turbo C++ regarding multiple inheritance and the aim of the code is to just take the input and print the inputs at the console. I have done the following code initially,
// 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();
}
and got the output as following when executed,
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 ?
The pointers productName
and CompanyName
are uninitialized, so you can't use them as if they point to valid storage for some characters. You should just use std::string
instead of char*
pointers, to save yourself from this and a lot of other issues.
Though Turbo C++ doesn't have std::string
, but maybe you can find a good string class for it if you must use that compiler. I can't really help much more with that, since Turbo C++ hardly even counts as C++ at this point.