Answer to Question #189337 in C++ for maha

Question #189337

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.


1
Expert's answer
2021-05-05T23:59:34-0400
#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;
}

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