Two Object related question

This is a discussion on Two Object related question within the C++ Programming forums, part of the General Programming Boards category; How does one make an object null? I think in Java, one could merely do Code: anObject = null; but ...

  1. #1
    Some Guy
    Join Date
    Jul 2004
    Posts
    32

    Two Object related questions

    How does one make an object null? I think in Java, one could merely do
    Code:
    anObject = null;
    but I could be wrong. Basically, I have an array of objects and I want to delete one of the objects. I think null is the way to do it, no?

    Secondly, I have this..

    Code:
    class EmpArys
    {
    	public:
    		EmpArys();
    		int findEmp(int pSocSecNbr);
    		Employee getEmp(int pSocSecNbr);
    		bool addEmp(Employee pEmp);
    		bool delEmp(int pSocSecNbr);
    		string toString();
    		string getPayList(Tax pTaxTable);
    	private:
    		Employee empList[100];  //i've temporarily put 100 there
    		int empCount;
    		const int maxNumOfEmps, empNotFound;
    };
    I can't figure out how to instaniate empList to maxNumOfEmps and also to instantiate maxNumOfEmps to 100 and empNotFound to -1.
    I can't instantiate them in the constructor because they are constant and not in the class either. Major help needed.

    I tried this for instantiating the object in the constructors, which would work in Java (I think), but not in C++
    Code:
     EmpArys::EmpArys()
    {
    empArys[maxNumOfEmps];
    }
    Thanks, I greatly appreciate the help.
    Last edited by gflores; 08-17-2004 at 10:26 PM.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    well, objects in java are a bit different that in c++. In Java you have low level types: int, char, but you also have Objects: Integer, String and so on.

    In your case, you cannot just make an object of type EmpArys=null, you need you constructor, set all the object's variables.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    Code:
    EmpArys::EmpArys()
    {
    empArys[maxNumOfEmps];
    }
    I change it like this:
    Code:
    class newEmpArys::EmpArys()   //define another class
            : EmpArys
    {
           empArys[maxNumOfEmps];
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    Const data members need to be set in a class' initializer list. This is the only place they can be set upon creation of an instance of a particular object. The following modifications will allow you to create an EmpArys object with a value for maxNumOfEmps that you specify when instantiating an object. You can specify a value or not in which case a default value of 100 will be given. empNotFound gets set to -1.
    Code:
    class EmpArys
    {
    public:
            EmpArys(const int maxEmps);
            ~EmpArys();
            int findEmp(int pSocSecNbr);
            Employee getEmp(int pSocSecNbr);
            bool addEmp(Employee pEmp);
            bool delEmp(int pSocSecNbr);
            string toString();
            string getPayList(Tax pTaxTable);
    private:
            Employee* empList;
            int empCount;
            const int maxNumOfEmps, empNotFound;
    };
    
    EmpArys::EmpArys(const int maxEmps = 100) : maxNumOfEmps(maxEmps), empNotFound(-1)
    {
        // Dynamically allocate a chunk of memory to hold maxNumOfEmps Employees
        empList = new Employee[maxNumOfEmps];
    }
    
    EmpArys::~EmpArys()
    {
        // Clean up dynamically allocated memory
        delete [] empList;
    }
    ...
    ...
    ...
    EmpArys a1;         // maxNumOfEmps for object a1 gets default value of 100
    EmpArys a2(200);    // maxNumOfEmps for object a2 gets value of 200
    
    I used to be an adventurer like you... then I took an arrow to the knee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to BGI
    By progue in forum Windows Programming
    Replies: 6
    Last Post: 05-25-2008, 10:04 AM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  3. A question related to dynamic memory allocation
    By spiit231 in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 12:25 AM
  4. Question..Help..Real Number Object ?
    By Meth in forum C++ Programming
    Replies: 5
    Last Post: 10-01-2005, 10:11 PM
  5. keystrokes related question..
    By AmiTsur in forum C Programming
    Replies: 3
    Last Post: 11-10-2002, 08:22 AM

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