Should i use inheritance or it could be build in simple classes. |
Sometimes inheritance (generalisation in UML speak) falls out of a design because it's obvious. But you shouldn't try to use it.
For example, let's consider Employee from your previous work. You could take the view that there's an Employee base class and derived kinds of Employees like Programmer and Tester. But that's contrived. If you think that a person can be a Team Leader and a Programmer at the same time, the model doesn't fit.
An alternative model is an Employee has Roles. The role can be a job. That could better represent someone being a Manager on one project, and a Team Leader on another while being a Programmer at the same time. In UML, you'd say an Employee is associated with one or more Roles.
Regarding your non-working example, you should NEVER USE PROTECTED DATA. There are two relevant abstractions here, Abstract Data Types and Objects.
Abstract Data Types are black boxes. You never see what's inside, you only access the functions that interface to it. For example a File. In the C standard library, there's FILE. You create one with
fopen()
, close it with
fclose()
, read from it with
fread()
, write to it with
fwrite()
and so on, but you never look at FILE.
Objects are black boxes too, but the model expresses relationships with other Objects. You can only send an object a message and get a reply; and that's it. Objects implement methods that implement the interface. In C++, objects are efficiently implemented by representing methods as virtual functions so the system can be validated at compile time (rather than when the program is running).
None of these models support protected or public data, and neither should you.
Your code wasn't working because you have a syntax error, you omitted the namespace for string, and you have the wrong header file. The code should be:
1 2 3 4 5 6 7 8 9 10 11
|
#include <string>
class Employe
{
private:
std::string employeName;
std::string employeSex;
std::string employeJob;
std::string employeOrganization;
std::string employeBirthday;
};
|