Help with code

I'm stuck with this code....


you will be creating a class that inherits from the class listed below (Person). You are not allowed to modify the class Person. In the new class, there will be one data member of string type to represent the social security number. In the new class, the functions show, get and set will be overridden (redefined). The new show function will display first name, last name, age and social security number on one line separated by spaces. Note: you will not be able to use the Person::show function in redefining the new show function. In the new get and set functions, there will be four parameters: one additional string type for the SSN.



#include <iostream>
#include <string>
using namespace std;

class Person
{
public:
void show()
{
cout << fname << ' ' << lname << ' ' << age << endl;
}
void get(string &fn, string &ln, int &a)
{
fn = fname;
ln = lname;
a = age;
}
void set(string fn, string ln, int a)
{
fname = fn;
lname = ln;
age = a;
}
private:
string fname;
string lname;
int age;
};

class Employee : public Person
{
public:
void show()
{
<------ Need code help here.....
}
void get(string &fn, string &ln, int &a, string &s)
{
<------ Need code help here.....
}
void set(string fn, string ln, int a, string s)
{
<------ Need code help here.....
}
private:
string ssn;
};

int main()
{
Person p1;
p1.set("John", "Smith", 31);
p1.show();
int age;
string firstName, lastName;
p1.get(firstName, lastName, age);
cout << "The person's name is " << firstName << ' ' << lastName
<< "\nand his age is " << age << " years old\n";
Employee e1;
e1.set("Mary", "Jones", 25, "123-45-6789");
e1.show();
string socSecNo;
e1.get(firstName, lastName, age, socSecNo);
cout << "The employee's name is " << firstName << ' ' << lastName
<< "\nand her age is " << age << " years old\n"
<< "and her social security number is " << socSecNo << endl;
return 0;
}
Last edited on
closed account (Lv0f92yv)
Was that assignment spec in a PDF or e-mail plaintext?

Please show us what you have been trying to do, what problems specifically you have been encountering, and ask some specific questions... We want to help, but only if it will actually benefit you.

In the future, please use code tags.
I'm having trouble with the code in void show void get and void set.

It was in plain text.
Last edited on
You know how to set up the std::cout and it is basic c++ that you are dealing with. You show that you know how to Do what you are asking in multiple places in your program.

For Person you do p1.get(...) and then you do the cout in your main line just move the cout to Person::Show(). Then the code would look like p1().get(...) followed by a p1.Show(...).

For Employee::Show(..) it would ::Show(...) to call the person's Show routine. Then add the Additional info that you wanted to show with any more cout's you have.
Could you put it in stupid man terms for me. I think I've been working on it to long. It's all sounding like mush to me right now
this is using most of your own code, I just cut and pasted and modified slightly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

class Person
{
public:
    void show()
    {
        cout << "Person Show::The person's name is " << firstName << ' ' << lastName
            << "\nand his age is " << age << " years old\n";
    }
    void get(string &fn, string &ln, int &a)
    {
        fn = fname;
        ln = lname;
        a = age;
    }
    void set(string fn, string ln, int a)
    {
        fname = fn;
        lname = ln;
        age = a;
    }
// note the change here....This stuff becomes private in the Employee class.
protected:
    string fname;
    string lname;
    int age;
};

class Employee : public Person
{
public:
    void show()
    {
        cout << "Employee Show::The person's name is " << firstName << ' ' << lastName
            << "\nand his age is " << age << " years old with a ssn of " << ssn << endl;
    }
    void get(string &fn, string &ln, int &a, string &s)
    {
        fn = fname;
        ln = lname;
        a = age;
        s = ssn;
    }
    void set(string fn, string ln, int a, string s)
    {
        fname = fn;
        lname = ln;
        age = a;
        ssn = s;
    }
private:
    string ssn;
};

int main()
{
    Person p1;
    p1.set("John", "Smith", 31);
    p1.show();
    int age;
    string firstName, lastName;
    p1.get(firstName, lastName, age);
    cout << "Main Line: The person's name is " << firstName << ' ' << lastName
        << "\nand his age is " << age << " years old\n";
    Employee e1;
    e1.set("Mary", "Jones", 25, "123-45-6789");
    e1.show();
    string socSecNo;
    e1.get(firstName, lastName, age, socSecNo);
    cout << "Main Line: The employee's name is " << firstName << ' ' << lastName
        << "\nand her age is " << age << " years old\n"
        << "and her social security number is " << socSecNo << endl;
    return 0;
}


Read through it for all the changes I made. Like I said this was basic C++ to me. The stuff you need to know at the core heart of C++.
Last edited on
Topic archived. No new replies allowed.