Link to home
Start Free TrialLog in
Avatar of jewee
jewee

asked on

Abstract class

Design question.  Lets say I have a base class Display and 2 abstract classes, Display1 and Display2.  The functions are similar such as loadConfigFile, ProcessFile and DisplayFile.  Of course, the functionality is different in the ProcessFile and DisplayFile.  Would using abstract classes be the best approach in this situation?  

If so, could you provide me with an example?
Avatar of dennis_george
dennis_george
Flag of India image

I don't get it !!!! can you explain me the relation between Display, Display1 and Display2....

See designing issue is based on what you want to acheive...... Normaly If you want create an interface you define abstract base calss and derive your other classes from this base class to provide the specific implementations...

Dennis
ASKER CERTIFIED SOLUTION
Avatar of dennis_george
dennis_george
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


Now the best is to implement a "Factory class" to create the instances of your child classes
and use them.
Note :: just study about Factory classes... Its a Design Pattern.. I hope you know if not then study about it.

or more simply you create directly your instances of sub class
Display *ob1 = new Display1() ; // assigned to base class pointer...
Display *ob2 = new Display2() ;

ob1->LoadConfigFile("system.conf")  ;
ob1->ProcessFile() ;
ob1->DisplayFile() ;

By this way you can add as many implementation of the class Display without changing the interfaces.

Dennis
Avatar of jewee
jewee

ASKER

Each display class processes and displays different kinds of data.  
If I keep it abstract, then the base class will only be used as a template without defined functionality other than just listing the functions?
Avatar of jewee

ASKER

ANother thing too, the same functionality exists for LoadConfigFile, so I probably won't want to set up an abstract class???
Can you write in detail what do you want to design ??

In the above example you can give different functionalities in Display1 and Display2 as you wish.... to the LaodConfigFile or all the other processes.

Still I don't know what do you want to acheive ?????????

Dennis
Hi jewee,
> If I keep it abstract, then the base class will only be used as a
> template without defined functionality other than just listing the functions?

Not quite. You can have zillions of methods and all the data your heart ever desired defined in an abstract base class - it's just that at least one virtual function is not defined, so you cannot instatiate it.

Cheers,
Stefan
jewee,
> ANother thing too, the same functionality exists for LoadConfigFile,
> so I probably won't want to set up an abstract class???

Just think of a class hierarchy as providing a set of capabilities. Some of them are used by a broader range of classes, so they are further up in the anchestor chain. Some of them are quite specialized, so you have them defined further down.

You can have the LoadConfigFile defined in your base class if the behaviour for all sibling classes is identical.

Stefan
Avatar of jewee

ASKER

Sorry, haven't programmed in a while.  If I was to use the LoadConfigFile from the base class,  I could just call LoadConfigFile from the constructor of my Display1 class, since it inherits from the base class.

I have one base class Display, and each subclass will process a file of 2 different types.  One file type, with a .fds extension will have signal data that needs to be processed and displayed while the other file with a .tsr extension will have different signal data which has to be processed differently.  Each class has the following functions:
LoadConfigData
PollDirectory
ProcessFile
DisplayData.

The LoadConfigData and PollDirectory data are the same except for the path.  The display data and processfile functions are different.  

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial