simple class issue

Discussion in 'C++' started by Guest, Jan 28, 2004.

  1. Guest

    Guest Guest

    Hello,

    I do not understand why the func getName() does not receive any data
    (actually, none of the get__receive any data.

    The class I am using as an almost exact example (the exception being that
    there are no strings involved only ints) has setFunctions (mutators)
    activate first, then the getFunctions (inspectors), then the constructor.

    Any thoughts will be appreciated.

    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;

    class Patient
    {
    public:
    Patient();
    Patient(string fName, string lName, string idString, int bYear);

    string getName() const;
    int getAge() const;
    string getID() const;

    void setFirstName (string fname);
    void setLastName (string lname);
    void setID(string idString);
    void setBirthYear(int bYear);

    private:
    string firstName;
    string lastName;
    string ID;
    int birthYear;
    };

    Patient::patient() {
    setFirstName("First");
    setLastName("Last");
    setID("123456789");
    setBirthYear(9999);
    }
    Patient::patient(string fName, string lName, string idString, int bYear) {
    setFirstName(fName);
    setLastName(lName);
    setID(idString);
    setBirthYear(bYear);
    }
    string Patient::getName() const {
    return lastName + ", " + firstName;

    }
    int Patient::getAge() const {
    return birthYear;
    }
    string Patient::getID() const {
    return ID;
    }
    void Patient::setFirstName(string fName) {
    cout <<"In setFirstName, fName = " <<fName <<endl;
    firstName = fName;
    cout <<"In setFirstName, firstName = " <<firstName <<endl;
    }
    void Patient::setLastName(string lName) {
    lastName = lName;
    }
    void Patient::setID(string idString) {
    ID = idString;
    }
    void Patient::setBirthYear(int Age) {
    birthYear = 2003 - Age;
    }


    int main()
    {
    cout<<"Enter patient's first name: ";
    string first;
    cin >> first;
    cout<<"Enter patient's last name: ";
    string last;
    cin >> last;
    cout<<"Enter SSN, no dashes: ";
    string id;
    cin >> id;
    cout <<"Enter year of birth: ";
    int birth;
    cin >>birth;

    Patient P(first, last, id, birth);

    cout<<setw(25) <<setiosflags(ios::left)<<"Patient Name" <<setw(20)
    <<setiosflags(ios::left)<<"Patient ID" <<setw(15)
    <<setiosflags(ios::left)<<"Year of Birth"<<endl;
    cout <<setw(25)<<setiosflags(ios::left)<<P.getName <<setw(20)
    <<setiosflags(ios::left)<<P.getID <<setw(15)
    <<setiosflags(ios::left)<<P.getAge<<endl;

    return 0;
    }
     
    Guest, Jan 28, 2004
    #1
    1. Advertisements

  2. Guest

    Jon Bell Guest

    When you call a member function, just like when you call any other
    function, you need to have the parentheses after the function name, even
    if the function doesn't need any arguments/parameters: P.getName(), etc.
     
    Jon Bell, Jan 28, 2004
    #2
    1. Advertisements

  3. Thoughts:

    1. Post code which actually compiles, or if it doesn't, list the
    compilation errors you get when you try.

    2. To invoke a function, use parentheses after the function name:
    P.getName().

    Jonathan
     
    Jonathan Turkanis, Jan 28, 2004
    #3
  4. Guest

    Guest Guest

    thanks guys...if those parentheses had been a snake...
     
    Guest, Jan 28, 2004
    #4
  5. No answer to your actual question (since that got already answered by
    others) but a suggestion for 'better' initialization of member variables:

    Patient::patient() :
    firstName("First"),
    lastName("Last"),
    ID("123456789"),
    birthYear(9999)
    {
    // <empty>
    }

    Patient::patient(
    string fName,
    string lName,
    string idString,
    int bYear) :
    firstName(fName),
    lastName(lName),
    ID(idString),
    birthYear(bYear)
    {
    // <empty>
    }
     
    Patrik Stellmann, Jan 28, 2004
    #5
  6. Guest

    Howard Guest

    Some other notes in addition to those given earlier: Calling the variable
    "birthYear" implies the "year of birth", which is the year entered, not that
    year subtracted from the current year. Also, since it's already 2004, you
    can see the problem with using a constant to get the age. SetBirthYear
    should just set the birthYear variable to the year given. The GetAge
    function should take as a parameter the current year (2004), and return as
    its result the difference between that and the birthYear. Then, you can
    supply a new GetBirthYear function so that the year of birth that was
    originally entered can also be retrieved if desired.

    -Howard
     
    Howard, Jan 28, 2004
    #6
  7. Guest

    Guest Guest

    Thanks so much Patrik. As you can most likely summize, I am quite new to
    C++ and I am not familiar with using a colon after the first line of a
    constructor, then using commas throughout the list. Nor am I familiar empty
    statements within brackets.

    I will do some research and play around with the code. Thanks again!
     
    Guest, Jan 29, 2004
    #7
  8. You may want to add in "operator <<" to print
    out your class:
    class Patient
    {
    // ...
    friend ostream& operator<<(ostream& out,
    const Patient& p);
    };

    ostream&
    operator<<(ostream& out, const Patient& p)
    {
    out << setw(25) << setiosflags(ios::left)
    << "Patient Name:"
    << firstName << " " << lastName << '\n';
    // ...
    return out;
    }

    In your main() function, you would just:
    cout << P;

    You could also add a function that would print a
    title line for your class.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c++/faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Jan 29, 2004
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.