Question

In: Computer Science

Refactor the following classes so they are both derived from a base class called Person. Write...

Refactor the following classes so they are both derived from a base class called Person. Write the code for the new base class as well. Try to avoid repeating code as much as possible. Write the classes so that any common methods can be invoked through a pointer or reference to the base class.

#include <string>
#include <cmath>
using namespace std;

class Student
{
private:
string name;
   int age;
   int studyYear;
public:
   Student(string,int,int);
void study();
   void eat();
};

Student::Student(string _name, int _age, int _studyYear)
{
   name = _name;
   age = _age;
   studyYear = _studyYear;
}

void Student::study()
{
   cout << name << " is studying." <<endl;
}

void Student::eat()
{
   cout << name << " is eating." <<endl;
}

class Doctor
{
private:
string name;
   int age;
   int salary;
public:
   Doctor(string,int,int);
void checkup();
   void eat();
};

Doctor::Doctor(string _name, int _age, int _salary)
{
   name = _name;
   age = _age;
   salary = _salary;
}

void Doctor::checkup()
{
   cout << name << " is checking up." <<endl;
}

void Doctor::eat()
{
   cout << name << " is eating." <<endl;
}

Solutions

Expert Solution

#include <iostream>
#include <string>

using namespace std;
class Person
{
protected:
string name;
int age;

public:
Person(string,int);
void eat();
};
//implementation section for Person
Person::Person(string _name, int _age) // constructor
{
name = _name;
age = _age;
}
void Person::eat()
{
cout << name << " is eating." <<endl;
}


// declaration section where Student is derived from Person
class Student : public Person
{
protected:
int studyYear;

public:
Student(string,int,int);// constructor
void study();
void eat();
};
Student::Student(string _name, int _age, int _studyYear)
{
name = _name;
age = _age;
studyYear = _studyYear;
}

void Student::study()
{
cout << name << " is studying in his "<<_studyYear <<" year" <<endl;

}

void Student::eat()
{
cout << name << " is eating." <<endl;
}


// declaration section where Doctor is derived from Person


class Doctor : public Person
{
protected:
int salary;

public:
Doctor(string,int,int);
void eat();
void checkup();
};
Doctor::Doctor(string _name, int _age, int _salary)
{
name = _name;
age = _age;
salary = _salary;
}

void Doctor::checkup()
{
cout << name << " is checking up." <<endl;
}

void Doctor::eat()
{
cout << name << " is eating." <<endl;
}

Comment down for any queries
Please give a thumbs up


Related Solutions

3. write a program that uses a class called "garment" that is derived from the class...
3. write a program that uses a class called "garment" that is derived from the class "fabric" and display some values of measurement. 20 pts. Based on write a program that uses the class "fabric" to display the square footage of a piece of large fabric.           class fabric            {                private:                    int length;                    int width;                    int...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a...
Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a function print_friend_info(person) which accepts a single argument, of type Person, and: prints out their name prints out their age if the person has any friends, prints 'Friends with {name}' Write a function create_fry() which returns a Person instance representing Fry. Fry is 25 and his full name is 'Philip J. Fry' Write a function make_friends(person_one, person_two) which sets each argument as the friend of...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
1- Write a class called MedicalStaff that is a Person (the class that you wrote in...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in last lab). A MedicalStaff has specialty (i.e. Orthopedic, cardiology, etc.). 2- Then write two classes: Doctor class has office visit fee. Nurse class has title (i.e. RN, NP, etc.) Both classes inherit from MedicalStaff. Be sure these are all complete classes, including toString method. 3- Write a tester to test these classes and their methods, by creating an array or ArrayList of Person and...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT