Thread: Need some help on class inheritance

  1. #1
    HelpMe
    Guest

    Question Need some help on class inheritance

    I'm working on a project for my computer science class and I've come upon a problem that I cannot figure out how to implement. The class PERSON was provided by the instructor and the assignment was to write a new class EMPLOYEE inheriting the functions of PERSON. The one thing that I am having a problem with is not inheriting the class PERSON but rather writing a constructor that allows the assignment of an object from class PERSON to an EMPLOYEE. I.E.

    This is the constructor that I wrote to set all data for the PERSON and EMPLOYEE class at once:

    employee::employee(char *n, char *s, char *d, char *b, int h,
    int w, char *companyName,
    char *companyDivision)
    : person(n,s,d,b,h,w)

    Now, what I'm having trouble with is:

    employee::employee(const person &p, char *companyName,
    char *companyDivision)

    Someone please help me as to how I could assign a person to the employee. Thanx in advance.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You could do something like -

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    class Person
    {
    	string name;
    public:
    	Person (string n):name(n){}
    	Person ():name("Joe"){}
    	friend ostream& operator << (ostream& os, const Person& p){
    		return os << p.name;
    	}
    };
    
    class Employee : public Person
    {
    	double salary;
    public:
    	Employee(Person p, double s):Person(p),salary(s){		
    		
    	}
    	friend ostream& operator << (ostream& os,const Employee& e)	{
    		return os << static_cast<Person>(e) << " : " << e.salary;
    	}
    };
    
    
    int main()
    {
    	Person p("Me");
        Employee e(p,0.57);
    	cout << e;
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Inheritance, istream, class datamember
    By guda in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2004, 12:25 PM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM