LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 10-16-2006, 11:38 PM   #1
xemous
Member
 
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, 12:06 AM   #2
nadroj
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, 09:37 AM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
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.
Cheers!
 
Old 10-17-2006, 11:35 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
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..
Code:
class Employee
{
    public:
       virtual void work()=0;
};
class Cook : public Employee
{
    public:
       virtual void work() { cout << "Im cooking" << endl; }
};
class Janitor : public Employee
{
    public:
       virtual void work() { cout << "Im cleaning" << endl; }
};
class EmployeeList
{
    void add(Employee* emp)
    {
        emps.push_back(emp);
    }
    void doWork()
    { 
        for(int i=0; i<emps.size(); ++i)
            emps[i]->work();
    }
    
    vector<Employee*> emps;
};
int main()
{
   EmployeeList el;
   el.add(new Cook());
   el.add(new Janitor());
   el.doWork();
   return 0;
}
 
  


Reply



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 10:40 PM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-21-2006 11:09 PM
pthreads inside a class maldini1010 Programming 2 02-16-2005 03:01 PM
C++ - throwing exceptions in derived class constructors? MadCactus Programming 4 08-09-2004 06:29 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 AM.

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