-
June 8th, 2018, 04:47 PM
#1
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:
??
so how can i use an Abstract class?
-
June 8th, 2018, 05:41 PM
#2
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.
-
June 8th, 2018, 06:28 PM
#3
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;
};
-
June 9th, 2018, 06:55 AM
#4
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
-
June 9th, 2018, 07:50 AM
#5
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)
-
June 9th, 2018, 09:02 AM
#6
Re: How use an Abstract class?
if i do:
Code:
virtual int get() override;
i must define it, by C++ rules
-
June 10th, 2018, 03:09 AM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|