Thread: Some simple questions

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    722

    Some simple questions

    Some simple question, that require simple answers:

    How can I force the implementation of methods in a class, like the Java's interfaces, if possible?
    Plus, how do I declare an abstract class?
    What is the relation between this and virtual functions?
    And how do the protected fields behave (I read the tutorial but didn't understand well)?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    For many of your questions there are answers at:
    www.bruceeckel.com
    Thinking in C++
    This books is among the best!
    Last edited by Micko; 07-07-2004 at 02:29 PM.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>How can I force the implementation of methods in a class
    Declare a function (method) as virtual or pure virtual, and overload it in a derived class (actually, if you declare an object of the derived class then I don't think you need to declare the function as virtual at all).

    >>Plus, how do I declare an abstract class?
    Any class with a pure virtual function is an abstract class.

    >>What is the relation between this and virtual functions?
    A virtual function is one that is meant to be overloaded. If you declare a pointer to a base class, then call the base class's virtual function, then the derived class's function will be called.

    >>And how do the protected fields behave
    Makes it so only the class itself and its derived classes can access the members declared as protected.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Declaring it as virtual does not mean it must be reimplemented. Declare it as pure virtual:
    Code:
    class A {
      public:
        virtual void foo( ) = 0; // Pure virtual. Must be reimplemented in anything to be instantiated.
    };

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Zach L.
    Code:
        virtual void foo( ) = 0;
    So this is a pure virtual function, and must be implemented in derived classes, right? Without the "= 0", it would be simply a virtual function, and the class abstract...
    But does it really have to be 0 or can be any number? What is that number suposed to do?

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Without the "= 0", it would be simply a virtual function, and the class abstract...
    I don't believe the class would be abstract without the "= 0". And yes, it has to be 0, although I don't think it really does anything except tell you that it's a pure virtual function. Maybe 0 is symbolic for nothingness, i.e. there's no function definition.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The class would not be abstract (or rather, not an abstract data type [ADT]) without the '= 0'. A class with a pure virtual cannot be instantiated. And, yes, it must be 0.

  8. #8
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    just to note, " = 0" is nothing more than a tag that the compiler notices. If it sees this in a class, and sees you instantiating an object of that class, it's supposed to yell at you. I used to think that it would be declaring that function pointer to NULL, and when you define it in child classes, it would change the pointer for that class, but I was informed otherwise.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    A final thing... (no need to make another thread )

    Imagine I have the following class:
    Code:
    template<typename T>
    class Array{
    private:
    	T *array;
    	uint _length;
    public:
    	Array(uint capacity = 0) :_length(capacity){	array = new T[capacity];	}
    	~Array(){	delete array;_length=0;	}
    
    	T operator[](uint idx) const;
    	//more operators
    	uint length() const;
    };
    Can I declare an operator to make operation like this?
    Array<int> a(3);
    a[0] = 5;//<<--operator
    Last edited by xErath; 07-07-2004 at 11:15 PM.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You'd need to return a T& instead of just T.
    Last edited by Hunter2; 07-08-2004 at 09:51 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    I didn't understand well..
    Code:
    template<typename T>
    T Array<T>::operator[](uint idx) const{
    	return array[idx];
    }
    I have this to do, e.g.:
    Array<int> a(3);
    int n = a[2];

    But I want to add an operator to do also:
    a[2] = n;

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    template<typename T>
    T& Array<T>::operator[](uint idx){
    	return array[idx];
    }
    Try that (and you have to change the function in the class declaration too).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    That worked very well!! Thank you

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    No problem You do know how and why it works though, right?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Look at this thread for some explanation of why it works as it does: http://cboard.cprogramming.com/showthread.php?t=54632

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A couple questions
    By Flakster in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2005, 01:22 PM
  3. Few simple questions...
    By Shane in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2005, 02:40 AM
  4. A few simple batch questions
    By sean in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-02-2003, 01:35 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM