1

I have an employee class and a server class and am trying to figure out why the printing of the server data in the main function isn't taking the server print() function and using it.

Employee.h:

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>

using namespace std;

class Employee
{
public:
    virtual void job();
    void print();
};
#endif

Employee.cpp:

#include "Employee.h"

void Employee::job() {
    cout << "Employee status yet to be determined.\n" << endl;
}

void Employee::print() {
    cout << "New employee\n" << endl;
}

Server.h:

#ifndef SERVER_H
#define SERVER_H
#include "Employee.h";
#include <iostream>

using namespace std;

class Server : public Employee
{
public:
    void job();
    void print();
};
#endif

Server.cpp:

#include "Server.h"

void Server::job() {
    cout << "Serve tables\n";
    print();
}

void Server::print() {
    "I am a server!\n";
}

main:

#include "Employee.h"
#include "Server.h"
#include <iostream>

void output(Employee* employee) {
    employee->job();
}

int main()
{
    Employee* a = new Server;
    Employee* b = new Employee;

    output(a);
    a->print();

    output(b);
    b->print();

    return 0;   
}

Just trying to wrap my head around using virtual functions and polymorphism.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • Beside the answer given below : You have memory leaks to in your code, for every `new` there should be a `delete`. C++ actually recommends using [std::make_unique](https://www.learncpp.com/cpp-tutorial/stdunique_ptr/) nowadays to avoid these kind of "missing delete" bugs. – Pepijn Kramer Oct 26 '22 at 04:56
  • Side note : Not really important for code like this, but important for bigger designs later. And to put a little seed in your head :) From a design point of view, is printing really something a Server or Employee can do itself? I know I can't print myself, I have a printer for that. Have a look at this : https://www.learncpp.com/cpp-tutorial/overloading-the-io-operators/ – Pepijn Kramer Oct 26 '22 at 04:56
  • You should remove both `using namespace std;` and `#include ` from your header file. Place the include in the source file and read [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721) – Costantino Grana Oct 26 '22 at 05:51

1 Answers1

2

The print function is not declared virtual, so there is no polymorphism in that call. What will happen is that each of your calls will call Employee::print because both a and b are of type Employee*.

Change your class definition to this:

class Employee
{
public:
    virtual void job();
    virtual void print();
};

You should also define a virtual destructor if you plan to delete objects through the base pointer.

class Employee
{
public:
    virtual ~Employee() {}

    virtual void job();
    virtual void print();
};
paddy
  • 60,864
  • 6
  • 61
  • 103
  • okay I've tried making the print function virtual but that didn't solve the problem. Do I have to call it differently in the main once I change the function definition inside the class? Edit: didn't see your full answer at first, give me a sec – ndalzell1219 Oct 26 '22 at 04:33
  • Did you make the `print` function virtual _in the base class_ like I showed? – paddy Oct 26 '22 at 04:34
  • okay so yes I made the print function virtual inside the Employee class and added the virtual destructor. The main function will still not print off the "I am a server!" string when I call a->print() – ndalzell1219 Oct 26 '22 at 04:37
  • Look at your implementation of `Server::print` closely. Do you see it using `std::cout`? All I see is a statement containing a string literal on its own, which does nothing at all. See https://godbolt.org/z/K1Ge4bn3W – paddy Oct 26 '22 at 04:39
  • wow yes I am a huge idiot. I did not add the cout to the print in the server print method. thanks *facepalm* – ndalzell1219 Oct 26 '22 at 04:53