Question

Design class Person with private fields name and social security number, and a public virtual member...

Design class Person with private fields name and social security number, and a public virtual member Design class Person with private fields name and social security number, and a public virtual member function print().


Derive classes Employee and Student from Person. An employee has a private field salary and a student has a private field grade in addition to the data fields in person.
Derive classes Professor and Secretary from Employee. A professor has a private field research area and a secretary has a private field department he/she works for in addition to the data fields in employee.

Every class has to have a print() function to print its own data fields and those of its base class.

Make sure the following main() function works correctly with your classes.

main()
{
    vector<Person *> persons;

    persons.push_back(new Person("Dave"));
    persons.push_back(new Person("Jack", "123-45-6789"));
    persons.push_back(new Employee("Sam", "111-22-3333", 50'000));
    persons.push_back(new Professor("Kay", "444-33-2222", 70'000, "Parallel Computing"));
    persons.push_back(new Professor("Joy", "555-55-5555", 1'000, "AI"));
    persons.push_back(new Secretary("Misty", "999-99-9999", 900'000));
    persons.push_back(new Student("Ace Prog", "654-32-3456", "B"));

    for (int i = 0; i < persons.size(); i++)
        persons[i]->print();

    for (int i = 0; i < persons.size(); i++)
        delete persons[i];
}

Hand in a printout of your program and typescript of its compile and run.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <vector>

using namespace std;

class Person {
        private:
                string name, ssn;
        public:
                Person(string a, string b="") {
                        name = a;
                        ssn = b;
                }
                string getName() const {
                        return name;
                }
                string getSSN() const {
                        return ssn;
                }
                virtual void print() const {
                        cout << "Person=>" << endl;
                        cout << "Name: " << name;
                        cout << ", SSN: " << ssn << endl << endl;
                };
};

class Employee: public Person {
        private:
                double salary;
        public:
                Employee(string a, string b, double s):Person(a, b) {
                        salary = s;
                }
                double getSalary() const {
                        return salary;
                }
                void print() const {
                        cout << "Employee=>" << endl;
                        cout << "Name: " << Person::getName();
                        cout << ", SSN: " << Person::getSSN();
                        cout << ", Salary: " << salary << endl << endl;
                }
};

class Student: public Person {
        private:
                string grade;
        public:
                Student(string a, string b, string g):Person(a, b) {
                        grade = g;
                }
                string getGrade() const {
                        return grade;
                }
                void print() const {
                        cout << "Student=>" << endl;
                        cout << "Name: " << Person::getName();
                        cout << ", SSN: " << Person::getSSN();
                        cout << ", Grade: " << grade << endl << endl;
                }
};

class Professor: public Employee {
        private:
                string research;
        public:
                Professor(string a, string b, double g, string s):Employee(a, b, g) {
                        research = s;
                }
                string getResearchArea() const {
                        return research ;
                }
                void print() const {
                        cout << "Professor=>" << endl;
                        cout << "Name: " << Employee::getName();
                        cout << ", SSN: " << Employee::getSSN();
                        cout << ", Salary: " << Employee::getSalary();
                        cout << ", Research Area: " << research << endl << endl;
                }
};

class Secretary: public Employee {
        private:
                string department;
        public:
                Secretary(string a, string b, double g, string d=""):Employee(a, b, g) {
                        department = d;
                }
                string getDepartment() const {
                        return department ;
                }
                void print() const {
                        cout << "Secretary=>" << endl;
                        cout << "Name: " << Employee::getName();
                        cout << ", SSN: " << Employee::getSSN();
                        cout << ", Salary: " << Employee::getSalary();
                        cout << ", Department: " << department << endl << endl;
                }
};

int main() {
    vector<Person *> persons;

    persons.push_back(new Person("Dave"));
    persons.push_back(new Person("Jack", "123-45-6789"));
    persons.push_back(new Employee("Sam", "111-22-3333", 50'000));
    persons.push_back(new Professor("Kay", "444-33-2222", 70'000, "Parallel Computing"));
    persons.push_back(new Professor("Joy", "555-55-5555", 1'000, "AI"));
    persons.push_back(new Secretary("Misty", "999-99-9999", 900'000));
    persons.push_back(new Student("Ace Prog", "654-32-3456", "B"));

    for (int i = 0; i < persons.size(); i++)
        persons[i]->print();

    for (int i = 0; i < persons.size(); i++)
        delete persons[i];
}

saved share gcc version 4.6.3 man.cpo Name: Dave, SSN: Per son-> 10e 101- int main Name Jack, SSN: 123 45 6789 vector<PersnnHi. please find the answer above.. i have given comments so that it is very easy for you to understand the flow. In case of any doubts, please ask in comments. If the answer helps you, please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Design class Person with private fields name and social security number, and a public virtual member...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a class named Person and its two derived classes named Student and Employee. MakeFaculty and Staff derived classes of Employee

    In Java(The Person, Student, Employee, Faculty, and Staff classes)Design a class named Person and its two derived classes named Student and Employee. MakeFaculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). An employee has an office, salary, and date hired. Define a class namedMyDate that contains the fields year, month, and day. A faculty member has office hours and a rank....

  • What is the final value of the count field? public class Person { private String name;...

    What is the final value of the count field? public class Person { private String name; private int age; private static int count=0; public Person(String name, int age) { this.name = name; this.age age; count++; } public static void main(String[] args) { Person a - new Person("John Doe". 35): Person b new Person("Jane Doe", 38): for(int i=0;i<5;i++) Person tempa

  • 10. Observe the following simple program: public class Student : Person { private string name; private...

    10. Observe the following simple program: public class Student : Person { private string name; private List<string> courses; public void AddCourse(string course) { courses.Add(course); public void PrintCourses() { foreach(string course in courses) { Console.WriteLine(course); a) What is the access modifier of the member field name? b) What is the access modifier of the member method AddCourse? c) What is the immediate base class of Student?

  • {C++} Create a base class called Person with a public member function called sayHi which outputs...

    {C++} Create a base class called Person with a public member function called sayHi which outputs “Hi, I am a person.” Create a derived class called Student which inherits from the Person class (public inheritance) and overwrites the sayHi function and outputs “Hi, I am a student.” Create another derived class called EngineeringStudent which inherits from the Student class (public inheritance) and overwrites the sayHi function and outputs “Hi, I am an engineering student.” In the main program, create a...

  • Design a class named Person with fields for holding a person's name, address, and telephone number...

    Design a class named Person with fields for holding a person's name, address, and telephone number (all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes...

  • C++ write a class “Dog” with a private int field called months and two public member...

    C++ write a class “Dog” with a private int field called months and two public member functions setAge and GetStage. setAge takes as argument monthsToSet and sets the member field months. setAge returns void. getStage is a const member function that takes no argument and returns a string “Puppy” if the dog is less than 9 months old, “Adolescence” if more than 9 and less than 13 months, “Adulthood” if more than 13 and less than 60 months and “Senior”...

  • C++ Define the class HotelRoom. The class has the following private data members: the room number...

    C++ Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [3pts]. The constructors and the set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception...

  • Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person...

    Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee. A Person object has a name, address, phone number, and email address (all Strings). A Student Object has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable. An Employee Object has an office number, salary (both ints ), and a date hired. Use...

  • Part I: (The Myate class) Design a class named MyDate. The class contains: • The data...

    Part I: (The Myate class) Design a class named MyDate. The class contains: • The data fields year, month, and day that represent a date. Month is 0-based, i.e., 0 is for January. • A no-arg constructor that creates a MyDate object for the current date. • A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. • A constructor hat constructs a MyDate object with the specified year, month, and...

  • Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

    Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT