-
Help~~
Hi
I am a beginner of C++.
Now I am going to write a inheritance program.
The following is the introduction of my program.
a. Implement the class "Employ" that have:
¡P protected data members name and salary which store the name of Employs and monthly salary;
¡P getName() retrieves the name of employs.
¡P calculateSalary() retrieves the salary.
b. Implement the Derive subclasses "Full-time" and ¡§Part-time¡¨. The calculateSalary() will calculate salary based on the following rules. printAll() function prints all information.
¡P For the Full-time employee, data member year stores the number of year the employ worked. If the year is > 15 years, then the salary would be increased by 20%, if the year is <= 15 years, then the salary would be increased by 10%.
¡P For the part-time employ, data member hourlyPay stores the hourly salary and hours store the number of hour the employ worked. The formula for calculateSalary() is ¡§salary =base-salary +hours * hourlyPay¡¨.
==================================================
I want my program include 7 files.
1.main.cpp (I have attached it)
2.employ.cpp
3.employ.h
4.fulltime.cpp
5.fulltime.h
6.parttime.cpp
7.parttime.h
Employ.h as following
=====================
#ifndef Employ_H
#define Employ_H
class Employ
{
public:
char* getName();
float calculateSalary();
void printAll();
protected:
char* Name;
float salary;
};
#endif
=================================
And I am writing the Employ.cpp.
But I don't know how to write the function of "getName", as it is use char pointer with it.
Can anyone help me solve this question?
Thanks a lot~
-
char *getName() const
{
return getName;
}
However this may be somewhat dangerous. For example:
main() {
char *name;
Employ e1;
name = e1.getName();
}
Now by using name afterwards you can change the e1's name field. You only want to change e1's name with setName function.
The new one should be safer.
char *getName()
{
char *returnName;
returnName = new char[strlen(name + 1)];
strcpy(returnName, name);
return returnName;
}
which is safer because you cannot change e1's name by using name in main.
If you want to an easy approach use string class :)
-
when you have pointers as members you MUST provide at least a default constructor,destructor,copy constructor and assignment operator. remember that you have to dynamically allocate and free memory associated with that pointer. think about why you cannot rely on the default compiler produced functions.Do not write any other functions for your class until you have at least these four most important functions written correctly or you will just be flogging a dead horse.
sorry have to go out now so no time to show you some code but have another go and come back with a skeleton class and i will help later.
-
In fact, this is the first time that I write a program by using over one cpp file.
Can anyone tell me what the difference of only one cpp file and over one cpp file to run the program?
Can give me an example like how can I write employ.cpp?
'Coz I just know employ.h is for declaration and employ.cpp is for function.
But inside employ.cpp, I need to write main() or not?
if needed, what kind of content need to write in main()?
-
Actually, instead of creating the mess of allocated memory someone has to free, use this:
const char* getName() const
{
return getName;
}
-
You should write only the definitions of the Employ class functions into employ.cpp and the class interface (members and function prototypes) should be in Employ.h. You include Employ.h in Employ.cpp.
In main, you don't write the algorithms, you just call the functions of the various classes which contains the algorithm. But you again include Employ.h in main.cpp.
-
k im feeling a little generous today so i will show you how to build your employ class
Code:
#include<cstring>
using namespace std;
class Employ
{
private: float Salary;
char* Name;
public: Employ();
Employ(float,char*);
Employ (const Employ&);
virtual ~Employ();
Employ& operator =(const Employ&);
const char* GetName()const {return Name;}
const float GetSalary()const {return Salary;}
};
Employ::Employ()
{
Name=new char[1];// make room for the null byte
if(Name) *Name='\0';// add null byte if new was successful
Salary=0.0;
}
Employ::Employ(float Sal,char* N)
{
Name=new char[strlen(N)+1];
if(Name) strcpy(Name,N);
Salary=Sal;
}
Employ::Employ(const Employ& E)
{
Name=new char[strlen (E.GetName())+1];
if (Name) strcpy(Name,E.GetName());
Salary=E.GetSalary();
}
virtual Employ::~Employ()
{
if (Name) delete[] Name;
}
Employ& Employ::operator =(const Employ& rhs)
{
if (this==&rhs) return *this;
if(Name) delete[] Name;
Name=new char [strlen (rhs.GetName())+1];
if (Name) strcpy(Name,rhs.GetName());
Salary=rhs.GetSalary;
return *this;
}
That gives you a small framework from which to continue building. Make Calc.Salary a virtual function of Employ and override it in the derived classes.