Page 1 of 1

DX little help with c++

Posted: Thu Oct 30, 2008 9:04 am
by captain_jack
i got to do a project in c++ but am a bit lost and was told if i figured out this program then i could use it to heklp me with the project can anyone tell me how to put the names into set_name so they can be copied to get names

#include<iostream.h>
#include<iomanip.h>
#include<string.h>
class person
{
char name[20];
char nationality[20];
int dobday;
char dobmonth[20];
int dobyear;
char sex[6];

public:
void set_names(char names[20]){strcpy(name,names);}
char* get_name(char name1) { return name;}

void set_nationalitys(char nationalitys[20]){strcpy(nationality,nationalitys);}
char* get_nationality() { return nationality;}

void set_dobday(int dob1){dobday = dob1;}
int get_dobday() { return dobday;}

void set_dobmonths(char dobmonths[20]){strcpy(dobmonth,dobmonths);}
char* get_dobmonth() { return dobmonth;}

void set_dobyear(int dob2){dobyear=dob2;}
int get_dobyear() { return dobyear;}

void set_sex(char sexs[6]){strcpy(sex,sexs);}
char* get_sex() { return sex;}
void show();
};



class student:public person
{
char name[20];
char nationality[20];
int dobday;
char dobmonth[20];
int dobyear;
char sex[6];


public :
int id;
int reg_date;

};

void person::show()
{
cout<<get_name()<<endl;
cout<<get_nationality()<<endl;
cout<<get_dobday()<<endl;
cout<<get_dobmonth()<<endl;
cout<<get_dobyear()<<endl;
cout<<get_sex()<<endl;





}

int main()
{
person p1;
char name1[20];




p1.get_name(name1[20]);
p1.get_nationality();
p1.get_dobday();
p1.get_dobmonth();
p1.get_dobyear();
p1.get_sex();





return 0;
}

Re: DX little help with c++

Posted: Mon Nov 03, 2008 3:31 am
by Harlequin
captain_jack wrote:i got to do a project in c++ but am a bit lost and was told if i figured out this program then i could use it to heklp me with the project can anyone tell me how to put the names into set_name so they can be copied to get names

#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#define MAX_NAME_LEN 20

class person
{
char name[MAX_NAME_LEN];

public:
void set_name(char *newname){strcpy(name,newname);}
char* get_name(char name1) { return name;}

};


The bit I've highlighted in red is completely unnecessary; get rid of it. Then, when you want to add a name, create a character array containing the name, ie.

Code: Select all

char *my_name = "Luke";

then create your person object

Code: Select all

person *me = new person;

and then to set the name:

Code: Select all

me->set_name(my_name);

and to retrieve it:

Code: Select all

char *the_name = me->get_name();

The other attributes can be done similarly.