Thread: Need Big Solution For Small Problem

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Arrow Need Big Solution For Small Problem

    I am trying to create objects of classes inside functions, and then using those objects in main(). Here is my code, followed by the problem.
    Code:
    #include <iostream>
    #include <cstdlib>
    
    void createDates();  // notice this
    
    /////////skip this//////////////////////////
    
    class Date
    {
     private:
        int month, day, year;
     public:
        Date(int m = 0, int d = 0, int y = 0)
          {month = m; day = d; year = y;}
        void setDate(int m, int d, int y){month = m; day = d; year = y;}
    };
    
    
    class Employee
    {
     private:
            int id, salary;
            Date hired;
     public:
      Employee(int, int, Date);
    };
    
    Employee::Employee(int i, int s, Date h)
    {
     id = i; salary = s;
     hired = h;
    }
    
    //////////////stop skipping////////////////////////////
    
    int main()
    {
     createDates();   // this executes before anything else in main.
     Date today(7, 31, 2002);  // object to be used in future lines
    
     Employee jake(1, 32900, today); // uses the Date object created above 
     Employee brian(2, 34000, tomorrow); // uses the object created in function
     
     system("pause");
     return 0;
    }
    
    void createDates()
    {
     Date tomorrow(8, 1, 2002);
    }
    main() begins by calling createDates(), which creates one Date object called tomorrow.
    Then, a date object is created called today directly in main().
    The code then goes on to create Employee objects, using Date objects as their last argument/parameter thingy. jake will work fine, but brian will not, because its class object parameter/argument thingy was declared outside main().

    I need to have tomorrow declared in a separate function. Need to!! how can I have Employee objects recognize Date objects declared in such functions?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    That's not the problem.

    Note that tommorow only exists in the scope of the function. Once you return from createDates(), tommorow will be gone!!

    I don't see why you would want to do that anyway.

  3. #3
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    ooooh.

    is there anyway to err... have it visible to the whole file?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  4. #4
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    woot found a way. i just declared tomorrow globally...
    Code:
    class Date
    {
        // ...
    }tomorrow;
    then initialized it using

    Code:
    void createDates()
    {
     tomorrow.setDate(arguments);
    }
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    A simple object resolution problem... and you figured it out yourself

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You would have to have your function return a pointer to a dynamically allocated object:
    Code:
    int main()
    {
        Date* tomorrow = createDates();
        ...
        ...
        ...
        return 0;
    }
    
    Date* createDates()
    {
        // Return a dynamically allocated date object.  Create
        // a constructor that can handle arguments to initialize it.
        return new Date(8, 1, 2002);
    }
    Easy huh!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    global variables are not a good idea in general, especially if your trying to be object-oriented. You will need to declare tomorrow as a const or pass it back and forth between functions.
    Couldn't think of anything interesting, cool or funny - sorry.

  8. #8
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    i don't know another way. the create dates caller is in a different source code then the function being called (i did that for organization).
    This isn't the real program i'm making, though. Endo knows the story.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  9. #9
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    What hk_mp5kpdw does is a good way to counter the problem. Another way is to declare it in main and pass the date to every function that needs to use it.

    Code:
    void function1( Date& date )
    {
       //stuff which might modify date
    }
    
    
    void function2( Date& date )
    {
       //more code stuff
    }
    
    void main( )
    {
       Date today( 1, 2, 3 );
       function1( today );
       function2( today );
    }
    I hope you can see the risk of using global variables, if you have 100 functions modifying it at different times and in different places its very hard to keep track of its value. If you've ever looked at Java programming there are no globals, everything is contained in classes.
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. solution to the problem
    By vapanchamukhi in forum C Programming
    Replies: 14
    Last Post: 09-08-2008, 10:41 PM
  2. Solution to former XSpace problem
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-25-2006, 12:05 AM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. Replies: 4
    Last Post: 10-17-2002, 10:09 PM