CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: abstract class

  1. #1
    Join Date
    Dec 2008
    Posts
    9

    abstract class

    A class with a pure virtual function will be an abstract class.

    1. we cannot have constructor for abstract class because we cannot instantiate an abstract class;

    2. can we have non pure virtual methods inside an abstract class?

    3. can we have member variables inside an abstract class which can be initialized by its subclass? if yes, how to initialize them?

    If C++ abstract class is the same as Java Interface, no member variables are allowed, only constants and abstract methods are allowed.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: abstract class

    Quote Originally Posted by codeguru001
    1. we cannot have constructor for abstract class because we cannot instantiate an abstract class;
    No, you can provide a constructor for an abstract class. Recall your constructor initialisation list questions: a derived class constructor can still invoke the constructor of an abstract base class. This would also answer your question #3.

    Quote Originally Posted by codeguru001
    2. can we have non pure virtual methods inside an abstract class?
    Yes, but you should have tried putting them in yourself before asking.

    Quote Originally Posted by codeguru001
    If C++ abstract class is the same as Java Interface, no member variables are allowed, only constants and abstract methods are allowed.
    C++ abstract class is roughly the same as Java abstract class. C++ does not have the equivalent of a Java interface construct, though an abstract class can be designed to be like an interface construct.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: abstract class

    LaserLight is correct. Some addiitional items.

    1) A method is abstract is it hase a "=0" specitier.
    2) abstract methods MAY (but are not required to have) implementations.
    3) abstract methods must be virtual
    4) abstract methods must by overriden in derived classes
    5) a class is abstract if it has at least one abstract method
    6) a method is "pure abstract" if itis abstract and does NOT have an implementation
    7) a class is "pure abstract" if it consists of ONLY pure abstract methods.
    8) a pure abstract class is an "interface".
    9) an abstract destrucrtor (unlike any other method) must STILL have an implementation [Thanks to Russco for pointing this out...]
    Last edited by TheCPUWizard; December 10th, 2008 at 03:16 PM. Reason: Added #9 for completenesss...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: abstract class

    One more thing worth noting is that sometimes its advantageous to have all common implementation in an abstract class with all virtuals having a default implementation then to make the class abstract the best route is to make the virtual destructor abstract, however in this case the destructor MUST have an implementation provided.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  5. #5
    Join Date
    Dec 2008
    Posts
    9

    Thumbs up Re: abstract class

    You guys' summary is really great! I cannot get those bundled info from books.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: abstract class

    Quote Originally Posted by codeguru001 View Post
    You guys' summary is really great! I cannot get those bundled info from books.
    Not sure that books you are using, but I checked three of mine, and all points are covered.

    This is one of the reasons a "learner" must follow a book carefully (that means reading every word (no skiimming, skipping, or browsing), and manually typing every line of code then stepping through every line with the debugger).

    Very often the critical piece of information you need will be a single sentance or a specific feature of one line of code in a sample.

    Learning a computer language (especially C++) is no time for "Short-cuts", they wil simply hurt you in the end.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: abstract class

    pure virtual function declaration provide the prototype of the method. but an implementation of the pure virtual function is typically not provided in an abstract class (in 99% of situations), but it may be included. Every non-abstract child class is still required to override the method, but the implementation provided by the abstract class may be called in this way:

    Code:
    class Abstract {
    public:
       virtual void pure_virtual() = 0;
     };
    
     void Abstract::pure_virtual() {
       // do something
     }
     
     class Child : public Abstract {
       virtual void pure_virtual(); // no longer abstract, this class may be
                                    // instantiated.
     };
     
     void Child::pure_virtual() {
       Abstract::pure_virtual(); // the implementation in the abstract class 
                                 // is executed
     }
    this is the flexibility of C++ but i am not sure we can do such thing in C#!
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: abstract class

    Quote Originally Posted by toraj58 View Post
    pure virtual function declaration provide the prototype of the method. but an implementation of the pure virtual function is typically not provided in an abstract class (in 99% of situations), but it may be included.

    this is the flexibility of C++ but i am not sure we can do such thing in C#!
    1) Actually I find that the base class provides an implementation in about 20%-33% of my designs, especially when the hierarchy is truely an "is-a" relationship.

    2) Why is C# even being mentioned in the C++ forums????
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: abstract class

    im not talking about simple base class i am talking about abstract class the main goal of abstarct class is to let inherited classes implement all or at least one of its abstarct methods. it is an option not a force; base class is somthing and abstract class as a base for childs is another story.

    about c# i mentioned that you come here and make me correct if i am wrong bacuse i don't think there is any way to implemet an abstract method in abstract class. in comparsion between languages learn us something new.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  10. #10
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: abstract class

    also my point was to implement a pure virtual function in the abstract class:

    Code:
    class Abstract {
    public:
       virtual void pure_virtual() = 0; 
    };
    
     void Abstract::pure_virtual() {
       // do something
     }
    Last edited by toraj58; December 11th, 2008 at 03:39 PM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: abstract class

    When you declare a method as abstract AND provide an implementation, then:

    1) The derviced class must over-ride it with an implementation.
    2) The derived class may call the base class method internally.

    Now we look at class hierarchies. public inheritance means "is-a". Consider:
    Code:
    class Foo() 
    {
    }
    
    class Bar() : public Foo()
    {
    }
    Now you write a whole bunch of business requirements and other documents about "Foo". You MUST be able to do a "search/replace" of the word "Foo" with "Bar" (and make no other changes) and still have the document be 100% correct, otherwise public inheritcance is NOT appropriate.

    Second, it is generally a good idea to make all non-leaf (base) classes abstract. This can be accomplished by the destructor being virtual allone, but it is often has advantages to make the people who will use your base class THINK about key operations, even if 99% of the derived classes all have the same behaviour. This is exactly where an abstract method with implementation applies.

    FYI: C# does not support abstract with implementation. You can come close with an abstract method and a protected method (slightly different names). The derived classes can then call the protected method in the base. This pattern is also used in C++ by a fair number of software development shops.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  12. #12
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: abstract class

    FYI: C# does not support abstract with implementation
    this is why i said 99% (trap) and you come and say 20% - 30% ... it means that you either don't read my post compltly or you don't understand what i mean.

    i knew that that c# don't support it dirctly (i have test it before) but in c++ we have a lot of detour to do something that is weird because it is flexible; in my opinion more that C# and ineherntly they have major differnce becuase C# sholud adhere to CTS, CLS and CLR and MSIL and is not as free as C++ to have some flexibility. most of the programmers and coders are not software architecture and they are genius and want to solve some problem with trick, detour and not only to worry about the ultimte structre of the program. they want to have output in the shortest time and they work individually.

    in a nutshell and IMHO C# is not good choice for a genius programmer; i have about 3 years experince with C# and i have red about 10 books and written hundreds program (from AI to Game) but i am not still convenience with it as C++ and for my 3D Game project i work with C++ and DirecX and i don't see any thing attactive in emerging technlogies LIke XNA, Managed DirectX and even C# other that simlicity. BTW i hate simplicity and it brings up performance hint it most cases. the good example is between C and C++.
    Last edited by toraj58; December 11th, 2008 at 04:43 PM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: abstract class

    This is the C++ forum, I fail to see WHY you are continually brining up other languages IN THIS FORUM. There are much better venues for such crooss language discussions, and I suggest you you them.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  14. #14
    Join Date
    Dec 2008
    Posts
    9

    Re: abstract class

    Quote Originally Posted by TheCPUWizard View Post
    Not sure that books you are using, but I checked three of mine, and all points are covered.
    can you recommend your books to me? What I want to say is those books I read is not as concise as you guys summary. I have some experience in programming languages and what I need now is some bundled info about a language. Those explanations are not important to me. I can think, and think again to figure out all those summarized points' meaning and use them.

  15. #15
    Join Date
    Dec 2008
    Posts
    9

    Re: abstract class

    Quote Originally Posted by TheCPUWizard View Post
    Second, it is generally a good idea to make all non-leaf (base) classes abstract. This can be accomplished by the destructor being virtual allone,

    Should be "This can be accomplished by the destructor being PURE virtual allone" and provide implementation in the same time. Am I right? Just want to make sure my understanding is correct.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured