I've been reading about abstract classes and I can't figure out their purpose. Also, are they suppose to have member functions and data members?
Thanks in advance!
I've been reading about abstract classes and I can't figure out their purpose. Also, are they suppose to have member functions and data members?
Thanks in advance!
Abstract classes support ,human way of thinking'; example: say your sister is in the kitchen, and you are somewhere else in the house (maybe programming an abstract class). If your sister tells you there is somebody else in the kitchen, this won't disturb you. But if she told you there is a robber in the kitchen, it is likely you will call the police. 'robber' is a very special 'somebody'. Now our classes:
class somebody{
public:
virtual void react()=0;
};
// It is useless to create an object of the class somebody for
// there is not enough information about its properties, and it
// is not clear why 'somebody' should have a member like
// 'dangerous'
class robberublic somebody{
bool dangerous;
public:
robber(){dangerous=true;}
virtual void react(){
CallPolice();
};
somebody* WhoIsInTheKitchen=new robber;
WhoIsInTheKitchen->React();
// and the code above demonstrates the purpose of abstract
// classes - I hope
--------------------------------------------------------------------------------
That makes sense, thanks!
Originally posted by Mecnels
Abstract classes support ,human way of thinking'; example: say your sister is in the kitchen, and you are somewhere else in the house (maybe programming an abstract class). If your sister tells you there is somebody else in the kitchen, this won't disturb you. But if she told you there is a robber in the kitchen, it is likely you will call the police. 'robber' is a very special 'somebody'. Now our classes:
class somebody{
public:
virtual void react()=0;
};
// It is useless to create an object of the class somebody for
// there is not enough information about its properties, and it
// is not clear why 'somebody' should have a member like
// 'dangerous'
class robberublic somebody{
bool dangerous;
public:
robber(){dangerous=true;}
virtual void react(){
CallPolice();
};
somebody* WhoIsInTheKitchen=new robber;
WhoIsInTheKitchen->React();
// and the code above demonstrates the purpose of abstract
// classes - I hope
--------------------------------------------------------------------------------
Man you are completely wrong.. Please dont confuse peopple here...
An abstract class is defined abstract or it becomes abstract if one of the member function is not defined (depends on the language). An abstract class cannot be instasiated to create objects.. An abstract class is designed only to act as a base class (to be inherited by other classes). It is a design concept in program development and provides a base upon which other classes may be built.
Let me say that you are working in a company that has to develop a big software project.. Thee are hundreds of modules and each programmer is assigned a module.. There needs to be a standard way of doing it so that all the modules can be integrated later...
So what the project leader does is create an abstract class.. so it contains all the modules (functions) prototype i.e. the parameters it take , the value it returns, the function nae etc.. so what you do is, you inherit this abstract class in your class and define the function.. So there is a standard maintained.. You dont define the name of the function, parameters it takes to your wims and fancies...
Last edited by vasanth; 04-29-2003 at 06:25 AM.
// don't write hundreds of virtual function declarations into your
// abstract base class, try this:
template <typename T> StandardReturnValue Fn ( T ){
}
// this is usually my first thought when told to define a standard
// for hundreds of modules, writing an abstract base class would
// cause wounded fingers (!)
So WHY are there abstract classes? There was a time when there were no programmer-defined classes at all (programmers had some ints and maybe chars, but certainly no MENUITEMINFO or LPDIRECTDRAWSURFACE7 objects to encode programs). The advantage of programmer-defined classes is to contain data concerning the same real-world-object in only one variable. For this reason it is possible to 'store' a 'lung' (f.e.) in memory. A 'lung' is a very concrete sort of an organ. One can store something like its volume or also a pointer to a surface which contains a graphic of a lung. But there are also things like an 'organ' in general on this world. Though everybody knows what an 'organ' in general is, somebody told to draw one will tell you not to have enough information to manage 'drawing an organ in general'. As a computer is even more stupid than this poor guy ordered to 'draw an organ in general', the base class of 'lung' (f.e. class organ_in_general) will not have a member LPDIRECTDRAWSURFACE7 picture_of_organ. A surgeon may know how to perform an operation on a person's lung, but if told to perform an operation on a person's organ (in general) he might decline to fulfill the task, though he could take out any organ if he knew which which specific organ is meant.
So, our base class may need something like :
class organ_in_general{
public:
virtual void perform_operation(bool tonarcotize)=0;
};
// ... where perform_operation may do very different things,
// ... depending on the specific organ (lung f.e.), which needs to
// ... be a class derived from organ_in_general.
Now, there are abstract classes for the only purpose of enabling programmers to work with abstract categories in their programs, it is clear that as there is no real-world-object 'organ_in_general' there can't be variables of the organ_in_general-class in a program, and if you know why there are abstract classes, you won't try this.
--------------------------------------------------------------------
I agree with vasanth.
>>Mecnels
I don't think the example you gave is good, although I think it sounds more like polymorphism! That you react to the object according to it's type at run-time, when you are using it's base class pointer to point to it, if that function is declared virtual, the one defined in it's class will be called.
none...
to you: hundreds of virtual declarations cause hundreds of 4-byte-of-memory-consuming Virtual Function Pointers, if you have heard of them. (Don't do this, especially if the virtual functions in the derived classes have to do blits, unless you want a maximum screen update of 20 fps or so.)Originally posted by vasanth
So what are you trying to say
Originally posted by Mecnels
to you: hundreds of virtual declarations cause hundreds of 4-byte-of-memory-consuming Virtual Function Pointers, if you have heard of them. (Don't do this, especially if the virtual functions in the derived classes have to do blits, unless you want a maximum screen update of 20 fps or so.)
ohh boy here we go again...
I was talkin about abstract classes and not V Functions.. i agree with you that the pointers used in case of virtaul functions do consume memory but they do provide an advantage of choosing the function at runtime which is verry important in some situations.. And you seem to be talking in perspective to some graphics (Hospital, doctor) some project... Please be more general...
And your understanding of abstract classes is wronged... You can never instansiate an abstract class but you have claimed so in your first post....
Dont take anything personaly.. you seem to be poking fut at my knowledge about pointers and their memory consumption... by saying "If you have herd of them". I am sorry to tell you that i have worked on many projects involving subjects such as AI where my entire project was based on dynamic memory and dynamic decesion making..
And please dont continue to do this to other members here.. This board is here to help programmers both new commers and advanced programmers.. We are all in the learning process so please dont misguide any one if you are not sure of a topic..
Thank you
Please excuse me if I offended you. I didn't want this.Originally posted by vasanth
ohh boy here we go again...
I was talkin about abstract classes and not V Functions.. i agree with you that the pointers used in case of virtaul functions do consume memory but they do provide an advantage of choosing the function at runtime which is verry important in some situations.. And you seem to be talking in perspective to some graphics (Hospital, doctor) some project... Please be more general...
And your understanding of abstract classes is wronged... You can never instansiate an abstract class but you have claimed so in your first post....
Dont take anything personaly.. you seem to be poking fut at my knowledge about pointers and their memory consumption... by saying "If you have herd of them". I am sorry to tell you that i have worked on many projects involving subjects such as AI where my entire project was based on dynamic memory and dynamic decesion making..
And please dont continue to do this to other members here.. This board is here to help programmers both new commers and advanced programmers.. We are all in the learning process so please dont misguide any one if you are not sure of a topic..
Thank you
But separating abstract classes and virtual functions is though impossible.
Let's answer all your questions.
C++ FAQ Inheritance - Virtual Functions
C++ FAQ Inheritance - abstract base classes (ABCs)
Quote:I was talkin about abstract classes and not V Functions..
"At the programming language level, an ABC is a class that has one or more pure virtual member functions. You cannot make an object (instance) of an ABC. "
You can't separate virtual functions from abstract classes.
Last edited by Dante Shamest; 04-29-2003 at 12:17 PM.
Interesting thought for the day.......
Is this an abstract base class?
Code:class ABC { public: int Get_hidden()const { return hidden_ ;} protected: ABC(int x) : hidden_(x) {} ~ABC() {}; ABC(const ABC& rhs) : hidden_(rhs.hidden_) {} private: int hidden_; };
Last edited by Stoned_Coder; 04-29-2003 at 02:37 PM.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Don't think so. I can derive a subclass, and still don't have to implement the constructors in the base class ABC.
Although I can't make an object from ABC, it doesn't qualify it as an abstract class.
EDIT:
An abstract class must force a derived class to implement at least one method, assuming the derived class wishes to create objects from itself. If the derived class does not implement the method, then the derived class itself is an abstract class.
Last edited by Dante Shamest; 04-29-2003 at 02:54 PM.
class ABC cannot be instantiated so it certainly ins't concrete.
You never implement base constructors in derived objects. constructors are not inherited.
In the case of ABC the constructor must be called by the deriving class as there is no default constructor.
class ABC also specifys an interface. ok a lame one but it was just to illustrate a point. The difference here is that the interface is not separated from the implementation so suppose it was, suppose i had used the pimple idiom and hid the privates behind a pointer to an implementation class.Does the fact that this class is not designed for polymorphic use stop it from being an abstract class?
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Frankly, I don't know. I'll like to just point out here I'm not an abstract thinker (pun intended).Does the fact that this class is not designed for polymorphic use stop it from being an abstract class?