1

I have a homework task with a given main.cpp code which is not allowed to be changed. According to that main.cpp and simple input and output(which is down below) example I must to finish the program.

My tries are: I'm trying to create 4 classes, class Person; class Worker; class Student; class InService; in my main function through instantiating an object of InService class I pass 4 parameters (name, sex, studentNo, workerNo); and with help of pointer of type of Base class, have the desired output. The error it shows is:

[Error] no unique final overrider for 'virtual std::string Person::getName()' in 'InService' [Error] no unique final overrider for 'virtual int Person::getSex()' in 'InService'

I've tried to use virtual inheritance for that, but I can't really figure out how to solve this problem. I did some research on virtual inheritance, and referenced to other experts answers, but still getting confused with whole OOP stuff.

//Inservice.h
#include<string>
using namespace std;
class Person{
    public:
        Person();
        ~Person();      
        string name;
        int sex;
        virtual string getName() = 0;
        virtual int getSex()  = 0;
};
///////////////////////////////////////////////////
class Student:virtual public Person{
    public:
        Student();
        ~Student();
        string sno;
        
        virtual string getName() {
        return name;
        }
        
        virtual int getSex(){
            return sex;
        }
        
        string getSno(){
            return sno;
        }
};
//////////////////////////////////////////////////
class Worker:virtual public Person{
    public:
        Worker();
        ~Worker();
        string wno;
        
        virtual std::string getName(){
        return name;
        }
        
        virtual int getSex(){
            return sex;
        }
        
        string getWno(){
            return wno;
        }
};
///////////////////////////////////////////////////////
class InService: public Student, public Worker{
    public:
    InService(string _name, int _sex, string _sno, string _wno){
        Person::name = _name;
        Person::sex - _sex;
        Worker::wno = _wno;
        Student::sno = _sno;
    }
};
///////////////////////////////////////////////////////

//main.cpp
#include <iostream>
#include "inservice.h"
using namespace std;
 
int main() {
    string name, sno, wno;
    int sex;

    cin >> name;
    cin >> sex;
    cin >> sno;
    cin >> wno;

    InService is(name, sex, sno, wno);

    Person* p = &is;
    Student* s = &is;
    Worker* w = &is; 
 
    cout << p->getName() << endl;
    cout << p->getSex() << endl;
    
    cout << s->getName() << endl;
    cout << s->getSex() << endl;
    cout << s->getSno() << endl;
    
    cout << w->getName() << endl;
    cout << w->getSex() << endl;
    cout << w->getWno() << endl;
    return 0;
}

Suppose my input is:

Jack  
1 //1-for male; 0 -for female  
12345678 //studentNo
87654321  //workerNo  

I expect the output to be:

Jack  
1  
12345678   
Jack  
1  
87654321  
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 2
    why getname and getSex are implemented twice exactly with the same code? are you sure it is necessary? – Federico May 22 '19 at 14:55
  • 2
    How should `InService` know which function it should call? It has 2 different implementations to choose from. `virtual` inheritance helps in case *base* class has method implemented. – Yksisarvinen May 22 '19 at 14:56
  • Why is `sex` and `name` property of students and workers? Does person not have those? – Quimby May 22 '19 at 15:02
  • @Federico so i was following this tutorial, and as i understood it is to made to use the same function name to operate on different objects. For example, base class is shape and it has pur virtual function getArea; we also have derived classes such as Rectangle, Circle and Triangle. Since for each shape it has different calculations, hence we need to use virtual fucntion. https://youtu.be/ng98qapa4Sw?t=1319 –  May 22 '19 at 15:04
  • 1
    is there actually any difference between a `Worker` and a `Student` ? I have the feeling your design needs a fix, but I dont really understand what you want to design for, it rather seems like you could forget about inheritance all togther to achieve similar – 463035818_is_not_an_ai May 22 '19 at 15:04
  • @Quimby Didn't i declare sex and name variables in Person class ? and Students and Workers get it by inheritance ? –  May 22 '19 at 15:06
  • @Dr.raider yes but there is no need to make the getters virtual you can define them in the base – 463035818_is_not_an_ai May 22 '19 at 15:07
  • The thing it is a school task,and the main.cpp code runs on the server, so i can't change it, i only need to implement the header file code. So by looking at that main function code i thought about inheritance. @formerlyknownas_463035818 –  May 22 '19 at 15:21
  • ok then I understand. Would be good if you included that information in the question. Not being allowed to change the `main` but having to fill the holes is quite essential to understand your problem – 463035818_is_not_an_ai May 22 '19 at 15:26
  • once this out of the way, it is also clear that you do not necessarily have to use a diamond. Now its basically up to you if your question is targeted at fixing your diamond or solving the actual task ;) – 463035818_is_not_an_ai May 22 '19 at 15:28

1 Answers1

0
 InService(string _name, int _sex, string _sno, string _wno){
        Person::name = _name;
        Person::sex - _sex;
        Worker::wno = _wno;
        Student::sno = _sno;
    }

There's a typo there, Person::sex - _sex; should be Person::sex = _sex;

Also you can remove name and sex virtual function and have it just a standard function in Person, since it's exactly the same for all classes that derive from it. That will remove the ambiguity of which getName and getSex function that InService class virtual table needs to point to.

noobius
  • 1,529
  • 7
  • 14