C++ Classes and Vectors help

This is a discussion on C++ Classes and Vectors help within the C++ Programming forums, part of the General Programming Boards category; I recently took my C++ midterm, and I want to make sure I know how to do the things that ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    16

    C++ Classes and Vectors help

    I recently took my C++ midterm, and I want to make sure I know how to do the things that it asked me to do. Can some people help explain to me how this would be done?

    Create a class Tweet with a constructor, accessor, and mutator functions where Tweet accesses strings Subject and Message, and a mutator that appends subject to message.

    I think i know how to do this part

    Code:
    class Tweet
    {
    Public:
    Tweet();
    Tweet(string subject, string message);//constructor
    Tpend(string subject, string message);
    Private:
    string subject;
    string message;
    }
    This probably isn't right, can someone help me with creating a class, specifically this one?

    Then it asked to create the functions for the class. I think i know how to do this one, but then again I may be wrong:

    Code:
    Tweet::Tweet()
    {
    this->subject = subject;
    this->message = message;
    }
    
    Tweet::Tweet(string subject, string message)
    {
    }
    
    Tweet::Tpend(string subject, string message)
    {
    message = subject + ' ' + message;
    }
    The other part of the question was to create an int main using tpend and addTweet (from a class it gave) to add a message with it's subject inside vector v.

    Code:
    int main()
    {
    string mess = "Hello!";
    string subj = "Twitter";
    Tweet t(subj, mess);
    t.Tpend(subj, mess);
    vector<string>v;
    t.addtweet(mess, v);
    }
    addTweet just does a simple v.push_back(message);

    I know this is a lot I'm asking, but just some help would be great! Thanks =)
    Last edited by bigboybz; 02-18-2011 at 12:24 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    You might want to check the part you think you know, before progressing to the subsequent bits.

    An accessor is a member function that retrieves the value of something, and a mutator is a member function that changes the value. Such functions are not generally constructors though.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    your class Tweet should look something like this

    Code:
    class Tweet
    {
    public:
        Tweet();
        Tweet(string subject, string message);//constructor
        string Tpend(string subject, string message);
        string getSubject();
        void setSubject(string subject);
        //do same for message
        void addTweet(string message, vector<string> *v);
    private:
        string subject;
        string message;
    };
    then for your class members etc... (might be a bit crowded but u can space it out)
    Code:
    Tweet::Tweet() : subject(""), message("") {}
    Tweet::Tweet(string subject, string message) : subject(subject), message(message) {}
    string Tweet::Tpend(string subject, string message) { return (subject + " " + message); }
    string Tweet::getSubject() { return this->subject; }
    void Tweet::setSubject(string subject) { this->subject = subject; }
    void Tweet::addTweet(string message, vector<string> *v) { v->push_back(message); }
    quick example of how to use it
    Code:
    int main(int argc, char * argv[])
    {
        Tweet t("Hello", "World =D");
        cout << t.getSubject();
        vector <string> vect;
        t.addTweet("Add this", &vect);
        cout << endl << vect[0];
        return 0;
    }
    Why didn't you place a vector in your class so that it'll store all of your tweets or messages instead of having an external one?
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of vectors containing classes
    By larne in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 06:19 AM
  2. classes and vectors
    By izuael in forum C++ Programming
    Replies: 10
    Last Post: 11-27-2006, 03:19 PM
  3. Vectors and custom classes
    By cunnus88 in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2006, 05:11 AM
  4. vectors and classes
    By jimothygu in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 07:53 PM
  5. How To use vectors for custom classes
    By johnnyd in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2003, 09:04 PM

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