Equivalent of Java's "super" keyword

This is a discussion on Equivalent of Java's "super" keyword within the C++ Programming forums, part of the General Programming Boards category; What I want to do is to be able to access a method that was overridden in a subclass. I ...

  1. #1
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175

    Equivalent of Java's "super" keyword

    What I want to do is to be able to access a method that was overridden in a subclass. I could do it in Java but don't know how to do this in C++. It would be something like this:

    PHP Code:
    class A
    {
      public:
        
    void method();
    };

    void A:method()
    {
      
    //do something
    }

    class 
    : class A
    {
      public:
        
    method();
    };

    void B:method();
    {
      
    //do what is in class A's method() and then do something else  

    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Code:
    #include <iostream>
    
    struct A
    {
        void f()
        {
            std::cout << "A::f\n";
        }
    };
    
    struct B : A
    {
        void f()
        {
            A::f();
            std::cout << "B::f\n";
        }
    };
    
    int main()
    {
        B b;
        b.f();
    }
    - lmov

  3. #3
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    OK, I've done mostly Java programming but I've moved to C++. So you can say A::method() even if the method isn't static? I didn't know that....
    I would test it myself but I don't have a compiler on this computer.

    Thanks
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    As above, but if you are doing this from outside the class's structure, you can call the base function using a base pointer....

    Code:
    #include <iostream>
    using namespace std;
    
    class A{
    public:
    	void Method(void){cout << "Hello from A" << endl;}
    };
    
    class B : public A{
    public:
    	void Method(void){
    		cout << "Hello from B" << endl;}
    	void CallBoth(void){
    		Method();
    		A::Method();//1. Call with base scope
    	}
    };
    
    int main(void){
    
    A myA,*ptrA;
    B myB;
    
    	myA.Method();
    	myB.Method();
    
    	ptrA = &myB;
    
    	ptrA->Method();//2. Call the base function via a base pointer
    
    	myB.CallBoth();
    
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    The ". " and its copyright...

    When I first studied OOP, and Classes,
    they told us to think about "." as a copy right ...

    As in the last post,

    Originally posted by Fordy

    Code:
    #include <iostream>
    using namespace std;
    
    class A{
    public:
    	void Method(void){cout << "Hello from A" << endl;}
    };
    
    class B : public A{
    public:
    	void Method(void){
    		cout << "Hello from B" << endl;}
    	void CallBoth(void){
    		Method();
    		A::Method();//1. Call with base scope
    	}
    };
    
    int main(void){
    
    A myA,*ptrA;
    B myB;
    
    	myA.Method();
    	myB.Method();
    
    	ptrA = &myB;
    
    	ptrA->Method();//2. Call the base function via a base pointer
    
    	myB.CallBoth();
    
    	return 0;
    }
    [/B]
    After you inheret the public part from class A in B you will be able to access the methods of A class inside the B class.

    by writing A.method1(); // And So onn.

    I hope you like C++.
    C++
    The best

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by JasonLikesJava
    OK, I've done mostly Java programming but I've moved to C++. So you can say A::method() even if the method isn't static? I didn't know that....
    I would test it myself but I don't have a compiler on this computer.

    Thanks

    Yes you can because the constructor of the base class is called before the constructor of the other class.

  7. #7
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175

    Re: The ". " and its copyright...

    Originally posted by NANO
    I hope you like C++.
    I think I do like C++ better

    Thanks all. I've got it all figured out.
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ equivalent to java's Integer class
    By abriggs2 in forum C++ Programming
    Replies: 1
    Last Post: 08-29-2008, 01:09 PM
  2. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 07:22 PM
  3. Virtual Keyword
    By Shal in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2006, 11:37 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  5. how to search a keyword in C?
    By kreyes in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 07:22 PM

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