Classes!!!!

Hi all, I have an assignment to do, all 5 questions is about classes... But first I only want help with the first question, if someone can please help me to understand this question, I will try to do the other questions myself.

So the question is:
Consider the following structure used to keep record of a meeting:
struct Meeting
{
string speaker;
string topic;
string venue;
string date;
}

Turn the meeting struct into a class. The class should have member variables for all the values in the corresponding struct. Make all member variables private. Include public member functions for each of the following:
*a default constructor that sets the string data members to blank strings
*member functions to set each of the member variables to a value given as an argument to the function (i.e. mutators)
*member functions to retrieve the data from each of the member variables (i.e. accessors)

Test the class in a program that instantiates an object of class Meeting (i.e. 'declare' an object of 'type' Meeting). The program should then input values to the object (obtained from the keyboard), and use the mutators to assign values to the member variables. Use the accessors to obtain the values of the member variables of the object and display those values on the screen. Test your program with the following input:

Input for member variables of Meeting object:
Speaker at Meeting: Johnny Apllemac
Topic at meeting: Learning to use classes and objects
Venue of Meeting: TvW 2-100, Muckleneuk Campus
Date of meeting: 1 February 2013


All I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

class Meeting
{
private:
     string speaker;
     string topic;
     string venue;
     string date;
public:
     //here I am not very sure... I know there should be void set function and get functions, but not sure to code them and for the .int main all I know s that there must be a variable Meeting with an object...  


Please help me with this one!
Wow, i have no idea whats your problem is.

Please format your question to some user friendly standard and we will help you out.

Thank you I will have a look at the websites...
@tath, the biggest problem is I don't know where to begin and how to do the main function...
You deal with classes like with structures.

so in main:

1
2
3
4
5
6
int main()
{
Meeting meeting1;

return 0;
}
Last edited on
I understand that now, but I'm still confused on how to use the get and set functions... I really am stuck, I tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

class Meeting
{
private:
      string speaker;
      string topic;
      string venue;
      string date;
public:
      void blanks();
      void set(string speakerX, string topicX, string venueX, string dateX);
      string get_speaker();
      string get_topic();
      string get_venue();
      string get_date();
};

int main()
{
     Meeting meeting1;

     return 0;
}


How do I do the coding to get the info from the user? I want to use cout statements that asks for all the info but I don't know if that is the right way of doing.
read the link I posted.
closed account (3qX21hU5)
Here is a simple example to help show you how. This example is just a very simple one showing how getters and setters can be used. There are much better ways to do this (Like not using getters and setters) but them can be more advanced and I will hold off on explaining those so I don't confuse you. Anyways here is the simple example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>

using namespace std;

class Meeting
{
    public:
        // Member function to get the name variable
        string getName() {return name;}
        // Member function to set the name variable
        void setName(string input) {name = input;}

    private:
        string name;

};

int main()
{
    // Declares your class object
    Meeting m1;

    // String to hold name input
    string n;

    cout << "Please enter you name: ";
    cin >> n;

    // This sets the name variable in your class to whatever
    // The user inputted. This uses the setter function
    m1.setName(n);

    // This uses the getter to retreive the info in the name variable
    cout << m1.getName() << endl;

    return 0;
}


If you don't understand anything here just let me know and I will do my best to help explain anything you are not familiar with to you. Hope this helps.
Thanks all! I will try it and read on the sites! Will let you know tomorrow! :-)
Sorry I didn't let you know, but my program works and I'm very happy! Thank you for all of you! Every website you gave me and Zereo for your example code!

NOW for the other four questions! Let me get to work...!
I do have another question though, in the question they say I must have a default constructor that sets the string data members to blank strings, how do I do that??
closed account (3qX21hU5)
Well technically you would just need a blank default constructor to initiliaze to a blank string which looks like this.


1
2
3
4
5
6
7
8
9
class MyClass
{
    public:
        // This is the default constructor
        MyClass() {}

    private:
        std::string myString;
}


That is a blanket default constructor. The reason why we don't need to use it to initialize the string is because the string will use its own default constructor implicitly to initialize the strings to blank strings.


But if you do want to use a constructor to explicitly initialize it you can do something like this.


1
2
3
4
5
6
7
8
9
10
class MyClass
{
    public:
        // This is the default constructor and will explicitly initialize the string to blank
        MyClass() : myString("") {}
            

    private:
        std::string myString;
}


That code will initialize the string to "" which is a blank string. For every other string or other type of variable you just put a comma and then initialize it the same way. For example.

MyClass() : myString(""), myDouble(0.0), myInt(0) {}

Would initialize string to "", the double to 0.0 and the int to 0.

Hope this helps didn't have much time to explain it better but let me know if you don't understand anything.
Thank you so so much! I understand 100%! Your explanation is much better than my textbook!
Topic archived. No new replies allowed.