Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For example let's say I have this class:
class base{
public:
update();
};
and I have a lot of classes that are inherit from this one:
class A : public base
{
//<...>
}
class B : public base
{
//<...>
}
and so i want to know - can i somehow call the function base::update(); in all the classes without writing it all out (A::update(); B::update();) etc.
Like calling one function that would do the update(); in all the classes that inherit it from the base class.
Thanks!
Edit:
What I'm doing is changing my entity system into component based one and I want to know whether there's an easier way to call the (let's say) update(); function, that is inherited, in all the members. Instead of doing enemy01.update(); enemy02.update(); etc. to just write down a single update(); that'd work on all classes that have it(inherited it), sorta like a message to all of them to call the function.
A virtual function?
class base{
public:
virtual void update();
};
And then this function:
void update_class(base &b) {
return b.update();
}
You can call update_class on any class that inherits base.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm fairly confident in C and Java and just started using C++, still getting used to the basics about best practices including data abstraction (which, as far as I understand, means proper header usage).
I'd like to make a project that solves different types of problems that all imply reading data from a file, solving the problem and writing the result to a new file, so I figured I'd use something similar to an interface in Java.
My header looks like this:
#include <iostream>
using namespace std;
class Problem {
public:
/*
* Read data from file.
*/
virtual bool read(string filename) = 0;
/*
* Solve problem.
*/
virtual void solve() = 0;
/*
* Write solution to file.
*/
virtual bool write(string filename) = 0;
};
class Brothers : public Problem {};
I'm trying to implement the first function in my Brothers.cpp source file like this:
bool Brothers::read(string filename) override {...}
Which, save for the Brothers:: part, is how I would normally implement this function if I had just source files.
I'm getting the errors Function 'read' was not declared in class 'Brothers' and Function doesn't override any base member functions, and I would like to understand why this approach isn't working and what I should do instead.
Even though you are overriding the base class declaration, unlike in java, you will still need to declare the function in the derived class's header file. The compiler only looks in the class associated with that function, so when you say that there is a function read in Brother and it can't see that there is one explicitly, you get an error.
Another thing is that when you use interfaces or other abstract classes, everything that is declared as pure virtual has to be defined before there is a class that is not pure virtual, so in this example:
class A
{
virtual void foo() = 0;
}
class B : public A
{
virtual int bar()
{
return 0;
}
}
class C : public B
{
virtual void foo()
{
// Do something
}
}
Class A is abstract because it has a pure virtual function, but so is B because it still has no definition for foo(). Until there is a definition for it, there can be no non-abstract classes.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have function get_time() in a class which return a private member.
How can I use the object which called this function in the implementation of the function.
For example in case comp object which have a member call name call get_time function (comp.get_time()):
I want to be able to get the comp.name in the implementation of the det_time_function. How can I?
e.g
class comp
{
public:
string name;
}
class calc : public comp
{
private:
int time;
public:
int get_time(){
///here I want to get the name of the object which call the calc
///should I use this.name?
}
}
calc calc_obj;
calc.get_time();
Derived classes can not access the private members of their base classes. However, if you use protected declaration instead of private, then you could do that. But another way (which you should practice and learn) is by providing a getter() function in your parent class which return its object's name, then you can use that getter to call it from the child class and get the private member of the parent class; name in your case.
A basic getter() looks something like this:
YourVariableTypeHere get_VariableName()
{
return this->VariableName;
}
int get_time(){
///here I want to get the name of the object which call the calc
///should I use this.name?
}
No. To fetch the base class public data attribute, you would use
comp::name
Note, however, that in general, public data attributes should be avoided (because this disables encapsulation).
Also, in this question, it appears to be possible that some other object or function can call the derived class method, not simply the base class. So perhaps your question is non-sequitor, or perhaps misleading.
One way to provide a 'label' to handle both cases is to include a string in the method.
calc::get_time("caller-name");
Now the name is provided to the method, and the method need not fetch it from the base class, nor from which ever invoking object called get_time().
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Generally in C++, we see same name functions with same arguments in different classes along with virtual keyword. Is it an example of polymorphism? If it is, how and is there any other way to understand polymorphism in c++??
Suppose that you have the following scheme:
class Base {
public:
virtual int foo() = 0;
};
class Derived1 : public Base {
public:
virtual int foo() { return 1; }
};
class Derived2 : public Base {
public:
virtual int foo() { return 2; }
};
Lets say now, that you want to store objects of Derived1 and Derived2 in a container, lets say a std::vector and every time you iterate through the vector to call for each one of its objects function foo. Normally, you'd need two vectors, one for objects of class Derived1 and another one for objects of class Derived2.
Look now how polymorphism comes to our rescue. We can store the addresses of objects Derived1 and Derived2 as pointers to Base objects, and then store these pointers in a std::vector<Base*> v. Now every time we iterate the vector of pointers and call member function foo (e.g., for(auto base : v) base->foo()) the right function foo is called for each one of the objects. This is one of the many aspects of polymorphism which is called upcasting
I hope that the above example of runtime polymorphism gave you a clue, as for how virtual functions and function overriding are related to inheritance and polymorphism.
Update:
Templates are a form of compile time polymorphism take a look at this SO question what is the difference between templates and polymorphism.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I specify the parameter of a method as any class that implements a specific interface ?
This is rather common in objective c.
There are no interfaces in standard C++, but we can simulate them pretty easily:
class IComparable
{
protected:
IComparable() {};
public:
virtual ~IComparable() = 0 {};
virtual int Compare(const IComparable& other) const = 0;
};
There is no way we can instantiate this class. It is effectively an interface. You can then derive concrete classes from this.
If you have an "interface" or abstract base class called Base, then a function which can accept any object implementing that interface would look like:
void fn(Base& obj) {
/*use Base functions on obj...*/
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know the virtual keyword does not need to be resused in a derived class member function declaration if it overrides a virtual base function but is it good practice to do so to make it clear that it is virtual? Also, what about the presence of the const keyword in declaration and/or definition? I think Alexandrescu mentions something about this but I can't recall what it was?
Your question seems very confused. virtual is optional when overriding a base-class method. const is never optional if you need it. This does not do what you think it does:
struct A
{
virtual void Func() const;
};
struct B : public A
{
virtual void Func();
};
The struct B has two functions named Func. One of them will be called when the object it is called on is const, and the other will be called when it is not const. Nothing in this code has been overridden; these are two separate virtual functions.
You cannot just ignore the const and expect everything to work out fine.
Indeed, this example also shows why you should use virtual when you're overriding in derived classes. In this case, it's fairly obvious that you intended to override a base class function, but you got the function signature wrong. Without the virtual there, there would not be an immediate indication that you intended to override something.
It's not a huge help, but it's something.
C++11 provides a better solution (in that it actually solves the problem) with the override pseudo-keyword.
struct A
{
virtual void Func() const;
};
struct B : public A
{
virtual void Func() override; //Gives a compiler error, since it is not overriding a base class function.
};