i want to make abstract class, but I usually hears that we can't do that without pure virtual function , but i'm trying to do that via constructor but without virtual
like this
[code]#include <iostream>
using namespace std;
class base{
int x,y;
public:
base(int q,int w)=0;
};
class deriverd :public base{
public:
deriverd();
};
base::base(int q, int w){q=w=0;}
deriverd::deriverd():base(0,0){;}
but i wonderful , because i don't ensure that it's perfect abstract class
can help me that if my code have any probability to perfect-less abstract class, or my code have to be error prone ??
The pure-specifier that is "= 0" may be used only in the declaration of a virtual function. A constructor can not be virtual.
But you can declare a destructor as a pure virtual function
It's not possible to have pure virtual constructors. If you just want to make it impossible to create instances of base you can make the constructor protected.
1 2 3 4 5
class base{
int x,y;
protected:
base(int q,int w);
};
I want to make abstract class just , i not intend to do any virtual pure function , i asked for if i want to make that abstract class just , is my code correct?
An abstract class has at least one pure virtual function, which means it has to be inherited to be used.
If you just want inheritance then make sure your class's methods are virtual but don't do the "=0" thing.
No, your code is not correct, you can't have virtual constructors. Also, your constructor is not even virtual, just pure, which is also illegal.
1 2 3 4 5 6 7 8 9 10 11
class Base
{
public:
virtual FuncWithPolymorphism(); //Base function
}
class Derived: public Base
{
public:
virtual FuncWithPolymorphism(); //Redefinition
}
#include <iostream>
usingnamespace std;
class base{
int x,y;
public:
base(int q,int w)=0;
};
class deriverd :public base{
public:
deriverd();
};
base::base(int q, int w){q=w=0;}
deriverd::deriverd():base(0,0){;}
void main(){
base object(0,0);
}
the error that it happened
: error C2259: 'base' : cannot instantiate abstract class
according my abstract definition my class is correct.
then i can make abstract class base(int q,int w)=0;
???????