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

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...
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....
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.
Class object in C++ programming language description about lesson base class and derived class example.
Class object in C++ programming language description about lesson base class and derived class example.
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include <iostream> using namespace std; class Person { private:         string fName;         string lName;         int birthYear;         int birthMonth;         int birthDay; public:         Person();         void setName(string, string);         void setBirthDate(int, int, int);         string getFullName();         string getBirthDate(); }; // File name: Person.cpp // Person class definition. Person is the base class. #include "Person.h" Person::Person() { fName = ""; lName =...
Write a C++ program that creates a base class called Vehicle that includes two pieces of...
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely: wheels (type int) weight (type float) Program requirements (Vehicle class): Provide set and a get member functions for each data member. Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members. Provide a pure virtual member function by the name displayData()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT