Solved

Abstract class

Posted on 2004-05-02
11
819 Views
Last Modified: 2006-11-17
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
Comment
Question by:jewee
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 5

Expert Comment

by:dennis_george
Comment Utility
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
 
LVL 5

Accepted Solution

by:
dennis_george earned 300 total points
Comment Utility
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
 
LVL 5

Expert Comment

by:dennis_george
Comment Utility


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
 

Author Comment

by:jewee
Comment Utility
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
 

Author Comment

by:jewee
Comment Utility
ANother thing too, the same functionality exists for LoadConfigFile, so I probably won't want to set up an abstract class???
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:dennis_george
Comment Utility
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
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
 

Author Comment

by:jewee
Comment Utility
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
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 150 total points
Comment Utility
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 50 total points
Comment Utility
>> 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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now