Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

about abstract base class!

Posted on 2000-03-30
9
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Accepted Solution

by:
degarn earned 60 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

730 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