Solved

about abstract base class!

Posted on 2000-03-30
9
210 Views
Last Modified: 2012-05-04
 I saw molliza source code Like:
 
 class IBase;
 class IC:public IBase{
  public:
   static const nsIID& GetIID()  {};
   
   b()=0;
   c()=0;
}

can i call IC a abstract base class?
why they use IBase?
0
Comment
Question by:lxtsy
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Accepted Solution

by:
degarn earned 20 total points
Comment Utility
From the moment you have at least a pure virtual fuction (as b()=0 is) as a memeber of your class, you can consider that class as an abstract class ( your compiler will confirm it to you if you try to instanciate IC class).

The interest of deriving your abstract class from another class depends on what is included in that class (IBase here).
0
 

Author Comment

by:lxtsy
Comment Utility
IBase include nothing which make me feel
strange!
0
 

Expert Comment

by:degarn
Comment Utility
Do you mean nothing is interesting in IBase that has to be retrieved in IC or do you mean you see nothing in IBase that has to deal with abstract classes ?

Could you give me more details ?
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
the code you post won't work, because IBase has not been full declared (only the name has been forward declared).

What your code show is a FORWARD DECLARTION.  Not an abstract base class.

And abstract base class is one that has a pure virtual (=0) (ie. you cannot create an object of that type, only derive from it and it forces you to override the pure virtual in any concrete class you derive).

A really nice ABC has only pure virtula functions and no dat amemeber .. this then simply describes and interface.

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:PKothari
Comment Utility
Well the class also has a static function which does not need an object to be instantiated to be called.
I mean u can call a static function as
Class Name::Function name.

As of now the class is an abstract class

Derving from IBase seems to be intresting after knowing what are the member functions and data members of that class.If the base calss contains some static data members then i think u can access them using the static member function of derived class.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
OK .. I've looked at your code / message again.  Didn't quite get the gist of what you were asking before.

IC is definitely an abstract base class because it has a pure virtual function.  That is all it takes.

NOTE: Pure virtual does NOT mean that the function has no implementation in for the ABC.  All it does is force the programmer to provide an override for that function in any class derived from the ABC - if he wants to create objects of that class.

It is quite legal, useful and legitimate to have an implementation of a pure virtual function in an ABC.
0
 
LVL 3

Expert Comment

by:Try
Comment Utility
There are a number of things I would like to point out about your sample code in effort to clarify certain points.

1.  Having:

class IBase;
class IC : public IBase
{...};

will NOT work (just as "RONSLOW stated) and you will receive a compile time error, because the class specified in the derivation list MUST have already been defined prior to being specified as a base class.  PERIOD!!!  It doesn't matter whether it's later defined with static member(s) inside it.  Once you include a class name in a derivation list, that class MUST have already been defined.  As you show it (just as "RONSLOW" pointed out again), it's nothing more than a forward declaration.


2.  OK.  Let's say your hierarchy looks like this:

class  IBase
{...};
class  IC : public IBase
{
  ...
  b() = 0;
  c() = 0;
};

Is class IC an abstract base class?

The answer is NO! because you will again receive a compile time error since abstract base class refers to class(es) with pure virtual functions.  Neither 'b()' nor 'c()' are virtual functions and their having the incorrect syntax is what will produce the compile error.  If they were virtual functions with the "= 0", then the answer would be YES! because the rule states that a class containing (or inheriting) one or more pure virtual functions is recognized as an abstract base class by the compiler.

NOTE:  I am only amplifying what "RONSLOW" has already stated.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
I asusmed that the 'virtual' keyword was missing from the original message (just like the return type).

Asusming it is just a typo, and the original looked like

class  IBase
{...};
class  IC : public IBase
{
  ...
  virtual void b() = 0;
  virtual void c() = 0;
};

then IC is an ABC.

It is also possible that b and c were declared virtual in IBase, like this

class  IBase
{
  ...
  virtual void b();
  virtual void c();
  ...
};
class  IC : public IBase
{
  ...
  void b() = 0;
  void c() = 0;
};

In which case, I THINK that IC wil compile OK and still be an ABC
0
 
LVL 3

Expert Comment

by:Try
Comment Utility
"RONSLOW", you are correct (given the layout you have shown), because the 'virtual' keyword only need to be used once for a particular function, and subsequently derived members of similar names and signature will be considered virtual.  The repeated use of the keyword 'virtual' in subsequently derived classes (for functions of similar names and signature) are done mostly for consistency and visual effect.  Functions of similar names and signatures are virtual in derived classes, even without the keyword 'virtual' preceding the function names.

On the matter of whether class IC would be an ABC, the answer to that is a bit tricky, and let me explain that (and I had to look this up in order to be sure).

If you were to leave the hierarchy just the way it now is, meaning just the two classes, IC could NOT be an ABC, and here's why.

According to the rule, "An abstract base class can only occur as a subobject in subsequently derived classes."

This means that without another class derived from IC, it could not be an ABC.  Given the way the hierarchy is now defined, there would have to be another class derived from IC, in order for IC to be considered an ABC.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

6 Experts available now in Live!

Get 1:1 Help Now