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
    Join Date
    Jul 2007
    Posts
    8

    Re: Calling a class in another class

    What do you mean by proper? “Proper” for what?

    The assignment in the constructor should be the other way around.

    Plus, if you don’t want the Stack instances to by synchronized you should try cloning the parameter before assignment. Otherwise any change in the ev parameter after the constructor is called will change the event field of the class.

    The toString method appends at the end of the string representation the event field. Being a Stack instance, its toString() doesn’t always looks nice. You should consider parsing the elements of the stack for a more appriopriate output.

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

    Re: Calling a class in another class

    This is a duplicate post - it's already been answered in this thread

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