Create a class: Doctor, with private data members: id, name, specialization and salary
with public member function: get_data () to take input for all data members, show_data
() to display the values of all data members and get specialization () to return
specialization. Write a program to write for detail of n doctors by allocating the
memory at run time only.
#include <iostream>
#include <string>
using namespace std;
class Doctor{
private:
//private data members: id, name, specialization and salary
int id;
float salary;
string name;
string specialization;
public:
//constructor
Doctor(){}
//public member function: get_data () to take input for all data members
void get_data(){
cout<<"Enter a doctor id: ";
cin>>id;
cin.ignore();
cout<<"Enter a doctor name: ";
getline(cin,name);
cout<<"Enter specialization: ";
getline(cin,specialization);
cout<<"Enter a doctor salary: ";
cin>>salary;
cin.ignore();
}
// show_data() to display the values of all data members
void show_data(){
cout<<"ID: "<<id<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Specialization: "<<get_specialization()<<endl;
cout<<"Salary: "<<salary<<endl;
}
// get specialization () to return specialization.
string get_specialization(){
return specialization;
}
};
int main(){
cout<<"Enter the number of doctors: ";
int numberDoctors;
cin>>numberDoctors;
//allocating the memory
Doctor** doctors=new Doctor*[numberDoctors];
cin.ignore();
for(int i = 0; i < numberDoctors; i++){
cout<<"Enter a detail information about doctor "<<i + 1<<endl;
doctors[i] = new Doctor();
doctors[i]->get_data();
cout<<endl;
}
for(int i = 0; i < numberDoctors; i++){
cout<<"\nDoctor "<<(i + 1)<<endl;
doctors[i]->show_data();
}
//free memory
for(int i = 0; i < numberDoctors; i++){
delete doctors[i];
}
system("pause");
return 0;
}
Comments
Leave a comment