473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 = " <<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;
}
Jul 22 '05 #1
7 2196
<mi******@sbcglobal.net> wrote in message
news:Jz****************@newssvr26.news.prodigy.com ...
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
Jul 22 '05 #2
In article <Jz****************@newssvr26.news.prodigy.com>,
<mi******@sbcglobal.net> wrote:
[snip]
cout <<setw(25)<<setiosflags(ios::left)<<P.getName <<setw(20)
<<setiosflags(ios::left)<<P.getID <<setw(15)
<<setiosflags(ios::left)<<P.getAge<<endl;


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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #3
thanks guys...if those parentheses had been a snake...

<mi******@sbcglobal.net> wrote in message
news:Jz****************@newssvr26.news.prodigy.com ...
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;
}

Jul 22 '05 #4
>
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>
}

Jul 22 '05 #5

<mi******@sbcglobal.net> wrote in message news:JzJRb.5343> private:
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


Jul 22 '05 #6
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" <st*******@tu-harburg.de> wrote in message
news:bv************@uni-berlin.de...

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>
}

Jul 22 '05 #7
mi******@sbcglobal.net wrote:
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;
}; [snip]
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;
}


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

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
2
by: Jacek Dziedzic | last post by:
Hi! Say a have a class called black_box which, among other things, contains an array of cells like this: class black_box { private: // ... public: black_box_details_struct details;
13
by: LRW | last post by:
Having a problem getting a onSubmit function to work, to where it popsup a confirmation depending on which radiobutton is selected. Here's what I have: function checkdel() { if...
4
by: Gauthier | last post by:
Hi, I've a simple issue with the use of extension objects. I'm trying to call a text formating method from an object that I add to my arguments collection, this method take an input string and...
19
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and...
3
by: | last post by:
I am trying to compile a simple c# class in Web Matrix called howdy.cs: // Program start class public class HowdyPartner { // Main begins program execution public static void Main() { //...
1
by: Maik Thiele | last post by:
Hey, I have following simple ftpService class with a method download. When I run the class not as a web service it works fine. But when I run the class as a web service the FtpGetFile() method...
8
by: obrianpatrick | last post by:
Hi, I am relatively new to object oriented programming and design. I am developing an application in VS 2005. I am having the following design problem: I have two interfaces X and Y. Y is...
2
by: Dragan | last post by:
Hi, We're working in VS 2005, Team edition, if it makes any difference at all (should be up-to-date and all that, but could not guarantee it is 100%). We've implemented a simple generic wrapper...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.