when a base class pointer points to the object of it's derived class and if a function being overridden we use virtual function to solve the problem . So that we can access the own function of derived class using the pointer.
Like this , i was thinking that if there a way which can be applied on virtual keyword in variable , so that we can access the latest version of a variable in derived class using pointer.
#include <iostream>
using namespace std;
class base
{
public:
int x;//but what about this , if we add virtual keyword here.
//it will give error if trying to do so .
//but can you tell me what can i do if i want to make use of it as virtual function
//if not please tell me why
virtual void display(void) //to call the recent version of display function we make use of virtual here
{
cout << "base\n";
}
};
class derived : public base
{
public:
int x;
void display(void)
{
cout << "derived\n";
}
};
int main(void)
{
base *p;
base ob1;
derived ob2;
p=&ob2;
p->x=100;//here i want to set 100 to the x of derived class not that x which has been inherited
//But it sets to the x of base class which i dont wanted
p->display();//here we can access the latest version of display function in derived class
return 0;
}
Please, No body ask me why i want to do so.I don't have any intention to do in my real code. i asked for the curiosity.
No, you cannot use virtual for fields, only for methods.
However you can simulate that by creating a function that returns a reference to a field:
class Base
{
private:
int x;
public:
virtual int& X() { return x; }
};
class Derived : public Base
{
private:
int x;
public:
virtual int& X() override { return x; }
};
int main()
{
Derived d;
Base* b = &d;
b->X() = 100; // will set d's x
}
You can't override member variables with the virtual keyword. You could, however, have virtual getters and setters that refer to different member variables in the base and derived classes to achieve a similar effect:
class base {
public:
virtual int getX() {
return x;
}
virtual void setX(int x) {
this->x = x;
}
private:
int x;
}
class derived : public base {
public:
int getX() {
return x;
}
void setX(int x) {
this->x = x;
}
private:
int x;
}
The other answers are totally fine but you can also use the much simpler syntax:
class base {
public:
virtual operator int&() { return x; };
virtual operator int() { return x; };
protected:
int x;
};
if you have a single variable that you'd wish to virtualize in your class.
The second declaration is only to avoid using a reference when you just need the value, while when assigning the reference is automatically chosen for you.
You can override these operators at will from classes derived from base.
class derived : public base {
public:
operator int() override { return x * 5; };
}
Related
I have several c++ classes that have similar behaviours. Moreover most of the class methods can be constructed from few fundamental ones. So I want to define a base class with the derived methods, inherit from the base class and define the remaining methods in the derived classes.
This is my attempt using the CRTP
template <class derived_class> class base_class {
public:
virtual derived_class& operator++ () = 0;
virtual derived_class& fun1() = 0;
derived_class operator++ (int) {
derived_class toreturn(static_cast<derived_class&>(*this));
++*this;
return toreturn;}
derived_class& fun2() {
this->fun1();
return static_cast<derived_class&>(*this);
};
};
class deriv1 : public base_class<deriv1> {
public:
int n;
deriv1():n(0){};
deriv1(deriv1& other):n(other.n){};
deriv1& operator++ () override { ++n; return *this;}
deriv1& fun1() override { n *= n; return *this;}
};
I don't understand why fun2() works but not the postscript increment.
If I call the postscript increment on a derived object I get the error message "Cannot increment value of type 'deriv1'".
The solution is to add a using statement:
class deriv1 : public base_class<deriv1> {
public:
....
using base_class::operator++;
};
The problem is that function resolution is failing. Lets think of a simpler solution to illustrate the problem:
struct Base
{
void f() {}
void f(int) {}
};
struct Derived: public Base
{
void f() {}
};
int main()
{
Derived a;
a.f(1); // This fails as there is no f() that takes an integer
// in Derived. And since the compiler found an f() in
// Derived it stopped looking further up the chain
// for additional matches.
}
This problem is solved in the same way.
struct Derived: public Base
{
using Base::f;
void f() {}
};
I am trying to learn inheritance in c++. I wrote some code to learn virtual functions.
#include <iostream>
using namespace std;
class A {
int a;
public:
A() {}
virtual int get_count() const = 0;
int get_A() { return a; }
};
class B : public A{
public:
int b;
B() {}
B(A& base)
: b(base.get_count()) {}
virtual int get_count() const { return 10; }
};
void func(A& base) {
B derived(base);
cout << derived.b;
}
int main() {
A base;
B derived;
func(derived);
}
When I try to compile I get this error:
test_inheritance_vir.cpp: In function ‘int main()’:
test_inheritance_vir.cpp:32: error: cannot declare variable ‘base’ to be of abstract type ‘A’
test_inheritance_vir.cpp:5: note: because the following virtual functions are pure within ‘A’:
test_inheritance_vir.cpp:10: note: virtual int A::get_count() const
Can you please tell me what I am doing wrong?
You are trying to instantiate an object of type A with A base;. It's not possible as A contains a pure virtual function. (get_count()) Suppose I tried calling base.get_count().
The method virtual int get_count() const = 0; is pure virtual. You can't create an object of a class that is abstract (or in other words - has a pure virtual member). If you want to create an object of A, remove the = 0 and define the function (with an empty body if you need):
virtual int get_count() const{};
should work.
The way you have implemented A (below) causes it to be an abstract base class.
class A
{
int a;
public:
A() {}
virtual int get_count() const = 0; // this is a pure virtual function
int get_A() { return a; }
};
It can only be used as a pointer to a derived class that implements the pure virtual functions:
int main()
{
B derived;
A* pA = new B; // this is okay
delete pA;
return 0;
}
The answers above are the technical reasons why it won't work, but there's a more insidious problem in that the design doesn't make sense. You're creating a B to add some functionality to your A. If you also create a C that extends A, do you really want to turn that into a B?
The classic example of inheritance is animals. Zebra and Giraffe are both Animals, so a class hierarchy would look like this
class Animal
{
stuff
}
class Zebra : public Animal
{
more stuff
}
class Giraffe : public Animal
{
different stuff
}
It doesn't make much sense to turn a Zebra into a Giraffe, though, even though both are animals.
I learnt the work of virtual functions: if the inherited classes inherit a function from the base class, and it is custom for each ones, I can call these functions with pointers that point to the base class, this way:
BaseClass* PointerName = &InheritedClassObject;
But what about variables? I found this question on the site that tells: I can't create virtual variables in C++. My experience proves it: for variables, Visual C++ says: 'virtual' is not allowed.
Then, what is the way to reach the value of a(n inherited) variable that belongs to an inherited class by using base class pointers?
Based off your comment, I think what you are trying to ask if how do child classes access their parent's variables. Consider this example:
class Parent
{
public:
Parent(): x(0) {}
virtual ~Parent() {}
protected:
int x;
};
class Child: public Parent
{
public:
Child(): Parent(), num(0) {}
private:
int num;
};
void Child::foo()
{
num = x; //Gets Parent's x,
}
NB: If you define an x in Child, that masks the x in Parent. So, if you want to get the x in Parent, you would need: Parent::x. To simply get x from a Child c, you use c.x if x is public or use a getter if x is protected or private:
int Child::getNum()
{
return num;
}
You don't. Virtual functions use them, do whatever needs to be done and return result if needed.
You can't use any function, data member of an inherited class if it's casted back to base class. However, you can alter those variables with virtual functions. Example:
#include <iostream>
class BaseClass {
public:
BaseClass() {}
virtual void do_smth() = 0;
private:
};
class InheritedClass: public BaseClass {
public:
InheritedClass(): a(1) {}
virtual void do_smth() { std::cout << ++a << std::endl; }
private:
int a;
};
int main() {
BaseClass* ptr = new InheritedClass();
ptr->do_smth();
return 0;
}
In this piece of code, virtual function did alteration of variable belongs to InheritedClass.
I have the following simple class
class base
{
public:
int x;
base &set(int y)
{
x = y;
return *this;
}
};
and want to create a new one with added functionality, say to print the value x. So I do:
class derived : public base
{
public:
void print()
{
cout << x << endl;
}
};
Now in the main program I want to do something like
D.set(2).print();
for which however the compiler complains that class base has no member named 'print'.
If I try to use covariant return types and write the two classes as
class base
{
public:
int x;
virtual base &set(int y)
{
x = y;
return *this;
}
};
class derived : public base
{
public:
derived &set(int y)
{
x = y;
return *this;
}
void print()
{
cout << x << endl;
}
};
then the statement works just fine, but I was forced to rewrite the exact same function body for 'set' in both classes, even though the only thing that changes is the return type.
If later on I need to change the functionality of base::set then I will have to go through all the derived classes to change the 'set' function... are there any ways to avoid that? Thanks in advance!
Depending on your circumstances, you might be able to use the CRTP:
template <class D>
class base {
D& set(int x) {
…;
return *static_cast<D*>(this);
}
};
class derived : base<derived> { … };
C++ work as you say, and you say in your base class set return base&, so that is what C++ do. But in order to solve this you have many ways.
First you are not forced to make a function virtual to override it in a derived class (note that virtual call are slightly slower than normal calls).
Second you can refer to base class implementation as base::set so the code will be as follow:
class base {
...
base& set( int x ) {...}
};
class derived : public base {
derived& set( int x ) {
return static_cast<derived&>( base::set(x) );
}
};
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Overriding static variables when subclassing
I have a set of classes that are all derived from a base class. Any of these derived classes declare the same static variable. It is however specific to each of the derived classes.
Consider the following code.
class Base {
// TODO: somehow declare a "virtual" static variable here?
bool foo(int y) {
return x > y; // error: ‘x’ was not declared in this scope
}
};
class A : public Base {
static int x;
};
class B : public Base {
static int x;
};
class C : public Base {
static int x;
};
int A::x = 1;
int B::x = 3;
int C::x = 5;
int main() {}
In my base class I wanted to implement some logic, that requires the knowledge of the derived-class-specific x. Any of the derived classes has this variable. Therefore I would like to be able to refer to this variable at base class scope.
This wouldn't be a problem if it were a simple member variable. However, semantically, the variable is indeed not a property of the derived class' instance, but rather of the derived class itself. Therefore it should be a static variable.
UPDATE I need the class hierarchy to preserve its polymorphic nature. That is, all my derived class' instances need to be members of a common base class.
Then however, how can I get my hands on this variable from the base class method?
You can use the Curiously recurring template pattern.
// This is the real base class, preserving the polymorphic structure
class Base
{
};
// This is an intermediate base class to define the static variable
template<class Derived>
class BaseX : public Base
{
// The example function in the original question
bool foo(int y)
{
return x > y;
}
static int x;
};
class Derived1 : public BaseX<Derived1>
{
};
class Derived2 : public BaseX<Derived2>
{
};
Now classes Derived1 and Derived2 will each have a static int x available via the intermediate base class! Also, Derived1 and Derived2 will both share common functionality via the absolute base class Base.
With a virtual getter function
class Base {
public:
bool foo(int y) const{
return getX() > y;
}
virtual int getX() const = 0;
};
class A : public Base {
static const int x;
int getX() const {return x;}
};
class B : public Base {
static const int x;
int getX() const {return x;}
};
class C : public Base {
static const int x;
int getX() const {return x;}
};
int A::x = 1;
int B::x = 3;
int C::x = 5;
int main()
{
C c;
bool b = c.foo(3);
}