Thread: help with classes....

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    Unhappy help with classes....

    Hi! I'm currently trying to make a small program that uses classes to simulate an email client. Meaning that it has the ability to record the 'to', 'from', 'subject', and 'message' fields. I've created the Message class, and used a constructor to take the initial 'to' and 'from' fields. I've also made a member function that ask for the subject and message from the user. All of the variables are strings stored in the public section of the class, but after the values are inputted, they don't all display when I try to print them out.

    For example, the 'to' and 'from' are inputted with the creation of a new instance of the class. So it's like
    Code:
     int main() { 
    message newMessage (t,f); // t&f being 'to' and 'from'
    newMessage.append(); // asks for the subject and msg body
    newMessage.print(); // prints out the message
    }
    I'm pretty sure that I'm either using the member functions incorrectly (they're all void except for the constructor), or maybe just in passing the string variables - so any help would be greatly appreciated. Even if it's just pointing me in the right direction. Thanks!!

    Jim

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    just in case....

    Here is some of the code for the class just in case it is of any help:

    Code:
    class message {
    
    public:
    message (string, string);
    ~message();
    void set_time();    //timestamp
    void append();      // msg body txt
    void print();          // print results
    string timestamp;     // timestamp holder
    string to;
    string from;
    string msg;
    string subject;
    string msg_string;       // holds all values together  
    /* for the purpose of the exercise we have to put all of the strings in one long string for storage  */
    
    private:
    
    
    
    };
    
    message::message (string to, string from) {
    system("cls");
         cout << "Create a new email message\n";
         cout << "--------------------------\n";
         cout << "To:";
         cin >> to;
         cout << endl;
         cout << "From:";
         cin >> from;
         cout << endl;
    
    }
    message::~message() {
    
    }
    thanks again

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "but after the values are inputted, they don't all display when I try to print them out."

    Since you don't show the code for how you try to print them out, how would you expect anybody to find any errors in that part of your code?

    In addition, in your constructor, you have two parameters which you immediately overwrite when you input values with cin. Guess what? Than means the data sent to the function is lost forever, so it's the same as not sending any parameters at all.

    Any class function has access to the data members of the class, so your constructor should have no parameters i.e. when you use the data member names in your class functions, the compiler knows you are referring to the data members of the class. The data member variables are "in scope" in the body of class functions.

    Your print() function should look like this:

    void message::print()
    {
    cout<<to<<endl;
    cout<<from<<endl;
    cout<<subject<<endl;
    cout<<msg<<endl;
    }
    Last edited by 7stud; 04-06-2003 at 03:50 PM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    printing

    I tried some various methods, but now I just have a cout statement that prints the variables. Like:

    Code:
    cout << "To:" << to << endl;
    cout << "From:" << from << endl;
    cout << "Subject:" << subject << endl;
    cout << "Message:" << msg << endl;
    What I get is:

    To:
    From:
    Subject: hey
    Message: blahblahblahblah

    the message and the subject fields show up, but the to\from do not. They are taken in by the two different functions .append() and the initial constructor. The .append() member function is the last called and it is the only to display. I just don't understand what happened to the info stored in the first two variables. I have just tried to print the to\from fields without the rest and that works, so something is getting lost in-between.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Your constructor is faulty, as I mentioned. The parameters to your constructor probably hide the data members since they have the same name, so you're reading in the data into local variables which are destroyed when the function terminates.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    ok

    ok, I guess I got confused in the initialization of the variables. If I want my constructor to take in the To & From fields, would I put those strings in the incoming parameters, or is there a way to modify the variables declared in the class?

    Basically I want this class to get values through the two member functions, and keep them in a way that they can be called and manipulated through other functions. Sorry about all of the posts, but the class structure is very confusing to me. Thanks again.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "ok, I guess I got confused in the initialization of the variables."

    I think you should study some basic tutorials on classes and constructors. It's a good idea to study classes before actually trying to use them. If you don't know the basics, you're just going to get into trouble, as is happening here.

    I thought telling you that the class data member are "in scope" in any class function would help, but that didn't seem to work either because you don't seem to understand scope, so I would suggest you also study some basic tutorials on functions, parameters, and scope.

    Here are some examples of constructors:

    message::message()
    {
    to = "empty";
    from = "empty";
    subject = "empty";
    msg = "empty";
    }

    In a normal function, you would get errors saying the variables to, from, subject, and msg are undefined identifiers. However, this is a class function, so any class data member declared in the class can be used in the function body. In main(), you would create an object that calls that constructor like this:

    message email();

    Here is another constructor, this time with parameters:

    message::message(string a, string b)
    {
    to = a;
    from = b;
    }

    You would call that constructor in main() like this:

    message email("Jane Doe", "Me");

    and the class data member to would be intialized with "Jane Doe", and the class data member from would be initialized with "Me".

    Here is another example of a constructor that takes no parameters:

    message:: message()
    {
    cin>>to;
    cin>>from;
    cin>>subject;
    cin>>msg;
    }

    and that would be called in main() like this:

    message email();

    Here is an example of a general class function:

    void message::change_members()
    {
    to = "Mr. Smith";
    from = "Mrs. Smith";
    subject = "Come home now!";
    msg = "I mean it!";
    }
    Last edited by 7stud; 04-06-2003 at 05:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM