-5

So my project requires me to create a header file that defines the following class and its members/functions:

#include <string>
using namespace std;

class Ticket
{   
public:
    string mName;
    string mFromCity;
    string mToCity;
    double mCost;
    Ticket(string name, string fromCity, string toCity, double cost)
    {
        string getName();
        string getFromCity();
        string getToCity();
        double getCost();
        void setName(string name);
        void setFromCity(string fromCity);
        void setToCity(string toCity);
        void setCost(double cost);
    }
};

The instructions said to copy the member and function names letter for letter but why is it that a bunch of the names for functions and members aren't consistent such as "mName", "name", and just "Name" later on? Furthermore the instructions had the declarations down in the form of "mName : string" (which generates errors) instead of just "string mName" like I did...

Noa Kim
  • 1
  • 1
  • 1
  • 2
    We can't tell you what the author of that code was thinking. – Rein Henrichs Aug 08 '17 at 08:09
  • You could ask the person who gave you the instructions. – DimChtz Aug 08 '17 at 08:10
  • 2
    Maybe the author of the instructions gave you some sort of pseudocode, which you have to transform to proper c++? Because this code you wrote would never compile.. – Ventu Aug 08 '17 at 08:11
  • 3
    All the lines inside the constructor should be outside the constructor. – molbdnilo Aug 08 '17 at 08:11
  • 2
    I think you need to [read a good beginners book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Aug 08 '17 at 08:11
  • When you add an attribute to a class, you must call it the same way if you refer to it. For example, if a person was a class, you'd have to call him by his mName (an attribute) and you could change its value with with the function : setName("newName"). His name will change to "newName", but this attribute will always be called mName. Is that clearer ? – Myriam Sarah Aug 08 '17 at 08:15
  • "m" (or "m_") is a common prefix for member variables. Both parameters and functions use the "camelCase" naming convention. – molbdnilo Aug 08 '17 at 08:47

1 Answers1

2

Here it is:

#include <string>
using namespace std;

class Ticket
{   
    string mName;
    string mFromCity;
    string mToCity;
    double mCost;
public:
    Ticket(string name, string fromCity, string toCity, double cost)
    {
         setName(name);
         setFromCity(fromCity);
         setToCity(toCity);
         setCost(cost);
    }
    string getName() { return mName; }
    string getFromCity() { return mFromCity; }
    string getToCity() { return mToCity; }
    double getCost() { return mCost; }
    void setName(string name) { mName = name; }
    void setFromCity(string fromCity) { mFromCity = fromCity; }
    void setToCity(string toCity) { mToCity = toCity; }
    void setCost(double cost) { mCost = cost; }
};

But read a tutorial please or it would be totally useless. Here are some very easy introductions you can start from:

Or if you want to read a few books, ranging from beginner to advanced go to

Nfagie Yansaneh
  • 215
  • 1
  • 10
8znr
  • 336
  • 3
  • 13