Thread: Question on pointers in classes

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    14

    Question on pointers in classes

    In the following program:
    #include <iostream>
    #include <string>
    using namespace std;
    class Employee
    {
    private:
    char name[20];
    public:
    Employee() {strcpy(name, "(no name)");}
    Employee(char *n) {strcpy(name, n); }
    void show() { cout << "Name" << name << endl; }
    ~Employee() { cout << "Employee destructor \n"; }
    };

    class Exempt : public Employee

    {
    private:
    float salary;
    public:
    Exempt() { salary = 10000.0; }
    Exempt(char *n, float s) : Employee(n)
    {
    salary = s;
    }
    void show()
    {
    Employee::show();
    cout << "Salary " << salary << endl;
    }
    ~Exempt()
    { cout << "Exempt destructor\n"; }
    };

    int main()
    {
    Employee e1("jackson"), *p1, *p2;
    Exempt staff1;
    Exempt x1("Alexander", 30000.0), *px = &x1;
    Employee e2 = e1;
    p1 = &e2;
    p2 = &x1;
    e1.show();
    staff1.show();
    (*p1).show();
    p2 -> show(); // statement in question
    px -> show();
    }

    in main, with the statement p2 ->show(); how is the output Name Alexander?

    when you are sending addresses can you by pass the arguements? i really am confused on this, i dont understand how Name Alexander would be printed.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Exempt x1("Alexander", 30000.0)
    -declares a exempt with a name of Alexander and a salary of 30k

    p2 = &x1;
    -says that p2 contains whatever is in the address of x1
    -therefore, since alexander is the name of x1, alexander is the name displayed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 01:46 AM
  3. Pointers to pointers question
    By mikahell in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2006, 12:54 PM
  4. Question about pointers #3
    By maxhavoc in forum C++ Programming
    Replies: 3
    Last Post: 06-19-2004, 10:45 AM
  5. Question on use of classes
    By Flyer in forum C++ Programming
    Replies: 8
    Last Post: 06-25-2003, 08:23 AM