Answer to Question #188614 in C++ for Sourav Meher

Question #188614

Create a class: Doctor, with private data members: id, name, specialization and salary with the 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.


1
Expert's answer
2021-05-03T17:06:26-0400
#include <iostream>
using namespace std;
class Doctor{
    private:
        int id, salary;
        string name, specialization;
    public:
        Doctor(){}
        Doctor(int id, int sal, string name, string special){
            get_data(id, sal, name, special);
        }
        void get_data(int id, int sal, string n, string special){
            this->id = id;
            salary = sal;
            name = n;
            specialization = special;
        }
        void show_data(){
            cout<<"Name: "<<name<<endl;
            cout<<"ID: "<<id<<endl;
            cout<<"Specialization: "<<get_specialization()<<endl;
            cout<<"Salary: "<<salary<<endl;
        }
        string get_specialization(){
            return specialization;
        }
};
int main(){
    string name, special;
    int id, salary;
    cout<<"Input number of doctors: ";
    int number; cin>>number;
    Doctor *drs[number];
    for(int i = 0; i < number; i++){
        cout<<"\nInput doctor name "<<i + 1<<endl;
        cin>>name;
        cout<<"Input doctor id "<<endl;
        cin>>id;
        cout<<"Input salary "<<endl;
        cin>>salary;
        cout<<"Input specialization "<<endl;
        cin>>special;
        drs[i] = new Doctor(id, salary, name, special);
    }
    for(int i = 0; i < number; i++){
        cout<<"\nDoctor "<<i + 1<<endl;
        drs[i]->show_data();
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS