Thread: help with array of objects

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Lithuania
    Posts
    2

    help with array of objects

    ok, I'm really stuck with this for weeks, was trying to find an example, but no luck with what I want to do, basic tutorials also for some reason don't explain much about constructors and destructors

    what I want to do is have an admin class which will hold all the employee objects, can add them, list and calculate salaries
    I'm trying to make array of objects, not sure if it's right

    here is the code
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    class Employee {
    public:
    Employee(string name, short type, int salary)
    {
        _name = name; _type = type; _salary = salary;
    }
    ~Employee() {}
    private:
        string _name;
        short _type;
        int _salary;
    };
    
    
    class Admin {
    private:
        int e;
    
    
    public:
    Admin(){
    e=0;
    }
    
    
    void addEmployee(string name, int payPerMonth){                // not sure if this is the right way
        Employee employee = new Employee[e](name, 1, payPerMonth); // this part I don't know
        e++;
    }
    
    
    void addEmployee(string name, short payPerHour){
        //...
    }
    
    
    void calculateTotalPay(){
        //...
    }
    
    
    void printEmployees(){
        //...
    }
    
    
    };
    
    
    int main(){
    
    
    Admin admin();
    int input, salaryM;
    short salaryH;
    string name;
    
    
    while(true){
        cout << "1 - add employee(monthly), 2 - add employee(hourly), 3 - calculate employees salaries, 4 - list all employees" << endl;
        cin >> input;
        if (input==1){
            cout << "name: " << endl;
            cin >> name;
            cout << "salary: " << endl;
            cin >> salaryM;
            admin.addEmployee(name, salaryM);
        }
    }
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Note that this declares a function named admin that takes no arguments and returns an Admin:
    Code:
    Admin admin();
    How does your code not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    If you want to have an instance of a class which is what you are trying to do here
    Code:
    Admin admin();
    Just do this
    Code:
    Admin name_of_instance;
    Just as you usually do when you want to have an instance of a class for example std::string
    Code:
    std::string name_of_string
    In case your constructor requires some arguments for instance for your employee class,this is the syntax:
    Code:
    Employee Waras(waras,500,500);
    Then you can comfortably use your employee
    Code:
    std::cout<<waras.salary<<std::endl;
    Last edited by Aslaville; 06-27-2013 at 04:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array of objects
    By gavra in forum C++ Programming
    Replies: 20
    Last Post: 06-07-2009, 12:09 PM
  2. Array of Objects
    By Nightmaresiege in forum C++ Programming
    Replies: 9
    Last Post: 01-21-2008, 01:44 AM
  3. Array of Objects.
    By Mastadex in forum C++ Programming
    Replies: 12
    Last Post: 11-21-2004, 05:58 PM
  4. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  5. Help with Array for objects
    By curwa in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2003, 11:21 AM

Tags for this Thread