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

    How use an Abstract class?

    see these simple class with a base class:
    Code:
    class base{
    
    
    public:
        virtual void MouseClick(){;};
        virtual void Move(){;};
    	base()
    	{
            MouseClick();
            Move();
    	}
    
    
    	void call()
    	{
    
    
            MouseClick();
            Move();
    	}
    };
    
    
    class derived : public base
    {
        public:
        virtual void Move()=0;
        virtual void MouseClick()=0;
    };
    
    
    void derived::MouseClick()
    {
        std::cout << "Mouse click\n";
    }
    
    
    void derived::Move()
    {
       std::cout << "Move\n";
    }
    i never used an Abstract class, so i need several advices:
    - why i can't create a derived object\instance?
    - why i can't do:
    Code:
    derived.call();
    ??
    so how can i use an Abstract class?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How use an Abstract class?

    The syntax around the virtual methods in the base class doesn't look correct.

    Google 'abstract class c++'. Lots of info.
    Last edited by Arjay; June 8th, 2018 at 05:44 PM.

  3. #3
    Join Date
    Aug 2006
    Posts
    231

    Re: How use an Abstract class?

    The base class should be abstract, not the derived class. It should look something like this:

    Code:
    class base
    {
    public:
        virtual ~base() = default;
    
        virtual void Move() = 0;
        virtual void MouseClick() = 0;
    };
    
    class derived : public base
    {
    public:
        void Move() override;
        void MouseClick() override;
    };

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

    Re: How use an Abstract class?

    my base and my derived class using pure virtual functions for i don't define the functions, unless i need them.
    and maybe these is the big problem

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

    Re: How use an Abstract class?

    You can't instantiate an object of a pure virtual class. You can only instantiate from a class that defines the virtual functions. Consider

    Code:
    struct my {
    	virtual int get() = 0;
    };
    
    struct myd : my {
    	virtual int get() override = 0;
    };
    
    struct mydd : myd {
    	virtual int get() override
    	{
    		return 5;
    	}
    };
    
    int main()
    {
    	mydd dd;
    	cout << dd.get();
    
    	//my m;	Error my is pure virtual
    	//myd d;	Error myd is pure virtual
    }
    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)

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

    Re: How use an Abstract class?

    if i do:
    Code:
    virtual int get() override;

    i must define it, by C++ rules

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How use an Abstract class?

    Well, if you want the derived class to be a concrete class such that you can instantiate objects of that class, then you do need to define the pure virtual member functions inherited from the abstract base class. If you intend the derived class to also be an abstract class, then sure, don't declare an override for the pure virtual function and hence don't define it, but then you also cannot create an instance of that derived class because it remains abstract, i.e., because it has at least one pure virtual member function.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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