Question

I got a homework question, I can't figure out how to do it. The program needs...

I got a homework question, I can't figure out how to do it.

The program needs to be in C++ 11 using namespace std;

The student class defines the strings studentName and studenSSN as protected data members. A constructor has arguments that initialize the data. The class StudentAthlete is derived from the student class and has a private data member studentSport, which describes the sport the student plays. Both classes have a member function, identity(). The base class student outputs the student information in the form:

student: studentName SSN: studentSSN

The identity() function in the base class should support polymorphism and should be overriden in the derived class to add the information - Sport: studentSport. Finally, the class studentWorker is also derived from the Student class and includes the data member studentDept, which describes the department where the student works. The identity function for this class should report the student name, the student's social security number, and the department where the student works.

Define these three classes and write a test program that creates a vector of one Student object, one studentAthlete object, and one StudentWorker object. In a loop, execute the identity() function for each object so that the information for each object is displayed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

------------------------------------------------------------

Program

//Header files
#include <iostream>
#include<string>
#include<vector>
using namespace std;
//Create a parent class
class Student {
   //Instance variables
private:
   string studentName;
   string studentSSN;
public:
   //Constructor
   Student(string name, string ssn) {
       studentName = name;
       studentSSN = ssn;
   }
   //Method
   virtual string identify() {
       return "student: " + studentName + " SSN: "+studentSSN;
   }
};
//Child class
class StudentAthlete :public Student {
   //Instance variables
private:
   string studentSport;
public:
   //Constructor
   StudentAthlete(string name, string ssn, string sport) :Student(name, ssn) {
       studentSport = sport;
   }
   //Override
   string identify() {
       return Student::identify()+" Sport: "+studentSport;
   }
};
//child class
class StudentWorker:public Student {
   //Instance variables
private:
   string studentDept;
public:
   //constructor
   StudentWorker(string name, string ssn, string dept) :Student(name, ssn) {
       studentDept = dept;
   }
   //Override
   string identify() {
       return Student::identify() + " Department: " + studentDept;
   }
};
int main()
{
   //Crate vector objects
   vector<Student> student1;
   Student s("Harsha", "S-1");
   student1.push_back(s);
   vector<StudentAthlete> student2;
   StudentAthlete sath("Harsha", "S-1", "Badminton");
   student2.push_back(sath);
   vector<StudentWorker> student3;
   StudentWorker swk("Harsha", "S-1", "Physics");
   student3.push_back(swk);
   //Loop to display
   for (int i = 0; i < 3; i++) {
       if (i == 0) {
           cout << student1.at(0).identify() << endl;
       }
       else if (i == 1) {
           cout << student2.at(0).identify() << endl;
       }
       else {
           cout << student3.at(0).identify() << endl;
       }
   }
}

---------------------------------------------

Output

student: Harsha SSN: S-1
student: Harsha SSN: S-1 Sport: Badminton
student: Harsha SSN: S-1 Department: Physics

Add a comment
Know the answer?
Add Answer to:
I got a homework question, I can't figure out how to do it. The program needs...
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
  • 13] Use UML notations to describe the following classes and their relationships. Define relevant private data,...

    13] Use UML notations to describe the following classes and their relationships. Define relevant private data, and the public methods for each class. a) Define a class Person that defines a generic person. b) Define a class Card that define a generic card c) Define a derived class from Person that describes a typical Student. d) Define a derived class from Card that describes a typical Student Card e) Define a derived class from Card that describes a typical Credit...

  • question

    : Implement the following given scenario in C++ a. Create an inheritance hierarchy by writing the source code, containing base class BankAccount and derived classes SavingsAccount and CheckingAccount that inherit from class BankAccount. b. Implement Polymorphism where required to achieve the polymorphic behavior. c. Multiple constructors be defined for initializing the account of a user. d. The class should provide four member functions. i. Member function Credit should add an amount to the current balance. ii. Member function Debit should...

  • 2- Write a program that has a Person class, a Student class, that is derived from...

    2- Write a program that has a Person class, a Student class, that is derived from Person, and a Faculty class, derived from Person as well, with the following specifications: The Person class has the following members: a. An integer private member variable named SSN. b. Two string protected member variables named firstName and lastName. C. An integer protected member variable named age. d. A constructor that takes SSN, first name, last name, and age as parameters. e. A constructor...

  • I am having difficulty with completing this task. thank you. Task 10.1 (a) Define a C++...

    I am having difficulty with completing this task. thank you. Task 10.1 (a) Define a C++ base class named Rectangle containing length and width data members. From this class, derive a class named Box with another data member named depth. The member functions for the base class Rectangle should consist of a constructor and an area() function. The derived class Box should have a constructor, a volume() function and an override function named area() that returns the surface area of...

  • Create a class hierarchy to be used in a university setting. The classes are as follows:...

    Create a class hierarchy to be used in a university setting. The classes are as follows: The base class is Person. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The class should also have a static data member called nextID which is used to assign an ID number to each object created (personID). All data members must be private. Create the following member functions: o Accessor functions to allow access to first name and last name...

  • I need this in C++. This is all one question. Introduction Your eighth assignment will consist...

    I need this in C++. This is all one question. Introduction Your eighth assignment will consist of two programs, which will involve the use of simple classes. The source code for these problems should be submitted using the naming conventions we specified in class. Please note that your computer programs should comply with the commenting and formatting rules as described in class. For example, there should be a header for the whole program that gives the author's name, class name,...

  • Question 11     The feature that enables you to split source code between two or more...

    Question 11     The feature that enables you to split source code between two or more files is: Select one: a. base class b. partial classes c. generics d. dynamic link library e. package Question 12     Packaging data attributes and behaviors into a single unit so that the implementation details can be hidden describes an object-oriented feature called: Select one: a. abstraction b. objects c. inheritance d. polymorphism e. encapsulation Question 13     A multitier application would probably have:...

  • Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class...

    Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class Account and derived class Savings-Account. Base class Account should include one data member of type double to represent the account balance. The class should provide a constructor that receives an initial baiance and uses it to initialize the data member. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money...

  • Create a base class and two derived classes. Also, create and call a member function named...

    Create a base class and two derived classes. Also, create and call a member function named communicate() that behaves differently when called by each class. Create a single C++ project (and CPP source file) named animal_communication as follows: Into this source, type the source code for Program 15-16, "Program Output," in Section 15.6, "Polymorphism and Virtual Member Functions," in Ch. 15, "Inheritance, Polymorphism, and Virtual Functions," of Starting Out With C++ From Control Structures Through Objects. Run and debug the...

  • Please help me with the following question. This is for Java programming. In this assignment you...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

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