• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 833
  • Last Modified:

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?
0
jewee
Asked:
jewee
  • 4
  • 3
  • 3
  • +1
3 Solutions
 
dennis_georgeCommented:
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
0
 
dennis_georgeCommented:
e.g.

this is your heirarchy structure
                         Display
                             |
                ------------------------
               |                             |
         Display1                    Display2

Here Display class is the base class which is used to provide you with interfaces.....

i.e.
class Display {

public :
        void LoadConfigFile(string strFileName) = 0 ;
        void ProcessFile() = 0 ;
        void DisplayFile() = 0 ;
} ;

So above class is a abstract class (more precisely pure abstract class) which only specifies the interafaces..

Now Derive from this class and implement your own functionalities....

class Display1 : public Display { // not an abtrsact class

public :
       void LoadConfigFile(string strFileName)
       {
        ...
       }

        void ProcessFile()
        {
         .....
        }

        void DisplayFile()
        {
         .....
        }
} ;

similarly you can implement Display2 also

Dennis
0
 
dennis_georgeCommented:


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
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
jeweeAuthor Commented:
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?
0
 
jeweeAuthor Commented:
ANother thing too, the same functionality exists for LoadConfigFile, so I probably won't want to set up an abstract class???
0
 
dennis_georgeCommented:
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
0
 
stefan73Commented:
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
0
 
stefan73Commented:
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
0
 
jeweeAuthor Commented:
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.  

0
 
stefan73Commented:
jewee,
Try this:

Have the file path as part of the constructor for both base class + the 2 display classes. Then you can move the config + poll handling to the base class.

For the polling, also have the extension to look for in the base class constructor. Each derived class constructor then can set its own individual extension, like:

class Display{
    Display(string& path, string& extension);
    [...]
};

class Display1 : public Display{
    Display1(string& path)
        : Display(path,".fds"){
        [...]
    }
};


class Display2 : public Display{
    Display2(string& path)
        : Display(path,".tsr"){
        [...]
    }
};

An alternative might be that you only have one Display class which knows what to do by the extension which you pass. This could be a bit messy, though.


Stefan
0
 
itsmeandnobodyelseCommented:
>> Lets say I have a base class Display and 2 abstract classes, Display1 and Display2

That expression is wrong. An abstract class is a base class that has at least one pure virtual function. so, not the derived classes are abstract but the base class.

In your case you may decide to turn class Display to an abstract class by defining Display::ProcessFile() and/or Display::DisplayFile() pure virtual, i. e. not providing a base class functionality for these functions _AND_ requiring that all derived classes that were subject to be instatianted _MUST_  have an implementation of these virtual functions. Also, the base class cannot be instantiated like

     Display anyBase("filename");     // ERROR, abstract class cannot be instantiated

If you provide a base class functionality for LoadConfigData(..) and PollDirectory() these functions must not be virtual. However, if you want to be able to override base class functionality, you _HAVE_ to declare them virtual in the base class.

Summary: A pure virtual function is a means to force derived classes to provide an implementation for that function. If there might be derived classes that don't need an implementation of these functions or if you want to have instances of the base class, e. g. to handle file types where no specialized class exist, than you shouldn't make it abstract.

Regards, Alex
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now