CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2006
    Posts
    37

    Calling a class in another class

    I have 2 classes, Event and Dear. I have created Event class with two variables which are Date and Type of Event. This class is known as the ADT Event class.

    Another class Dear, has variables name, hphone, email, type of relation and event(which is supposed to be a stack).

    How do I write the proper constructors, set and get and toString methods for the event part(highlighted) in the 'Dear' class?

    Is the code below correct?

    Code:
    public Dear(String n, int hp, String e, String t, Stack ev)
        {
            setData(n, hp, e, t, ev);
        }
        
        private void setData(String n, int hp, String e, String t, Stack ev)
        {
            n = name;
            hp = hphone;
            e = email;
            t = tor;
            ev = event;
        }
    Code:
      public Stack getEvent(){
            return event;
        }
    Code:
     public String toString() 
        {
            return name + "\t" + hphone + "\t" + email + "\t" + tor "\t" + event;
        }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Calling a class in another class

    The assignments are back-to-front in the setData method. Also, that code should really go in the constructor - there's little point having a separate private method to do that.

    The getEvent method returns a Stack. Is a stack an event? I would have thought the stack is a collection of events.

    If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Calling a class in another class

    Code:
    		n = name;
    		hp = hphone;
    		e = email;
    		t = tor;
    		ev = event;
    All of these assignments are the wrong way around. You should be setting 'name' to equal 'n', 'hphone' to equal 'hp' etc.

    Edit:
    Hi Dave, well you've beaten me to the punch 2 posts in a row - I give in.
    Last edited by keang; July 2nd, 2007 at 08:58 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured