 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Jan 28, 2004 7:31 am Post subject: simple class issue |
|
|
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 = " <
firstName = fName;
cout <<"In setFirstName, firstName = " <
}
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<
<
<
cout <
<
<
return 0;
}
|
|
| Back to top |
|
 |
Jon Bell Guest
|
Posted: Wed Jan 28, 2004 7:52 am Post subject: Re: simple class issue |
|
|
In article <JzJRb.5343$BA2.935 (AT) newssvr26 (DOT) news.prodigy.com>,
<mickifox (AT) sbcglobal (DOT) net> wrote:
[snip]
| Quote: | | cout <
setiosflags(ios::left)<
setiosflags(ios::left)<
|
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
Dept. of Physics and Computer Science Clinton, South Carolina USA
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Wed Jan 28, 2004 7:56 am Post subject: Re: simple class issue |
|
|
<mickifox (AT) sbcglobal (DOT) net> wrote
| Quote: | 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.
|
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
|
|
| Back to top |
|
 |
Guest
|
Posted: Wed Jan 28, 2004 8:09 am Post subject: Re: simple class issue |
|
|
thanks guys...if those parentheses had been a snake...
<mickifox (AT) sbcglobal (DOT) net> wrote
| Quote: | 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
#include
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 = " <
firstName = fName;
cout <<"In setFirstName, firstName = " <
}
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<
setiosflags(ios::left)<<"Patient ID" <
setiosflags(ios::left)<<"Year of Birth"<
cout <
setiosflags(ios::left)<
setiosflags(ios::left)<
return 0;
}
|
|
|
| Back to top |
|
 |
Patrik Stellmann Guest
|
Posted: Wed Jan 28, 2004 8:16 am Post subject: Re: simple class issue |
|
|
| Quote: |
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);
}
|
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>
}
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Wed Jan 28, 2004 3:25 pm Post subject: Re: simple class issue |
|
|
<mickifox (AT) sbcglobal (DOT) net> wrote
| Quote: | string firstName;
string lastName;
string ID;
int birthYear;
};
int Patient::getAge() const {
return birthYear;
}
void Patient::setBirthYear(int Age) {
birthYear = 2003 - Age;
|
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
|
|
| Back to top |
|
 |
Guest
|
Posted: Thu Jan 29, 2004 8:09 am Post subject: Re: simple class issue |
|
|
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!
"Patrik Stellmann" <stellmann (AT) tu-harburg (DOT) de> wrote
| Quote: |
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);
}
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)
{
//
}
Patient::Patient(
string fName,
string lName,
string idString,
int bYear) :
firstName(fName),
lastName(lName),
ID(idString),
birthYear(bYear)
{
//
}
|
|
|
| Back to top |
|
 |
Thomas Matthews Guest
|
Posted: Thu Jan 29, 2004 5:46 pm Post subject: Re: simple class issue |
|
|
[email]mickifox (AT) sbcglobal (DOT) net[/email] wrote:
| Quote: | 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
#include
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;
};
[snip] |
| Quote: | 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<
setiosflags(ios::left)<<"Patient ID" <
setiosflags(ios::left)<<"Year of Birth"<
cout <
setiosflags(ios::left)<
setiosflags(ios::left)<
return 0;
}
|
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|