I am new to programming and just shifted to C++ from C.
I was learning the concepts of inheritance and tried to make a simple application that takes input from the user and show that data on consol, But I am facing some problems running it. My program seems syntactically correct but it crashes frequently. So I wondered if anyone can teach me what to take into consideration while writing the program to prevent those crashes. here is my sample code
/*
Define a class Hospital having rollno and name as data members and member function to
get and print data.
Derive a class Ward from class Hospital having data members: ward number and member function to get and print data.
Derive another class Room from Hospital having data member bed number and nature of illness and member function to get and print data.
Derive class Patient from Class Ward and Class Room.
In main () declare 5 object of Class Patient and get and display all the information.
Use the concept of Virtual Base Class and Hybrid Inheritance.
*/
#include <iostream>
#include <string>
using namespace std;
class Hospital
{
int rollno;
string name;
public:
void get()
{
cout << "Enter the roll number : ";
cin >> rollno;
fflush(stdin);
cout << "Enter the name :";
fflush(stdin);
getline(cin, name);
fflush(stdin);
}
void print()
{
cout << "\nRoll No : " << rollno;
cout << "\nName : " << name;
}
};
class Ward : public virtual Hospital
{
int W_number;
public:
void get()
{
// Hospital :: get();
cout << "Enter the Ward number : ";
cin >> W_number;
}
void print()
{
cout << "\nWard number : " << W_number;
}
};
class Room : virtual public Hospital
{
int bedNumber;
string natureOfIllness;
public:
void get()
{
cout << "Enter the bed number of patient : ";
cin >> bedNumber;
fflush(stdin);
cout << "Enter the nature of illness : ";
fflush(stdin);
getline(cin, natureOfIllness);
}
void print()
{
cout << "\nBed Number : " << bedNumber;
cout << "\nNature of illness : " << natureOfIllness;
}
};
class Patient : public Ward, public Room
{
public:
void get()
{
Hospital::get();
Ward::get();
Room::get();
}
void print()
{
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
Hospital::print();
Ward::print();
Room::print();
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
};
int main()
{
Patient p[5];
for (int i = 0; i < 5; i++)
{
cout << "\n\nEnter informtion of patient " << i + 1 << endl;
p[i].get();
}
for (int i = 0; i < 5; i++)
{
cout << "Informtion of patient " << i + 1 << endl;
p[i].print();
}
return 0;
}
thank you.