Abstract Classes
Results 1 to 7 of 7

Thread: Abstract Classes

  1. #1
    Member
    Join Date
    Sep 2003
    Posts
    76

    Abstract Classes

    Just a quick question to see if anyone out there gives me a reply

    Is there any other reason to making a class abstract in C++ that its not to make some other programmer implement something you haven't???
    I Speak in frequencies even dogs have trouble hearing


  2. #2
    Senior Member
    Join Date
    May 2002
    Posts
    344
    what do you mean??
    An abstract class is any class that contains one or more virtual functions. An abstract class cannot be used to create objects, but it is still useful because you can create a family of dirived classes from your abstract base class. Lets say that you have a program and you create a class and in that class, you have a function that says talk. When an object named dog is passed, you dont want the talk function to say meow, instead you want it to say bark. However, when a cat object is passed, you want the talk function to say meow. This is when you would want to use a base class (abstract class) and derive some classes from your base class that all do different things but have the same names. i hope i made sense...and by the way, you made it sound like you were testing our knowledge...dont try to do that
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  3. #3
    Member
    Join Date
    Sep 2003
    Posts
    76
    Jeje sorry if i seemed to be testing your knowledge is not about that.
    I understand that a virtual function's purpose is so that when you have several classes and you have the same function for several of them, when the program is running it understands to what object the specified function is applied to (what you explained here). Its just that while reading about abstract classes, they tell me that the pure virtual functions have to be implemented by a derived class, so I wonder if that is done to force someone to implement something that maybe I dont know how to implement. They didnt give me a reason so I guessed it was for that.
    I Speak in frequencies even dogs have trouble hearing


  4. #4
    Senior Member
    Join Date
    May 2002
    Posts
    344
    wait i dont think you completly understand what a virtual function does in C++. Although it has to be declared in an abstract class (a base class), it has no function definition. Basically it just sits there, but it can be implemented by the dirived classes. (wait sorry i reread your post and it seems like you got it...sorry, omit what i typed above, i am to lazy to erase it).

    they tell me that the pure virtual functions have to be implemented by a derived class, so I wonder if that is done to force someone to implement something that maybe I dont know how to implement. They didnt give me a reason so I guessed it was for that.
    what book are you reading and who tells you that? I am sure it is correct, i havent done any serious C++ work in a long time so i am kinda rusty...however, i am sure they are correct. Why dont you try entering code into your compiler where you dont implement your virtual functions by your derived classes? Try that and see what kind of error message you get...you might get a warning. I guess they just dont want functions that are laying around with nothing inside of them...do you know how to define a pure virtual function in your base class by the way(trick question kinda...)? if you dont know how to implement your virtual functions in your dirived classes, then you should probably reread the section or go ask your teacher (i am pressumming this is for a class). Basically, i think you have to change the pure virtual function so you arent carrying any pure virtual functions from your base class over to your dirived class that dont do anything because they are afraid that you might call one through an object you created and the dirived classes wont know what to do with it because there is no real definition in the blank pure virtual function definition of your derived class. I am geussing that you would recieve a warring and not an error if you tried to compile a program without implementing your pure virtual functions...Anyways, I dont think what i typed made any sense, but i would advise you to reread the chapter in your book...if you have more problems, ill get out my books and go back to my notes and i will try to help you out a little bit better, but it is 10:30 here and i am tired cause i have been writing this damn preprocessor tutorial and i had to do a major workout for my crew team...
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  5. #5
    Member
    Join Date
    Sep 2003
    Posts
    76
    Thanx for your time Eskimo, I am taking a class on data structures using C++ (I love it jeje) We havent talked about abstract classes yet, but I was doing a little reading ahead and I bumped into this part about pure virtual functions (they divide virtual functions in three areas, basically the ones you have to make virtual, because like I said u used the same one for differente classes and in running time the program has to make a difference to what object it is being called for, the ones that you may or may not declare as virtual, cause maybe u think someone (or you) can make a derived class from yours and use the same function thats also in the base class, and the pure virtual classes that are exclusive to abstract classes (u equal them to cero by the way jeje)) anyways i kinda lost myself there, the compilers dont give me warning everytime, it depends on what the function is. In the case of the pure virtual one I tried it didnt give me a warning when I just put the headings and leave the implementation blank. I guess youre right about not taking virtual functions into derived classes that dont do nothing

    By the way sorry if i cant make myself really clear but english is not my first language. Spanish is, from Puerto Rico

    thanx again bye byee
    I Speak in frequencies even dogs have trouble hearing


  6. #6
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    There are good reasons for declaring an abstract class.

    - Make a base class abstract, then you can subclass it. You can use it in polymorphism.
    - Make it a "pure abstract class" by declaring its virtual methods as abstract
    Code:
    class MyBaseClass {
      virtual int GetSomething() = 0;
      virtual std::string GetName() = 0;
      virtual ~MyBaseClass();
    };
    Then the compiler should make sure that you never instantiate a MyBaseClass by accident (it would crash as soon as you called one of the virtual methods anyway).

    Slarty

  7. #7
    Member
    Join Date
    Sep 2003
    Posts
    76
    thanz for the reply slarty
    I Speak in frequencies even dogs have trouble hearing


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •