Help~~

This is a discussion on Help~~ within the C++ Programming forums, part of the General Programming Boards category; Hi I am a beginner of C++. Now I am going to write a inheritance program. The following is the ...

  1. #1
    joy
    joy is offline
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Unhappy 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~
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    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

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    joy
    joy is offline
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Question

    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()?

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Actually, instead of creating the mess of allocated memory someone has to free, use this:

    const char* getName() const
    {
    return getName;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    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.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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.
    Last edited by Stoned_Coder; 11-24-2001 at 08:49 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help~!~
    By cude87 in forum C++ Programming
    Replies: 16
    Last Post: 10-02-2004, 06:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21