Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,574 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,895 people online right now. Registration is fast and FREE... Join Now!




Creating a person class to represent a person

 
Reply to this topicStart new topic

Creating a person class to represent a person

graves2008
6 May, 2008 - 06:03 AM
Post #1

New D.I.C Head
*

Joined: 10 Apr, 2008
Posts: 4

My prof has asked me to "Create a person class to represent a person. To simplify things, have the class have 2 variable members for the person's first and last name. Include 2 constructors. One should be a default constructor and the other should be one with parameters. Include respective functions for:


setting the name,
getting the name, and
printing the name on the screen.

"

I have created a class, however, my class prints the wrong name. What "exactly" am I doing wrong?

CODE

#include <iostream>

using namespace std;
char a,b;
class person{

protected:
char first[20];
char last[20];


public:

void get_person( char *f , char *l );
void print_person();

};

void person::get_person( char *f , char *l )
{
*first = *f;
*last = *l;
}

void person::print_person( )
{
cout <<endl<<"first name: "<<*first;
cout <<endl<<"last name: "<<*last;
}

int main ()
{
char a[20],b[20];

person one;
cout <<endl<<"first name = ";
cin >> a;
cout <<endl<<"last name = ";
cin >> b;


one.get_person(a,b);
one.print_person();

system ("pause");
return 0;
}

User is offlineProfile CardPM
+Quote Post

KYA
RE: Creating A Person Class To Represent A Person
6 May, 2008 - 06:39 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
It's only reading in the first letter of each name.
User is offlineProfile CardPM
+Quote Post

jeronimo0d0a
RE: Creating A Person Class To Represent A Person
6 May, 2008 - 09:09 AM
Post #3

D.I.C Head
**

Joined: 3 Mar, 2008
Posts: 141


My Contributions
Yes it is just reading the first letter of each name becasue you deferenced the pointer. You have to do a strcpy(dest, src) ; instead of a=f ; Or... First, learn to use your debugger to step through the code line by line and see what it is doing. It is the most valuable tool you have for this kind of thing. Then migrate to true C++. I would use the C++ string class instead of the old C style string. See the patched code.
Hope this helps.
Jeronimo
CODE


#include <iostream>
#include <string>

using namespace std;
string a,b;
class person{

protected:
string first;
string last;


public:

void get_person( string f , string l );
void print_person();

};

void person::get_person( string f , string l )
{
first = f;
last = l;
}

void person::print_person( )
{
cout <<endl<<"\tfirst name: "<<first;
cout <<endl<<"\tlast name: "<<last;
}

int main ()
{
string a,b;

person one;
cout <<endl<<"\tfirst name = ";
cin >> a;
cout <<endl<<"\tlast name = ";
cin >> b;


one.get_person(a,b);
one.print_person();
cout << "\n\n\t";
system ("pause");
return 0;
}

User is offlineProfile CardPM
+Quote Post

graves2008
RE: Creating A Person Class To Represent A Person
6 May, 2008 - 10:39 PM
Post #4

New D.I.C Head
*

Joined: 10 Apr, 2008
Posts: 4

Thank you everybody! And YES, I will start to use real C++ language from now on...

All to easy...
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:05AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month