Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

12 - Virtual and Pure Virtual Function C++ Advance Please only answer this quest

ID: 3697987 • Letter: 1

Question

12 - Virtual and Pure Virtual Function

C++ Advance

Please only answer this question if your code is 100%. Include your answer in text so I may copy and paste the code into visual studio. The same applies for the individual questions. Please only answer if you have the correct answer. Thank you.

1.Create class Person as following

class Person

{

private:

   string name;

public:

   Person();

   virtual void getData();

   virtual void print()=0;

   void printName();

NOTE: that the print() function is pure virtual function. That means you have to implement print() function in derived class

2.Derive class Employee from Person class. And the following private member variables:

A.Salary

B.Title

3.Derive class Student from Person class, Add GPA as private member.

4.Override getData() and print() function in Employee and Student class

5.Write a test program, add 2 functions in the test program:

A.Call_getData() pass Person object by reference or pointer

B.Call_print() pass Person object by reference or pointer

C.Show the use of the virtual functions.

6.Write another test program to show in what case the slicing problem happens and how to fix slicing problem by using pointers

Explanation / Answer


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


static int prof_id = 0;
static int stud_id = 0;
class Person{
private:
   string name;
   int age;
public:
   virtual void getdata() = 0;
   virtual void putdata() = 0;
   void putname(string s){
       this->name = s;
   }
   void putage(int age){
       this->age = age;
   }
   string printname(){
       return this->name;
   }
   int printage(){
       return this->age;
   }
};

class Employee: public Person{
private:
   int publications;
public:
   void getdata(){
       string dummyname;
       cin>>dummyname;
       putname(dummyname);
       int dummyage;
       cin>>dummyage;
       putage(dummyage);
       cin>>this->publications;
   }
   void putdata(){
       cout<<printname()<<" "<<printage()<<" "<<publications<<" ";
       cout<<++prof_id<<endl;
   }
};
class Student: public Person{
private:
   int marks[6];
public:
   void getdata(){
       string dummyname;
       cin>>dummyname;
       putname(dummyname);
       int dummyage;
       cin>>dummyage;
       putage(dummyage);
       for(int i = 0; i < 6 ;i++)cin>>marks[i];
   }
   void putdata(){
       int totalmarks = 0;
       for(int i = 0; i <6 ;i++)totalmarks+=marks[i];
       cout<<printname()<<" "<<printage()<<" "<<totalmarks<<" ";
       cout<<++stud_id<<endl;
   }
};


int main(){

    int n, val;
    cin>>n; //The number of objects that is going to be created.
    Person *per[n];

    for(int i = 0;i < n;i++){

        cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Employee
            per[i] = new Employee;

        }
        else per[i] = new Student; // Else the current object is of type Student

        per[i]->getdata(); // Get the data from the user.

    }

    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.

    return 0;

}