Re: constructors

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 09/01/04

  • Next message: Anand Hariharan: "Re: Compilation error with function object predicate"
    Date: Tue, 31 Aug 2004 22:05:11 GMT
    
    

    "iwasinnihon" <iwasinnihon@hotmail.com> wrote in message
    news:4134e663$0$75878$3a2ecee9@news.csolutions.net...
    > I am a little new to c++ programming and I don't really understand
    > constructors. I am writing a program for school and would just like some
    > help. I have to write a constructor and then pass information in to
    create
    > a object dynamically. Here is the code that I have so far. What am
    > missing? I'd appreciate any pointers you can give me.
    >
    > ------------------
    > class
    > -------------
    > class Employee
    > {
    > private:
    > int empNumber;
    > char address[MAX];
    > char phoneNumber[MAX];
    > int deptNumber;
    > char name[MAX];
    > int weeklySalary;
    > float commission;
    > int revenue;
    > public:
    >
    > //constructor
    > Employee(int, char*, char*, int, char*, int, float);
    >
    > char* getEmp();
    >
    > int setValue(int);
    >
    > int calcPay();
    > };
    > -------------------
    > Here is my implementation
    > --------------------
    > Employee::Employee(int empNumberIn,
    > char* addressIn[],
    > char* phoneNumberIn[],
    > int deptNumberIn,
    > char* nameIn[],
    > int weeklySalaryIn,
    > float commisionIn):
    >
    > empNumber = empNumberIn;
    > strcpy(address, addressIn);
    > strcpy(phoneNumber, phoneNumberIn);
    > deptNumber = deptNumberIn;
    > strcpy(name, nameIn);
    > weeklySalary = weeklySalaryIn;
    > commission = commissionIn;
    > (}
    > --------------------
    > Here is my code for creating the object
    > ---------------------
    > Employee* empPtr = new Employee(
    > 1,
    > "12 Madison Ave",
    > "123-4567",
    > 100,
    > "I. M. Great",
    > 1000,
    > 0.4);

    #include <string>
        using std::string;

    class Employee
    {

        int empNumber;
        string address;
        string phoneNumber;
        int deptNumber;
        string name;
        int weeklySalary;
        double commission;
        int revenue;

    public:
        Employee(int,
                 const string&,
                 const string&,
                 int,
                 const string&,
                 int,
                 double);

        const string& getEmp() const;
        int setValue(int);
        int calcPay() const;
    };

    Employee::Employee(int empNumberIn,
                       const string& addressIn,
                       const string& phoneNumberIn,
                       int deptNumberIn,
                       const string& nameIn,
                       int weeklySalaryIn,
                       double commissionIn) :
                            empNumber(empNumberIn),
                            address(addressIn),
                            phoneNumber(phoneNumberIn),
                            deptNumber(deptNumberIn),
                            name(nameIn),
                            weeklySalary(weeklySalaryIn),
                            commission(commissionIn),
                            revenue(0)
    {
    }

    int main()
    {

        Employee emp(1,
                     "12 Madison Ave",
                     "123-4567",
                     100,
                     "I. M. Great",
                     1000,
                     0.4);

        return 0;
    }

    -Mike


  • Next message: Anand Hariharan: "Re: Compilation error with function object predicate"

    Relevant Pages

    • Re: Why copy constructor is not called
      ... Copy constructors should use the syntax "Class". ... the assignment operator should take const reference arguments. ... here is a sample program that I'm wondering why copy constroctor is ... int, and then from this int using the int constructor to build new object ...
      (microsoft.public.vc.language)
    • Re: does Ruby not support multiple "initialize" methods for a class???
      ... and/or overloaded constructors. ... One advantage of static factory methods is that, unlike constructors, they have names. ... overloading in general isn't such a good idea either. ... def m int ...
      (comp.lang.ruby)
    • Cascading through the constructors
      ... As a human, i'm lazy, so if i need to write a class with three constructors, i'd like to minimize the work. ... C (int i, int j) } ... Constructors calling a common method. ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: How to escape the Ocamls superfluous parentheses and type declarations?
      ... > The constructor Mycons expects 2 argument, ... author of Camlp4), though, IIRC, application for constructors is done ... > Besides, if Int and Char are functions, then are they defined in terms ... > terms of the functions Int and Char (which would require Int and Char ...
      (comp.lang.ml)
    • Re: ECMA Doc 8.8.4 Error
      ... Special member functions like default constructors, ... SMF support has been withdrawn. ... > int Xor; ...
      (microsoft.public.dotnet.languages.vc)