Overclock.net banner

1 - 8 of 8 Posts

·
Registered
Joined
·
3 Posts
Discussion Starter #1
Ok just so you all know, yes this is homework. I am just needing a little help with the last part. The assignment is as follows: Please finish the implementation of class name and derived classes person and employee in the following code.

Quote:


#include <iostream>

#include <fstream>

using namespace std;

class name {

protected:

char *first, *last;

public:

name() {};

name(char *f, char *l){// SET F TO FORST NAME AND L TO LAST}

virtual void info(){

// FILL IN

}

};

class person : public name {

char gender;

int ssn;

public:

person() {};

person(char *f, char *l, char g, int s): gender(g), ssn(s), name::name(f, l) {}

virtual void info() {

name::info();

//FINISH

}

};

class employee : public person {

protected:

int payroll;

char *position;

public:

employee() {};

employee(char *f, char *l, char g, int s, int p, char *pos) {

//FINISH

}

void info() { // formatted display of the information

//FINISH

}

};

int main()

{

char fn[20], ln[20], pos[20];

char gen;

int s, pay;

for (int i=0; i<3; i++) {

cout << "Input the first name: " << endl;

cin >> fn;

cout << "Input the last name: " << endl;

cin >> ln;

cout << "Input the gender: " << endl;

cin >> gen;

cout << "Input the social security number: " << endl;

cin >> s;

cout << "Input the payroll: " << endl;

cin >> pay;

cout << "Input the position: " << endl;

cin >> pos;

employee worker(fn, ln, gen, s, pay, pos);

worker.info();

}

char cc; cin >> cc;

return 0;

}


my code I have works great for the input, but then just disappears when I compile. First few times I tried it errored out and closed. Now it just closes. I belive my problem is at the end, I see he has us using employee worker(fn, ln, gen, s, pay, pos); and worker.info(); in the main

my code is below

Quote:


#include <iostream>

#include <fstream>

using namespace std;

class name {

protected:

char *first, *last;

public:

name() {};

name(char *f, char *l):first(f), last(l){}

virtual void info(){

name::info();

}

};

class person : public name {

char gender;

int ssn;

public:

person() {};

person(char *f, char *l, char g, int s): gender(g), ssn(s), name::name(f, l) {}

virtual void info() {

name::info();

person::info();

}

};

class employee : public person {

protected:

int payroll;

char *position;

public:

employee() {};

employee(char *f, char *l, char g, int s, int p, char *pos): payroll(p),position(pos),person:
erson(f,l,g,s){}

void info() {

person::info();

employee::info();

}

};

int main() {

char fn[20], ln[20], pos[20];

char gen;

int s, pay;

for (int i=0; i<3; i++) {

cout << "Input the first name: " << endl;

cin >> fn;

cout << "Input the last name: " << endl;

cin >> ln;

cout << "Input the gender: " << endl;

cin >> gen;

cout << "Input the social security number: " << endl;

cin >> s;

cout << "Input the payroll: " << endl;

cin >> pay;

cout << "Input the position: " << endl;

cin >> pos;

employee worker(fn, ln, gen, s, pay, pos);

worker.info();

}

char cc; cin >> cc;

return 0;

}


Can anyone offer advice?
 

·
Registered
Joined
·
45 Posts
To set the code use tags [] and put the word code in them: [ code ] (don't forget the [ / code ] at the end!). For your post just edit the tags from saying "quote" to saying "code".

Could you be a bit more specific about the issues you are having? You say it works fine for the input but then doesn't compile? How can you test the input without a successful compile? What compiler errors are you getting? What do you mean by "my code disappears"? Does it get erased and then you have to re-write it all?

Don't mean to grille you with questions, its just that the way things are now it'll be hard to help you without knowing what the actual problem is!
 

·
Registered
Joined
·
821 Posts
Your default implementations of the various virtual methods are calling themselves!? For example:

Code:

Code:
// Heh?
virtual void name::info() {

        name::info();

}
Not sure of what you intended by that.

Good luck!
 

·
Premium Member
Joined
·
13,477 Posts
Quote:

Originally Posted by Spotswood View Post
Your default implementations of the various virtual methods are calling themselves!? For example:

Code:

Code:
// Heh?
virtual void info() {

        name::info();

}
Not sure of what you intended by that.

Good luck!
It's calling the base class.

I'm still confused as to what the OP's question is. The code appears to be missing only the cout calls to print the member variables.
 

·
Registered
Joined
·
821 Posts
Quote:

Originally Posted by error10 View Post
It's calling the base class.

I'm still confused as to what the OP's question is. The code appears to be missing only the cout calls to print the member variables.
That's his intention. But calling name::info() from name::info() is silly.

The program "closes" because it overflows the stack.
 

·
Banned
Joined
·
12,758 Posts
Quote:


Originally Posted by error10
View Post

It's calling the base class.

I'm still confused as to what the OP's question is. The code appears to be missing only the cout calls to print the member variables.

Looks like he's already in the base class. So it seems to be unintended recursion.

EDIT: Looks like someone mentioned the stack overflow already.
 
1 - 8 of 8 Posts
Top