Relationship needed for interface and class in a class diagramm, C++ - c++

Lets say I have a simple interface and a simple class, implementing that interface
class Inter_simple {
public:
virtual ~Inter_simple(){};
void do_something(Simple_class* cl) = 0;
}
class My_class: public Inter_simple {
public:
void another_function(Simple_class* cl);
void do_something(Simple_class* cl) override;
}
where both of them are using the class Point, which looks like:
class Simple_class{
public:
Simple_class();
~Simple_class();
private:
int some_value;
}
So in the class diagram, My_class is implementing the interface Inter_simple, and has an association to Simple_class (since it knows about that class).
But what about the interface? Do I also have to draw an association line between Inter_simple and Simple_class?

Related

Does it make sense to create a class just to store member variables for subclasses to use?

Currently, I have the following classes where there are 2 different types of solvers for each class that I have. Therefore, I have decided to design my classes as such:
// My two interfaces
class Type1Solver {
virtual void solve() = 0;
}
class Type2Solver {
virtual string solve(string) = 0;
}
// Implementing the interfaces
class XType1 : public Type1Solver {
void solve() override; // Implemented in .cpp
}
class XType2 : public Type2Solver {
string solve(string) override; // Implemented in .cpp
}
// And so on for other class A, B, C ...
The issue is both XType1 and XType2 uses the same variables since they are also X class. In that case what I was thinking of doing is as follows:
class X {
protected:
unordered_map<string, string> some_data;
}
// Doing multiple inheritance so my specific X solvers can
// utilize the protected variables in X in their solve() methods.
class XType1 : public Type1Solver, public X {
void solve() override; // Implemented in .cpp
}
class XType2 : public Type2Solver, public X {
string solve(string) override; // Implemented in .cpp
}
Question is, is this use of multiple inheritance valid? Does it break OOP / SOLID Principles. And alternatively, would it be a better case to make a helper class:
namespace XHelper {
unordered_map<string, string> some_data = ...
}
Which I then import and utilize?

Why does CLion's implement methods shortcut make the method private?

I have the following base class:
class OpCode {
public:
OpCode(const std::shared_ptr<CharacterContext> &characterContext);
virtual void operator()(std::uint_fast32_t argument) = 0;
protected:
std::shared_ptr<CharacterContext> character_context;
};
If I declare a class like this:
class xxx : OpCode {
public:
using OpCode::OpCode;
}
and then do ^I to implement methods and select operator() it creates:
private:
void operator()(std::uint_fast32_t argument) override {
}
Why is it making this private? Shouldn't it be public? I'm wondering if there's some underlying C++ idiom that I don't know.

C++ how to make a class type more general and use it as a kind of interface

I have a class called "OpenGLCamera"
and there is another class that needs a camera so at the moment he has OpenGLCamera (since the class is used as a type)
like so:
void draw(const OpenGLCamera& camera);
But i want it to be more general, so i want it to be
void draw(const SomeCamera& camera);
and this "SomeCamera" should be a pointer/reference or something to OpenGLCamera ofcourse!
I have a class called "Visual_settings".. and i heared i should use inheritance to achieve this... but i dont understand how.. how to do this? in the class Visual_Settings? Make it a base class of... and then??
Visual_Settings.h
#include "OpenGLCamera.h"
class Visual_Settings : public OpenGLCamera
{
};
Thanks in advance
The generic type that you are looking for which is SomeCamera, then OpenGLCamera should derive from this type SomeCamera. If you cannot do that then you need to use Adapter pattern. This would make sense if you have other types of cameras.
class SomeCamera
{
public:
virtual void Dowork()=0;
};
class OpenGLCamera : public SomeCamera
{
public:
virtual void Dowork() override
{
//use camera
}
};
//Approach 2 (bridge pattern): when you cannot modify OpenGLCamera:
//***Use proper constructors/destructors and initialize base class constructors as required
class SomeCamera
{
public:
virtual void Dowork()=0;
};
class OpenGLCamera
{
public:
void OpenGlDowork()
{
//use camera
}
};
class OpenGLCameraAdapter : public SomeCamera, OpenGLCamera
{
public:
virtual void Dowork() override
{
//use camera
OpenGlDowork();
}
};
OpenGLCamera should be inherited from SomeCamera.
SomeCamera should have interface, that will be overriden in OpenGLCamera.

WinRT inheritance and common code

I want to extract common code from a few WinRT components to one base class so I don't need to copy&past it. I have the following base class:
[Windows::Foundation::Metadata::WebHostHidden]
ref class ExpandableView : public Windows::UI::Xaml::DependencyObject
{
public:
static void onIsExpandedChanged(Windows::UI::Xaml::DependencyObject^ object,
Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ arguments);
public:
property bool IsExpanded
{
bool get(){return (bool)GetValue(IsExpandedProperty);}
void set(bool value){SetValue(IsExpandedProperty, value);}
}
static property Windows::UI::Xaml::DependencyProperty^ IsExpandedProperty
{
Windows::UI::Xaml::DependencyProperty^ get(){return _IsExpandedProperty;}
}
protected:
ExpandableView();
virtual void viewExpanded();
virtual void viewCollapsed();
private:
void _expand();
void _collapse();
private:
static Windows::UI::Xaml::DependencyProperty^ _IsExpandedProperty;
};
And I create a few User Controls which should be somehow inherited from this base class. And it is not possible to do it the way I want because winrt class can inherit only one ref class and other should be interfaces. But I need this very class which has dependency property which has some logic when it is set and I don't want to copy&past this property across all my classes.
So the question is: how to achieve it with WinRT?
Have you tried using a template and inheritance of the specific class needed:
template<typename BaseClass>
ref class ExpandableView : public BaseClass;
Now the subclasses reusing ExpandableView can inherit whatever they need, not only Windows::UI::Xaml::DependencyObject.

In C++, how can I create two interfaces for a class?

For example, when creating a class library, I would like to specify an internal API and a public API for each classes, so I can hide some details from the user. The internal API would be used by other classes in the library, and the public API would be used by the library user.
Is it possible?
In C++, interface could mean many things. It could mean pure virtual functions that you implement in the derived classes, as in the following example,
class Interface
{
public:
virtual void f() = 0 ;
};
class Implementation : public Interface
{
public:
virtual void f() {}
};
--
Or it could mean just public functions in your class:
class A
{
public:
void f() {} //public function - an interface that the outside world can
//use to talk to your class.
};
You can use either of these and can make use of access-specifiers ( public, protected, private) to make your interfaces public or internal/private!
Kind of.
Some libraries use friend classes/functions for this. Each class declares other classes as friends if they need access to more than the "public" interface:
class Car {
friend class Mechanic;
private:
Engine engine;
};
class Mechanic {
// something involving Car::engine...
};
It's not very pretty, but it works.
Another approach that might work for you is the pimpl (pointer-to-implementation) idiom:
class CarImpl; // declaration only
class Car {
private:
CarImpl *impl;
public:
CarImpl *getImpl(); // doesn't strictly belong in the pimpl pattern
// methods that simply call the corresponding methods on impl
};
The internal interface can be accessed through a getImpl() call. You would put the CarImpl declaration in a header file that is clearly marked as internal, so clients won't access it. For example, you could put such headers in a subdirectory called internal.
The obvious drawback is that the Car class has a bunch of trivial methods that you have to implement.
A third approach, that I do not recommend, is inheritance:
class Car {
public:
virtual void start() = 0;
static Car *create();
};
And in an internal header:
class CarImpl : public Car {
public:
virtual void start();
};
The Car class only exposes the public interface; to get access to the internal interface, internal code needs to do a downcast to CarImpl. This is ugly.
You can use many tricks to grant friendship or an "extended" interface to a given few, however it is soon cumbersome.
The simplest way to separate the external interface from the internal interface... is to have two interfaces, thus two classes.
If you take a peek at the set of Design Patterns proposed by the GoF, you'll notice the Proxy pattern.
You can use this by not exposing the class to the exterior of your library, but instead exposing a Proxy, in which you wrap the class, and which only exposes a subset of its interface.
class MyClass
{
public:
void foo();
void bar();
void printDebugInfo();
void test();
};
class MyClassProxy
{
public:
MyClassProxy(std::unique_ptr<MyClass> ptr): _ptr(ptr) {}
void foo() { _ptr->foo(); }
void bar() { _ptr->bar(); }
private:
std::unique_ptr<MyClass> _ptr;
};
I personally find this design rather clean. No down-casting, No subtle inheritance trick, No friendship list longer than my arm.
I'm not quite sure what you're asking, but if you have an abstract class defined:
class Loggable { ... };
You can inherit from it privately, like this:
class User : private Loggable { ... };
The class User now has the members of Loggable, but they are private.
Please see the C++ FAQ lite.
There is a number of ways to approach this. One is runtime polymorphism:
struct widget {
virtual debug_info diagnose() = 0;
virtual void draw() = 0;
};
struct window {
virtual void draw() = 0;
};
struct view : public window, public widget {
void draw();
debug_info diagnose(); // internal
};
Or with compile-time polymorphism:
struct view {
void draw();
debug_info diagnose(); // internal
};
template<class W>
void do_it(W window)
{
widget.draw();
}
template<class W>
void diagnose_it(W widget)
{
debug_info d = widget.diagnose();
}
Another approach is to expose private members to specific functions or classes:
struct widget {
virtual void draw() = 0;
};
struct view : public widget {
friend void diagnose_widget(widget w);
void draw();
private:
debug_info diagnose();
};
// internal
debug_info diagnose_widget(widget w)
{
debug_info d = w.diagnose();
}
A C++ class has 3 levels of protection: public, protected and private. Public things are accessible to everybody, protected only to descendant -- and then for themselves and not for other descendants --, private for the class and its friend.
Thus friendship is the only way to grant more than public access to a class/function which isn't a descendant, and it grants full access, which isn't always convenient.
An heavy solution which I've used with success was to write a wrapper which was a friend of the main class, and then provided additional access to its own friends (which were the only one able to construct the wrapper). I'm not really recommending it, it is tedious, but it could be useful if you have such a need.
class Main {
public:
...
private: // but wrapped
void foo();
protected:
...
private: // and not wrapped
void bar();
};
class Wrapper {
pubic:
void foo() { wrapped.foo(); }
private:
Wrapper(Main& m) : wrapped(ma) {}
Main& wrapped;
friend void qux(Main&);
};
void qux(Main& m) {
Wrapper mwrapped(m)
mwrapped.foo();
// still can't access bar
}