C++ Exam Practice

Hi everyone, I'm a total n00b at C++, I'm still learning and I've got loads of questions to ask, here's one of them ^^.

I'm going over a past paper to practice for my upcoming C++ Exam so I'll post the question in concern and I'll post what I've come up as an answer for that question. Since there are no answer's available for the past paper exams I tend to run into brick walls when I answer questions, so here goes.

Question:

c) In this question we will develop a class for a telephone book.

i) Define a phone_number class containing a name of a person/company (as a
string) and a phone number (again as a string). Include a constructor and
appropriate selector methods.

My Answer:

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
#include <iostream>
#include <string>
using namespace std;

class Phone_number 
{
      public:
             string name; //name of person/company
             string phoneNumber; //phone number as string
             
             Phone_number(string name_, string phoneNumber_);
             //initializes the name and phone to argurments
             
             /*This is a selector method to get the name.*/
             string Phone_number::getName()
             {
                  return name;
             }
             
             /*This is a selector method to get the phoneNumber.*/
             string Phone_number::getNumber()
             {
                    return phoneNumber;
             }
};

int main()
{
    return 0;
}


My answer compiles without any errors on Dev C++, I just want to know if I've hit all the points on that question, also am I right in thinking a selector method is an accessor method, because thats how I've written my code :S.

P.S. Can anyone let me know if my code structure is ok or not etc. Thank you. :)
As far as he/she wants only the implementation of the class,main method is useless.Although if you want to make one on every case,write a code example of using the phone_number class.
And,if I were you I would have made a typical implementation of Phone_number(string name_, string phoneNumber_)

method.
feel free to ask.
Oh I just added the main method when compiling it, it's a habit, I always write that main method LOL :).

I didn't quite understand what you meant by typical implementation of the constructor Phone_number(string name_, string phoneNumber_), could you elaborate on that please. :)

Oh I spotted one thing, my current constructor is like Phone_number(string name_, string phoneNumber_);

Would it make more sense to have it like this Phone_number(string name_, string phoneNumber_ : name(name_), phoneNumber(phoneNumber_);

Thanks
Phone_number(string name_, string phoneNumber_) : name (name_), phoneNumber(phoneNumber_){};

I made a mistake on my previous post.
Topic archived. No new replies allowed.