C++ Class Inheritance: Functions -
i've been doing coursework programming module of physics degree i've been having trouble. had make class called person , subclass of called employee such that: person.hpp:
#ifndef person_hpp_ #define person_hpp_ class person { public: person(const std::string & name="anonymous"): name(name) {;} ~person() {;} std::string getname(){ return name; } void setname(std::string newname) { name = newname; } void print(); private: std::string name; }; #endif /* person_hpp_ */
person.cpp:
void person::print(){ std::string name = person::getname; std::cout << name << std::endl; }
employee.hpp:
#ifndef employee_hpp_ #define employee_hpp_ class employee: public person { public: employee(const std::string & name, const std::string & job) : name(name), job(job){;} ~employee() {;} std::string getjob(){ return job; } void setjob(std::string newjob) { job = newjob; } void print() const; private: std::string job; }; #endif /* employee_hpp_ */
employee.cpp:
void employee::print(){ person::print(); std::string job = employee::getjob; std::cout << job << std::endl; }
main.cpp:
#include <iostream> #include <string> #include <vector> #include "person.hpp" #include "person.cpp" #include "employee.hpp" #include "employee.cpp" #include "friend.hpp" #include "friend.cpp" int main() { return 0; }
the error in employee.cpp. when building error shows: ../employee.cpp:10:6: error: use of undeclared identifier 'employee'
i realise have made basic mistake frustrating me cannot see it.
any great! in advance, sean cooper
n.b. purpose of employee.cpp print name of employee along associated job.
your include
's should this:
person.cpp:
#include <iostream> #include <string> #include "person.hpp"
employee.cpp:
#include <iostream> #include <string> #include "employee.hpp"
main.cpp
#include <iostream> #include <string> #include <vector> #include "person.hpp" #include "employee.hpp" #include "friend.hpp"
that is, each .cpp
(implementation) includes respective .hpp
(interface) along additional headers needed (like <string>
). main.cpp
includes needed headers no other .cpp
file. compiler parse .cpp
files individually , linker link results executable. rule of thumb, never include .cpp
anywhere.
the specific error when compiler sees
void employee::print()
and doesn't know employee
is. including employee.hpp
fixes bringing in employee
's definition.
Comments
Post a Comment