Hey all, in my program I have a Parent class Employee and a class that derives from it. In the Employee parent class I have a virtual function. In my main.cpp file I use polymorphism to create a pointer variable of class Employee and set it equal to the reference address of my derived class object. When I try and call my function using the Employee pointer it should call the function that is in my derived Class. But in the output console all I see is "lldb" and a thread break point that highlights the function in the derived class .cpp file that Im trying to call.
Heres the code incase my explanation is confusing :)
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include "Employee.h"
#include "Dave.h"
int main(int argc, constchar * argv[])
{
Dave d("David McMahon", 21, "Caherleaheen Tralee", 8.65);
d.displayEmployee();
Dave k("Kevin O Shea", 34, "Tralee", 12.45);
Employee *e = &k;
e->displayName();
return 0;
}
#ifndef Inheritance_Employee_h
#define Inheritance_Employee_h
#include <iostream>
#include <string>
class Employee
{
private:
std::string name;
int age;
std::string address;
float salary;
public:
Employee(std::string n, int a, std::string addr, float s);
~Employee();
void displayEmployee();
virtualvoid displayName()
{
std::cout << "\nName of this object is: " << name << std::endl;
}
};
#endif
Dave.cpp (derived class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "Dave.h"
Dave::Dave(std::string n, int a, std::string addr, float s): Employee(n,a,addr,s)
{
}
Dave::~Dave()
{
}
void Dave::displayName()
{
std::cout << "\nVirtual Function in class Dave" << std::endl; // Thread break point at this function
}
If anyone could help me with this error/breakpoint I would appreciate it!
Are you using XCode? If so you might have created a breakpoint by accident at that line. When breakpoints are enabled you have to manually step through them, so thats why your program is hanging.
Yes Im using XCode and Id say your right! :) and yes moorecm there is a Dave.h file there but seeing as its just a header file for the functions and constructors I thought Dave.cpp would be enough to post. Thanks guys! :)