simple question

This is a discussion on simple question within the C++ Programming forums, part of the General Programming Boards category; Hi, I have seen some function defintion like this: virtual int test()const; virtual int test()const = 0; virtual int test()=0; ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    simple question

    Hi, I have seen some function defintion like this:

    virtual int test()const;
    virtual int test()const = 0;
    virtual int test()=0;

    I am wondering what the difference among them and "const" only for virtual only? or what they mean??

    Thanks
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    I guess I'll take a stab at this since nobody else has yet.

    First off, it appears that these are meant to be member functions of some base class from which a derived class will be created using inheritance.

    The const simply means that the function in question is not going to modify the particular instance of the object we are dealing with. Within a const function, the only member functions that can be called are other const functions. No changes to the objects data members can be made. The const qualifier is not limited to virtual functions only.

    The =0 parts indicate that these are pure virtual functions. If a class has any pure virtual functions then the class is called an abstract base class. One cannot construct an instance of an abstract base class, you can only use that class to create a derived class and you must overload all of those pure virtual functions in the derived class for you to be able to instantiate an object of the derived class.

    ex:
    Code:
    // Create an abstract base class, you cannot instantiate an object of this type
    class base
    {
    public:
        virtual int test() = 0;
    };
    
    // Create a derived class using the abstract base class
    class derived: public base
    {
    public:
        int test()
        {
            // Implement the test function, must be present in order to
            // instantiate objects of type "derived"
        }
        void func1() const
        {
            value = 10;  // This statement is illegal since this is a const
                         // function and it changes a class's member data
            func2();     // Also illegal since func2 is a non-const function
            func3();     // Fine since func3 is const function
        }
        void func2()
        {
            value = 10;  // This is fine, it is perfectly allowable to modify a
                         // member variable since it is a non-const function
        }
        void func3() const
        {
        }
        int value;
    };
    
    base b;     // Error - base is abstract class and you cannot create an instance of this class.
    derived d;  // Ok
    Basically the base class becomes a framework with a specified set of functions that form a basic interface with known arguments and known return values and which the creator of the derived class must implement themselves.
    Last edited by hk_mp5kpdw; 01-24-2005 at 11:14 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    Thanks, a lot!!!
    Don't laugh at me,I am just a SuperNewbie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21