Solved

about abstract base class!

Posted on 2000-03-30
9
214 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
ID: 2672285
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
ID: 2672379
IBase include nothing which make me feel
strange!
0
 

Expert Comment

by:degarn
ID: 2672420
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
LVL 10

Expert Comment

by:RONSLOW
ID: 2672771
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
 

Expert Comment

by:PKothari
ID: 2677582
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
ID: 2678472
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
ID: 2678585
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
ID: 2678592
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
ID: 2679273
"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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

791 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