Thread: Base classes

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367

    Base classes

    Hello all,

    I am writing a base class that will be inherited by other classes. However, I do not want this class to be instantiated directly. I only want it to be inherited from. Is this possible?

    Thanks, dene.
    Be a leader and not a follower.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    13
    Yes there is use something like the following code segment.

    Code:
    virtual class MyClass
    {
         public:
             int get_data();
             virtual void my_function();
         private:
              int data;
    };
    "Computer Science is no more about computers than astronomy is about telescopes"
    - E.W. Dijkstra

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    http://msdn.microsoft.com/library/de...m/deriv_15.asp

    Although you could still have pointers or references to a class object
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    "Although you could still have pointers or references to a class object". Does this mean that I can still do: MyClass *t = new MyClass()?
    Be a leader and not a follower.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    No...First of all it would be:
    MyClass* T = new MyClass

    and second, you could not do it anyway
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Ok, thanks for everyones help.
    Be a leader and not a follower.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    virtual class ..
    ???
    I doubt that's allowed in C++. Abstract classes have at least one pure virtual method:
    virtual void pure() = 0;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I doubt that's allowed in C++.
    It isn't. But it could just be a new feature recently added to the language by the extensions committee. You know how much they love adding new features.
    My best code is written with the delete key.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The pointers you can use like this:
    Code:
    class A
    {
       virtual void foo( ) = 0;
    };
    class B : public A
    {
       void foo( ) { }
    };
    
    // Somewhere else...
    A* obj = new B; // ... = new B( ); would also work, though
    And if you want a class that should be an abstract data type, but none of the member functions lend themselves to being pure virtual, you can have a pure virtual destructor.

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You could make the constructor protected.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    13
    Nope, I don't think it's a new feature its a typo sometimes I break into an accidental Java type code except in Java the class is preceeded with the keyword abstract. Thanks for catching that CornedBee!
    "Computer Science is no more about computers than astronomy is about telescopes"
    - E.W. Dijkstra

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Sang-drax
    You could make the constructor protected.
    True... But it is still possible to instantiate it (static builder function, for example). Granted, the particular application will determine if this makes sense or not in the context of the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Virtual base class
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2008, 07:45 AM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  5. Virtual Base Classes
    By skewray in forum C++ Programming
    Replies: 11
    Last Post: 12-21-2006, 06:56 PM