Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-17-2006, 12:38 AM   #1
Registered: Jun 2004
Posts: 80

Rep: Reputation: 15
Derived class inside parent class array, possible?

Can you derive a class, say fulltime_employee from an employee class and add the fulltime_employee object to an employee theEmployees = new employee[10]; array and call the new fulltime_employee methods?
Old 10-17-2006, 01:06 AM   #2
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
are you referring to polymorphism?

ie, is the function that you want called in fulltime_employee overriding a base function in employee? or is it a function that isnt in employee?

if the former, then this is called polymorphism. if its the later then id assume youd have to cast the variable to a fulltime_employee object then you can use the methods.
Old 10-17-2006, 10:37 AM   #3
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
I havent used C++ in a while, but you can do that... its just that you really dont want to do that
As nadroj said, if you want to invoke methods from fulltime_employee (which arent part of the employee interface), then you'll have to cast your object. First, if you have an array, you dont know which type of object you have, so you cant safely cast them all, because it could break in runtime. Second, I guess you can ask for the class of an object (I dont know how to do it in C++ but I guess it can be done) but then your code is kinda ugly. The rule of the thumb says that if you have to cast some objects of your array, then there is something that doesnt fit quite right. Perhaps you are think more like a structured program
If you have to cast them all, just define your array as a fulltime_employee and be happy.

The bottom line is that while casting is allowed an sometimes necesary, it is also something you want to avoid as much as you can since it skips types-checking and it will break your program on runtime which is harder to test and debug.

Hope this is useful.
Old 10-17-2006, 12:35 PM   #4
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Originally Posted by xemous
Can you derive a class, say fulltime_employee from an employee class and add the fulltime_employee object to an employee theEmployees = new employee[10]; array and call the new fulltime_employee methods?
to do this without casting the Employee class would need to be abstract. which is the solution that i would probably go with.. here is an example that is completely untested..
class Employee
       virtual void work()=0;
class Cook : public Employee
       virtual void work() { cout << "Im cooking" << endl; }
class Janitor : public Employee
       virtual void work() { cout << "Im cleaning" << endl; }
class EmployeeList
    void add(Employee* emp)
    void doWork()
        for(int i=0; i<emps.size(); ++i)
    vector<Employee*> emps;
int main()
   EmployeeList el;
   el.add(new Cook());
   el.add(new Janitor());
   return 0;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
In C++, how to write destructor of derived class? ArthurHuang Programming 3 06-04-2006 11:40 PM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-22-2006 12:09 AM
pthreads inside a class maldini1010 Programming 2 02-16-2005 04:01 PM
C++ - throwing exceptions in derived class constructors? MadCactus Programming 4 08-09-2004 07:29 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 07:33 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:09 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration