functions and multiple class hierarchy - c++

trying to understand methods and virtual function lets say i have 3 classes (the ones below)
class abstruct {void go()};
class animal:public abstruct {
public:
char* name;
void go(){}
};
class bird:public abstruct {
public:
char* name;
void fly(){}
};
class animalbird:public animal,public bird {
void go(){}
};
my question is how can go function from animal be accessed in the class animalbird?? i tried just to write void go(){} but it seems im wrong.what am i doing wrong?

void animalbird::go() {
animal::go();
}

Related

How to properly override function with different arguments in derived class?

I'm making a simple MVC example for c++ classes at my uni. First, look at the code:
The executor.h part:
class IExecutor {
IParams params;
public:
virtual void initialize(IParams iParams);
virtual void execute();
};
class QEExec : public IExecutor {
public:
void initialize(QEParams iParams) override;
void execute() override;
};
And now params.h part:
class IParams {
};
class QEParams : public IParams {
public:
int a;
int b;
int c;
};
The problem is that I want to create void initialize(QEParams iParams) function for QEExec and pass QEParams to it in order to have access to a, b, and c parameters (I'll need that later) but I can't do so because of virtual void initialize(IParams). I thought that if QEParams is derives from IParams I will be able to do so, but I can't access parameters that I mentioned earlier. How to make it work so that I'll be able to access a, b and c parameters in initialize function?
EDIT: I'll put a photo of how it should look like:
https://i.stack.imgur.com/KWaSQ.jpg
Interface doesn't have any fields
Interface has only pure virtual methods
Name initialize of IExecutor indicates some misunderstanding. Looks like it suppose to be called once at the begging during construction time. It should be hidden in step where some factory creates object implementing IExecutor
So basically I'm suspecting you need more something like this:
class IExecutor
{
public:
virutal ~IExecutor() {}
virtual void execute() = 0;
};
struct QEParams {
int a;
int b;
int c;
};
class QEExec: public IExecutor
{
public:
QEExec(int b, int c) ....
void initialie(); // second step init
void execute() override;
};
class CAExec: public SomeOtherBaseClass, public IExecutor
{
public:
CAExec(int a, int c) ....
void execute() override;
};
std::unique_ptr<IExecutor> executorFactory(const QEParams& params)
{
if (params.a < 0) {
auto result = std::make_unique<QEExec>(params.b, params.c);
result->initialie();
return result;
}
return std::make_unique<CAExec>(params.a, params.c);
}
Usually factory parameters are structural data and extra abstraction is obsolete.
If different kind of arguments are needed to create alternative version of IExecutor you just provide different factory function (possibly overload):
std::unique_ptr<IExecutor> executorFactory(const std::string& fileName)
{
....
}
It sounds like you're using OOP incorrectly.
Since QEExec is a IExecutor (it inherits), and it can initialize just like IExecutor can, ideally, both of those initialize's will be doing similar things to their IParams objects. If so, then the one who should be acting on a, b and c should be QEParams, not QEExec.
You could do this with polymorphism like:
class IParams {
virtual void init_logic() { }
};
class QEParams : public IParams {
public:
void init_logic() { /* Do something with a/b/c */ }
private:
int a;
int b;
int c;
};
And then...
class IExecutor {
IParams params;
public:
virtual void initialize(IParams *iParams);
virtual void execute();
};
class QEExec : public IExecutor {
public:
//Will call the QEParams init_logic when passed a QEParams pointer
void initialize(IParams *iParams) { iParams->init_logic(); }
void execute() override;
};

pure virtual function needs to take different parameters

I have the following Base Class.
class Furniture
{
public:
virtual void Collapse() = 0;
};
With derived classes:
class Table : public Furniture
{
public:
void Collapse()
{
Save(my_file);
}
protected:
void Save(char* filepath);
private:
char* my_file;
};
class Armoire : public Furniture
{
public:
void Collapse()
{
Save(my_file);
}
protected:
void Save(char* filepath);
private:
char* my_file;
};
class Chair : public Furniture
{
public:
void Collapse()
{
Save(); // note - no filepath
}
protected:
void Save();
};
After reading the comments, I have edited this question somewhat, to describe more accurately the problem in hand.
My problem is that all but one of the classes derived from Furniture define the Save() function with a parameter, like Table and Armoire are doing. It's only the Chair class that defines a Save() function with no parameter.
I want to somehow move the declaration of the Save() function - to be part of an interface, and have the derived classes provide the implementation. But that one class Chair that does not require a parameter means that I can't do this.
What is the best way to design this?
Use a default parameter in the base class:
virtual void Collapse( int seconds = 0 ) = 0;

Restrict visibility of friend classes in C++ [duplicate]

I want to make a class A friend class of class B. I want to do this as these interact very much and A needs to change internals of class B (which I dont want to expose using public). But I want to make sure it has access to only a few selected functions not all the functions.
Example:
class A
{
};
class B
{
private:
void setState();
void setFlags();
friend class A
};
I want A to be able to access setState but not setFlags... Is there a design pattern or a nice way of doing this or am I left with giving full access or no access at all in this case.
Thanks
It depends on what you mean by "a nice way" :) At comp.lang.c++.moderated we had the same question a while ago. You may see the discussion it generated there.
IIRC, we ended up using the "friend of a nested key" approach. Applied to your example, this would yield:
class A
{
};
class B
{
public:
class Key{
friend class A;
Key();
};
void setFlags(Key){setFlags();}
private:
void setState();
void setFlags();
};
The idea is that the public setFlags() must be called with a "Key", and only friends of Key can create one, as its ctor is private.
One approach is through explicit interfaces, because the implementor of an interface can select who they give them to:
class NearlyPrivateInterface {
public:
virtual void setState() = 0;
virtual void setFlags() = 0;
};
class A {
public:
void attach(NearlyPrivateInterface* instanceOfB);
};
class B: private NearlyPrivateInterface {
public:
void attach(A& a) { a.attach(this); }
};
You can do following thing..
class A{
};
class B{
private:
void setFlags();
protected:
void setState();
};
class RestrictedB :public B{
friend class A;
};

Virtual function call

Here is my hierarchic of classes.
I have declare following abstract interface class, which have just one function:
class IAuthenticator
{
public:
virtual void CreateJson() = 0;
};
After I have created on more class 'UIData' and inherits it from interface class, in this case:
class UIData : public IAuthenticator
{
protected:
UIData() : mWindowHandle(0)
{ /* Constructor do nothing. **/ }
private:
integer mWindowHandle;
public:
void CreateJson()
{
std::cout<<"UIData::CreateJson\n";
}
};
I have one more class which inherits from UIData
class AuthenticateIn : public UIData
{
private:
string mOrigin;
string mLogoURL;
string mUserID;
public:
void CreateJson()
{
std::cout<<"AuthenticateIn::CreateJson\n";
}
};
Question
In my main function I have write code like this.
int main()
{
AuthenticateIn* ai = new AuthenticateIn();
ai->CreateJson();
}
When I call CreateJson() function I see log "AuthenticateIn::CreateJson". I want to find a way to call CreateJson() and it will be called for all base classes.
I know that I can do that calling this->UIData::CreateJson() from AuthenticateIn class CreateJson function, but is there any other way to do that, some automatic way ? Thanks !!
is there any other way to do that, some automatic way
No, there isn't. You have to call the base class's implementation from the derived class. The compiler won't do this automatically since it doesn't know whether you actually want this.
You have to call the base class function in the derived class sort of like this:
void CreateJson() {
UIData::CreateJSon();
}
etc
No, there is no such way. If you want to call virtual function from base class you should do this directly.
You may not be able to force a call to a virtual base class, but you can use indirection to simulate the behaviour.
typedef int integer;
#include <iostream>
#include <string>
using std::string;
using std::cout;
class IAuthenticator
{
public:
virtual void CreateJson() = 0;
};
class UIData : public IAuthenticator
{
protected:
UIData() : mWindowHandle(0)
{ /* Constructor do nothing. **/ }
private:
integer mWindowHandle;
virtual void CreateJsonPrivate() = 0;
public:
void CreateJson()
{
CreateJsonPrivate();
std::cout<<"UIData::CreateJson\n";
}
};
class AuthenticateIn : public UIData
{
private:
string mOrigin;
string mLogoURL;
string mUserID;
virtual void CreateJsonPrivate()
{
std::cout<<"AuthenticateIn::CreateJson\n";
}
};
int main()
{
AuthenticateIn* ai = new AuthenticateIn();
ai->CreateJson();
}
Output:
AuthenticateIn::CreateJson
UIData::CreateJson

friend class with limited access

I want to make a class A friend class of class B. I want to do this as these interact very much and A needs to change internals of class B (which I dont want to expose using public). But I want to make sure it has access to only a few selected functions not all the functions.
Example:
class A
{
};
class B
{
private:
void setState();
void setFlags();
friend class A
};
I want A to be able to access setState but not setFlags... Is there a design pattern or a nice way of doing this or am I left with giving full access or no access at all in this case.
Thanks
It depends on what you mean by "a nice way" :) At comp.lang.c++.moderated we had the same question a while ago. You may see the discussion it generated there.
IIRC, we ended up using the "friend of a nested key" approach. Applied to your example, this would yield:
class A
{
};
class B
{
public:
class Key{
friend class A;
Key();
};
void setFlags(Key){setFlags();}
private:
void setState();
void setFlags();
};
The idea is that the public setFlags() must be called with a "Key", and only friends of Key can create one, as its ctor is private.
One approach is through explicit interfaces, because the implementor of an interface can select who they give them to:
class NearlyPrivateInterface {
public:
virtual void setState() = 0;
virtual void setFlags() = 0;
};
class A {
public:
void attach(NearlyPrivateInterface* instanceOfB);
};
class B: private NearlyPrivateInterface {
public:
void attach(A& a) { a.attach(this); }
};
You can do following thing..
class A{
};
class B{
private:
void setFlags();
protected:
void setState();
};
class RestrictedB :public B{
friend class A;
};