CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [RESOLVED] can i change the class function without declaration?

    see these sample:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    class test
    {
        public:
         virtual void hey()
         {
             cout << "hello world!!!\n";
         }
    };
    
    
    class test2: public test
    {
    
    
    }test2;
    
    
    void test2::hey()
    {
        cout << "hey brother\n";
    }
    the 'hey' is declared on 'test'.. if the 'test2' is from 'test', why i must re-declare the 'hey' function?
    (i mean i must change the 'test' functions, on a derive class without re-declare them)

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: can i change the class function without declaration?

    I'm not clear what question you are asking? Consider:

    Code:
    #include <iostream>
    using namespace std;
    
    class test
    {
    public:
    	virtual void hey() const
    	{
    		cout << "hello world!!!\n";
    	}
    };
    
    
    class test2 : public test
    {
    public:
    	void hey() const override
    	{
    		cout << "hey brother\n";
    	}
    };
    
    int main()
    {
    	test* t = new test;
    	test* t2 = new test2;
    
    	t->hey();
    	t2->hey();
    }
    then this displays:

    Code:
    hello world!!!
    hey brother
    using hey() from the required class. If you want a different function to be called for different classes but with the same name (polymorphism) then the required functions have to be defined.

    These can be defined as:

    Code:
    #include <iostream>
    using namespace std;
    
    class test
    {
    public:
    	virtual void hey() const;
    };
    
    
    class test2 : public test
    {
    public:
    	void hey() const override;
    };
    
    void test::hey() const
    {
    	cout << "hello world!!!\n";
    }
    
    void test2::hey() const
    {
    	cout << "hey brother\n";
    }
    
    int main()
    {
    	test* t = new test;
    	test* t2 = new test2;
    
    	t->hey();
    	t2->hey();
    }
    to separate out the function body from the class declaration. But in this case a declaration must still be present in the class definition.
    Last edited by 2kaud; January 19th, 2020 at 05:42 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: can i change the class function without declaration?

    hey... hello
    i mean:
    Code:
    #include <iostream>using namespace std;
    
    class test
    {
    public:
    	virtual void hey() const;
    };
    
    
    class test2 : public test
    {
    public:
    	//void hey() const override; //without these line
    };
    
    void test::hey() const
    {
    	cout << "hello world!!!\n";
    }
    
    void test2::hey() const
    {
    	cout << "hey brother\n";
    }
    
    int main()
    {
    	test* t = new test;
    	test* t2 = new test2;
    
    	t->hey();
    	t2->hey(); }
    if the 'test' is public and public on 'test2' and the 'hey' is virtual, why we must declare it on 'test2'? is there a way for avoid the declaration?

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: can i change the class function without declaration?

    is there a way for avoid the declaration?
    No. Hey() can be a non-member function outside of the classes that takes a pointer/reference to a class as one of its arguments (like getline() does). However you loose the polymorphic ability and unless this non-member function only needs access to public variables (not good) or info returned from a 'getter' class function, then the class will need to declare this function as a friend. So you just might as well declare the function within the class.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: can i change the class function without declaration?

    understood... thank you so much for all

Tags for this Thread

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