C++ Polymorphism and specific methods - c++

Given 3 classes.
class vehicle{
public:
void start();
}
class airplane: public vehicle{
void start();
void setRoute(route r);
void setAltitude(altitude a);
}
class boat: public vehicle{
void start();
void setRoute(route r);
}
Suppose we receive a parameter by command line that let us understand if we will manage an airplane or a boat.
Vehicle* v;
if (parameter == 1) {
v = new airplane();
v->setRoute(r);
v->setALtitude(a);
}
if (parameter != 1) {
v = new boat();
v->setRoute(r);
}
v->start();
Note that different methods are called for boat and airplane before start.
(Start is the only common method)
This code will not work because v doesn't contains methods setRoute/SetALtitude.
What's the correct way of implement this pattern?

You can easily do:
if(parameter != 1)
{
boat *b = new boat;
b->setRoute(r);
v = b;
}
and similar for airplane.

class __declspec(novtable) vehicle{
public:
virtual void start();
virtual void setRoute(route r) = 0;
virtual void setAltitude(altitude a) = 0;
};
class airplane: public vehicle{
virtual void start();
virtual void setRoute(route r);
virtual void setAltitude(altitude a);
};
class boat: public vehicle{
virtual void start();
virtual void setRoute(route r);
virtual void setAltitude(altitude a);
};

Related

Interface to modify an abstract attribute

class AbstractShape;
class RectangularShape : public AbstractShape
{
void setWidth(double v);
void setLength(double v);
};
class CircleShape : public AbstractShape
{
void setRadius(double v);
};
class PolygonalShape : public AbstractShape
{
void addPoint(Point p);
};
class Element
{
protected:
AbstractShape* _shape; //RectangularShape, PolygonalShape or CircleShape
};
I want to create methods in Element to modify the shape _shape of Element ( ie if the shape is Rectangular I have to be able to change the length and the width, otherwise I have to be able to add a Point to the polygonal shape etc ).
For instance I cannot declare a setLength method because setLength as only a meaning if _shape is a RectangularShape. A solution would be to subclass Element in RectangularElement, PolygonalElement, CircularElement but I would like to avoid this solution.
Do you see another way to do that ?
May be you could do something like following :
class Shape
{
// ....
virtual void setWidth(double v) { /* not implemented, throw error ? */ }
virtual void setLength(double v){ /* not implemented, throw error ? */}
virtual void setRadius(double v){ /* not implemented, throw error ? */}
virtual void addPoint(Point p) { /* not implemented, throw error ? */}
//....
};
class RectangularShape : public Shape
{
void setWidth(double v) override ;
void setLength(double v) override;
};
class CircleShape : public Shape
{
void setRadius(double v) override ;
};
class PolygonalShape : public Shape
{
void addPoint(Point p) override;
};
class Element
{
protected:
Shape* _shape; //Any shape
};
Calling meaningless function on a _shape will print a message or assert .
Other than basic fat interface implementation I mentioned, if you don't want to pollute your abstract base class for whatever reason, one possible solution is to use a fat interface on another class and capability queries
class AbstractShape {};
class RectangularShape : public AbstractShape
{
public:
void setWidth(double v);
void setLength(double v);
};
class CircleShape : public AbstractShape
{
public:
void setRadius(double v);
};
class PolygonalShape : public AbstractShape
{
public:
void addPoint(Point p);
};
class Element {
public:
void setWidth(double);
void setLength(double);
void setRadius(double);
void addPoint(Point);
protected:
std::unique_ptr<AbstractShape> shape;
};
And the implementation of setWidth() for example could look like this
void Element::setWidth(double val) {
if (auto rectangle = dynamic_cast<Rectangle*>(this->shape.get())) {
rectangle->setWidth(val);
} else {
throw std::runtime_error{"This type does not implement setWidth()"};
}
}
Also note that you probably want to use a std::unique_ptr and not a raw pointer.

How to resolve the compilation error here - for the observer pattern in C++?

I get the following compilation error here. Why can't the derived class access another derived class method. Although the method is public. I am confused here. Please help.
/export/home/joshis1/Dropbox/Design_Patterns_UML/ObserverPattern/ObserverPatternProgram-build-desktop-Qt_4_8_4_in_PATH__System__Release/../ObserverPatternProgram/UpdateReceiver.h:43:
error: no matching function for call to
‘IPublisher::registerObserver(UpdateReceiver* const)’
I have a publisher class - Interface -
class ISubscriber;
/** Interface **/
class IPublisher
{
public:
virtual void registerObserver(ISubscriber *observer) = 0;
virtual void unregisterObserver(ISubscriber &observer) = 0;
virtual void notifyObserver() = 0;
virtual void setBaudRate(int newBaudRate) = 0;
virtual void setAspectRatio(int newAspectRatio) = 0;
virtual void setVolumeLevel(int newVolume) = 0;
};
//////////////////////////////////////
class ISubscriber;
class UpdateManager: public IPublisher
{
private:
QList<ISubscriber *> subscribers;
int m_baudRate;
int m_aspectRatio;
int m_volumeLevel;
public:
UpdateManager()
{
qDebug()<<"Update Manager --- Server/Subject Initialized";
subscribers.clear();
}
virtual void registerObserver(ISubscriber *observer)
{
subscribers.append(observer);
}
virtual void unregisterObserver(ISubscriber *observer)
{
int index = subscribers.indexOf(observer);
subscribers.removeAt(index);
qWarning()<<"Removed the subscriber Index = "<<index;
}
virtual void setBaudRate(int newBaudRate)
{
m_baudRate = newBaudRate;
notifyObserver();
}
virtual void setAspectRatio(int newAspectRatio)
{
m_aspectRatio = newAspectRatio;
notifyObserver();
}
virtual void setVolumeLevel(int newVolume)
{
m_volumeLevel = newVolume;
notifyObserver();
}
virtual void notifyObserver()
{
#if 0
foreach( ISubscriber observer, subscribers )
{
observer.update(m_baudRate,m_aspectRatio,m_volumeLevel);
}
#endif
}
};
///////////////////
I have the observer --
/** Subscriber Interface **/
class ISubsrciber
{
public:
virtual void update(int baudRate, int AspectRatio, int VolumeLevel) = 0;
};
class UpdateManager;
class ISubsrciber;
#include "IPublisher.h"
class UpdateReceiver: public ISubsrciber
{
private:
int m_baudRate;
int m_aspectRatio;
int m_volumeLevel;
int m_receiverNumber;
public:
// static int updateReceiverTracker;
/** Update Interface of the client **/
void update(int baudRate, int AspectRatio, int VolumeLevel)
{
m_baudRate = baudRate;
m_aspectRatio = AspectRatio;
m_volumeLevel = VolumeLevel;
qDebug()<<"The following client number : "<< m_receiverNumber<< " got the update ";
qDebug()<<"The baudRate is "<<baudRate;
qDebug()<<"The AspectRatio is"<<AspectRatio;
qDebug()<<"The VolumeLevel is"<<VolumeLevel;
}
/** Constructor -- and registers with the Server or Publisher or UpdateManager **/
UpdateReceiver(IPublisher *updateManager)
{
//m_receiverNumber = UpdateReceiver::updateReceiverTracker++;
updateManager->registerObserver(this);
}
};
Regarding the error message you have - you don't have a method (at least in this question) with the signature that your code is apparently looking for. You have:
virtual void registerObserver(ISubscriber *observer)
{
subscribers.append(observer);
}
Your first file describes an ISubscriber but your second file inherits from an ISubsrciber (sic).
You have a typo in your file. Change to ISubscriber and you should have better luck!
here updateManager->registerObserver(this); you are sending it object of UpdateReceiver while it requires ISubscriber.
class
class UpdateReceiver: public ISubsrciber // wrong
class UpdateReceiver: public UpdateManager // right
should inherit UpdateManager not ISubsrciber?
Well I do see a few problems along your solution. Firstly you do have different forward declarations for the type ISubscriber as I stated in my earlier comment. Which results in your program not compiling.
These two wont match for sure.
virtual void registerObserver(ISubscriber *observer) {...}
class UpdateReceiver: public ISubsrciber {...}
^^^^
Secondly your notifyObserver implementation does need some rethinking as well. But since you kind of commented that code out, I'll leave this one up to you. Just remember that you're holding pointers to ISubscriber in your subscriber list when you're trying to iterate/loop over it.
Also the definition of unregisterObserver does not match your declaration.
virtual void unregisterObserver(ISubscriber &observer) = 0;
virtual void unregisterObserver(ISubscriber *observer) {...}
BUT
I would consider some major refactorings for your code. In my opinion you should not mix up the Interface of the Observerpattern with your application logic.
I think your solution should look similar to this. It's not complete and does not use your QT classes tho but you should get the idea. See the demo for a runnable solution.
class ISubscriber {
public:
virtual void update() = 0;
};
class Publisher {
std::vector<ISubscriber*> m_observerList;
public:
virtual void registerSubscriber(ISubscriber& observer);
virtual void unregisterSubscriber(ISubscriber& observer);
virtual void notifySubscribers();
};
class UpdateManager : public Publisher {
int m_baudRate;
int m_aspectRatio;
int m_volumeLevel;
public:
virtual void setBaudRate(int newBaudRate);
virtual void setAspectRatio(int newAspectRatio);
virtual void setVolumeLevel(int newVolume);
int getBaudRate() const;
int getAspectRatio() const;
int getVolumeLevel() const;
};
class UpdateReceiver : public ISubscriber {
UpdateManager& m_manager;
public:
UpdateReceiver(UpdateManager& manager) : m_manager(manager) {
m_manager.registerSubscriber(*this);
}
virtual void update() {
// Process the new values
m_manager.getBaudRate();
m_manager.getAspectRatio();
m_manager.getVolumeLevel();
}
};
DEMO

Inheritance and virtual function can't compile (from Head First DP)

I am new to Design Pattern, and I'm trying the first example of (Head First Design Patterns) but I'm trying to code it in C++. I can't compile my code! I don't know why. Here's my code.
#include <iostream>
using namespace std;
class QuackBehavior
{
public:
virtual void quack();
virtual ~QuackBehavior();
};
class Quack : public QuackBehavior
{
public:
void quack()
{
cout<<"Quacking"<<endl;
}
};
class MuteQuack : public QuackBehavior
{
public:
void quack()
{
cout<<"<<< Silence >>>"<<endl;
}
};
class Squeak : public QuackBehavior
{
public:
void quack()
{
cout<<"Squeak"<<endl;
}
};
class FlyBehavior
{
public:
virtual void fly();
virtual ~FlyBehavior();
};
class FlyWithWings : public FlyBehavior
{
public:
void fly()
{
cout<<"I'm flying"<<endl;
}
};
class FlyNoWay : public FlyBehavior
{
public:
void fly()
{
cout<<"I can't fly"<<endl;
}
};
class Duck
{
public:
FlyBehavior *flyBehavior;
QuackBehavior *quackBehavior;
void display();
void performFly()
{
flyBehavior->fly();
}
void performQuack()
{
quackBehavior->quack();
}
};
class MallardDuck : public Duck
{
public:
MallardDuck()
{
quackBehavior = new Quack();
flyBehavior = new FlyWithWings();
}
};
int main()
{
Duck *mallard = new MallardDuck;
cout<<"Test"<<endl;
mallard->performFly();
// mallard->performQuack();
return 0;
}
Thanks for your help.
You get a compile error because you have not provided default definitions for functions in class QuackBehavior and class FlyBehavior.
Either you could provide default implementation or make the functions pure virtual.
Make the below two changes and your code should compile fine.
class QuackBehavior
{
public:
virtual void quack(){}
virtual ~QuackBehavior(){}
};
class FlyBehavior
{
public:
virtual void fly(){}
virtual ~FlyBehavior(){}
};
OR
class FlyBehavior
{
public:
virtual void fly() = 0;
};
class QuackBehavior
{
public:
virtual void quack() = 0;
};

Visitor Pattern in C++ with multiple visitable parameters

Consider the following hierarchy:
class Base
{
virtual void Method() = 0;
virtual void Accept(Visitor *iVisitor) = 0;
};
class Derived1: public Base
{
virtual void Method(){//impl}
virtual void Accept(Visitor *iVisitor)
{
iVisitor->Visit(this);
}
};
class Derived2: public Base
{
virtual void Method(){//impl}
virtual void Accept(Visitor *iVisitor)
{
iVisitor->Visit(this);
}
};
and the visitor class:
class VisitorInterface
{
virtual void Visit(Derived1 * param);
virtual void Visit(Derived2 * param);
}
class Visitor: public VisitorInterface
{
void Visit(Derived1 * param){}
void Visit(Derived2 * param){}
}
Usually I use the visitor pattern to achieve double dispatching when the overload method depends on the parameter type, but I have only the pointer to the base class.
For example:
void foo(Visitor *visitorPtr, Base * basePtr)
{
basePtr->Accept(visitorPtr);
}
I think this is the only way to achieve double dispatching since the dynamic binding of virtual functions should happen only on the object upon which the method is called and not on its parameters (derived types).
Now I encountered a new situation, where I need a sort of Visit method overloading on multiple parameters. Something like this:
class VisitorInterfaceMultiple
{
virtual void Visit(Derived1 * param1, Derived2 * param2);
virtual void Visit(Derived2 * param1, Derived3 *param2);
}
I cannot use the classical visitor pattern solution because accept method is called on only one of the parameters.
My question is: does exist any similar visitor pattern solution, or something similar, that I could use in this situation? (I need to overload Visit with exact 2 parameters, no more than 2).
I created for you "triple" dispatch pattern: http://ideone.com/FoXNW
It is quite easy.
Main part below:
class Derived1;
class Derived2;
class Visitor;
class Base
{
public:
virtual void Accept(Visitor &iVisitor, Base& param1) = 0;
virtual void Accept(Visitor &iVisitor, Derived1& param2) = 0;
virtual void Accept(Visitor &iVisitor, Derived2& param2) = 0;
};
class Visitor
{
public:
virtual void Visit(Derived1 & param1, Derived1 &param2) { cout << "11\n"; }
virtual void Visit(Derived1 & param1, Derived2 &param2) { cout << "12\n"; }
virtual void Visit(Derived2 & param1, Derived1 &param2) { cout << "21\n"; }
virtual void Visit(Derived2 & param1, Derived2 &param2) { cout << "22\n"; }
};
class Derived1: public Base
{
public:
virtual void Accept(Visitor &iVisitor, Base& param1)
{ param1.Accept(iVisitor, *this); }
virtual void Accept(Visitor &iVisitor, Derived1& param2)
{ iVisitor.Visit(*this, param2); }
virtual void Accept(Visitor &iVisitor, Derived2& param2)
{ iVisitor.Visit(*this, param2); }
};
class Derived2: public Base
{
public:
virtual void Accept(Visitor &iVisitor, Base& param1)
{ param1.Accept(iVisitor, *this); }
virtual void Accept(Visitor &iVisitor, Derived1& param2)
{ iVisitor.Visit(*this, param2); }
virtual void Accept(Visitor &iVisitor, Derived2& param2)
{ iVisitor.Visit(*this, param2); }
};
void Visit(Visitor& visitor, Base& param1, Base& param2)
{
param2.Accept(visitor, param1);
}
Note that implementation of Derived1 and Derived2 is literally identical. You can enclose this in macro if you have more derived.

Implementing 2 abstract classes with a common function?

What happens when a class inherits from multiple abstract classes when 2 or more of them have a function with the same name, return type, and arguments?
Assuming all functions here are virtual
Thanks
class C inherits from A and B at the same time and both A & B have virtual void func(int h);
If this is what you mean,
#include <iostream.h>
class A
{
public:
virtual void a_show()=0;
virtual void show()
{
cout<<"A";
}
};
class B
{
public:
virtual void b_show()=0;
virtual void show()
{
cout<<"B";
}
};
class C : public A, public B
{
virtual void a_show()
{}
virtual void b_show()
{}
};
void main()
{
C s;
s.show();
}
The code gives an error with VC++ like
error C2385: 'C::show' is ambiguous
You need to declare show like this :
#include <iostream.h>
class A
{
public:
virtual void a_show()=0;
virtual void show()
{
cout<<"A";
}
};
class B
{
public:
virtual void b_show()=0;
virtual void show()
{
cout<<"B";
}
};
class C : public A, public B
{
public:
virtual void a_show()
{}
virtual void b_show()
{}
void show()
{
cout<<"C";
}
};
void main()
{
C s;
s.show();
}
This sure will give C
C++ also allows to pick an inherited virtual member function (IVMF) as well, so you don't need to override an IVMF. Borrowing the example from mihsathe, we can do the following:
class C : public A, public B {
public:
virtual void a_show() { }
virtual void b_show() { }
using B::show;
// using A:show; // If you want to use show() from A
};