How to properly use interfaces and data abstraction together in C++? [closed] - c++

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.

Related

C++ standard way to create "Abstract Class" (Pure Virtual Class) [duplicate]

This question already has answers here:
C++ abstract class without pure virtual functions?
(3 answers)
Closed 7 years ago.
I will start with what's most of us already know:
If I want my class to be abstract, I must define at least one of its methods as "pure virtual", for example, here, the method someFunction() is defined as "pure virtual", as it is defined with the virtual keyword and it is assigned with 0:
class SomeClass {
virtual void someFunction() = 0;
};
My question is, when I want an "abstract class", i.e. a class which cannot be instantiated (like "pure virtual" class), but I want to implement all its methods. Is there any standard way to do it?
My current workaround is ugly - I just define another dummy pure virtual method:
class UglyWorkaround {
public:
virtual void doAction1();
virtual void doAction2();
// My ugly workaround for making the class abstract - defining a dummy method
virtual void thisIsADummyMethod() = 0;
};
This is very bad, as any deriving non-abstract class will have to implement it
Is there a more standard/popular way to define and implement such a class?
I want to clarify - I don't want another ugly workaround - I ask whether there is any standard way that is commonly used. The objective is to make the code readable for other programmers, so they immediately understand that the class is abstract
Define the constructor protected

How to call a function in all inheritant classes? [closed]

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.

Inheritance, pure virtual functions and non-inline member function definitions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have boiled down my issue to the following code all in a single *.cpp file:
class A {
public:
A() {};
int PerformAction() {
return Action();
}
protected:
virtual int Action();
}
class B: public A {
protected:
int Action();
}
int B::Action() {
return 4;
}
int main() {
B newB;
newB.PerformAction();
};
The previous code throws an Unresolved external symbol error on B::Action(). Changing a::Action()'s definition to:
virtual int Action() = 0;
yields instead the Cannot instantiate abstract class compiler error where it is used in the main function. All of the answers I have seen relate to having the code spread across multiple files, but this is occurring all within one source file. I imagine it is related to B's implementation of Action() being outside the class definition of B but can't see any reason why it should cause any error.
Your error messages, taken together, indicate a signature mismatch between A::Action and B::Action, such that B::Action does not become an overrider. The signatures must match perfectly (including cv-qualification of this), except that return type covariance is allowed.
And B::Action must be virtual. It will be implicitly if the signature matches, unless it is a template. Templates can't override.
If you have a C++11 compiler, I suggest using the override keyword, to make signature mismatch a compile error.
The previous code throws an Unresolved external symbol error on B::Action().
Indeed, all non-pure virtual functions must be defined, so that their address can be stored in the virtual function table for the class.
yields instead the Cannot instantiate abstract class compiler error
No it doesn't: http://ideone.com/IS7PJj
You'd get that error if you tried to directly instantiate A, or a subclass which didn't override the pure virtual function. B does override it, and so is not abstract.
Possibly, your real class B has an incorrect signature for Action, so it doesn't actually override the one declared in A. In C++11, you can add the override specifier to the one in B, to get a more helpful error message in that case.
I imagine it is related to B's implementation of Action() being outside the class definition of B
No, that shouldn't be an issue, as long as there's exactly one definition of the function.

What is a good convention (or requirement) for the location of the const and virtual keywords in C++? [closed]

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.
};

How to call overriden methods in all derived classes [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to force child same virtual function call its parent virtual function first
I have a class hierarchy where each derived class overrides a given virtual function and starts its implementation by calling the one in its parent class. The goal is to have each of the derived implementation to be executed, but I do not like the way I do it.
For example, I have this class:
class base
{
public:
void do_stuff() { do_something(); }
virtual void do_something() { }
};
Then I derive this class on several levels:
class derived_10:
public derived_9 // which inherit from derived_8 and so on until derived_0
// which inherit from base
{
public:
virtual void do_something()
{
// this will also call derived_8::do_something() and so on
// until base::do_something()
derived_9::do_something();
// then, some stuff
}
};
I'm looking for a solution that will make sure that all derived_x::do_something() will be called in sequence when base::do_stuff() is called, without having to expect the derived classes to do this themselves. Do you have an idea of the best way to get this behavior ?
I've already asked a very similar question before: Calling overriden class methods as a chain in C++
The answer I've accepted pointed at your own solution. I can give you an idea about an alternative though. Constructors and destructors already have this behavior in C++, you might want to consider restructuring your code, so that the work is done during the construction or the destruction of an object that belongs to a class in a hierarchy. I'm not sure you'll be able to make this worth the effort though. On the other hand, you never know what you can get out of some template metaprogramming + some preprocessor magic.