Hello, I am attempting to make a Duck interface which can produce different types of ducks. I was hoping you could look over my code, and point out possible issues, or suggest alternatives to better design. Sorry, but I don't have any specific question. This is the 1st c++ program I've made in a while and I'm trying to figure smart pointers out.
Here is my main Function
Code:
#include <iostream>#include "Duck.h"
#include <memory>
using std::cout;
std::unique_ptr < Duck > createDuck();
void operateDuck(Duck &);
int main()
{
auto duck = createDuck();
operateDuck(*duck);
//duck = createDuck();
//operateDuck(duck.get());
return 0;
}
std::unique_ptr < Duck > createDuck()
{
int op;
cout << "1 for mallard, 2 for model.." << endl;
cin >> op;
switch (op) {
case 1:
cout << "Creating Mallard..." << endl;
return std::make_unique < MallardDuck > ();
break;
case 2:
cout << "Creating Model..." << endl;
return std::make_unique < ModelDuck > ();
break;
default:
cout << "Continuing.." << endl;
}
}
void operateDuck(Duck & duck)
{
int menuChoice;
cout <<
"Enter 1 to fly, 2 to flynomore, 3 to flyhigh, and 4 to quit, 5 to make new duck"
<< endl;
cin >> menuChoice;
while (menuChoice < 4) {
cout << "Enter your choice again: " << endl;
cin >> menuChoice;
switch (menuChoice) {
case 1:
duck.performFly();
break;
case 2:
duck.setFlyBehaviour(std::make_unique < FlyNoWay > ());
break;
case 3:
duck.setFlyBehaviour(std::make_unique < FlyWithWings > ());
break;
case 4:
cout << "Relinquishing command... " << endl;
break;
case 5:
//recursively create new ducks until system failure
break;
default:
cout << "No input..." << endl;
}
}
cout << "Leaving OperateDuck() " << endl;
}
And here is my Duck.h
Code:
//// Created by playerone on 2019-10-24.
//
#ifndef OODESIGNEXAMPLES_DUCK_H
#define OODESIGNEXAMPLES_DUCK_H
#include "QuackBehaviour.h"
#include "FlyBehaviour.h"
#include <iostream>
#include <memory>
/*
* class C {
C(const C&) = default; // Copy constructor
C(C&&) = default; // Move constructor
C& operator=(const C&) = default; // Copy assignment operator
C& operator=(C&&) = default; // Move assignment operator
virtual ~C() { } // Destructor
};
*
*/
class Duck {
public:
Duck() = default;
Duck(const Duck & other) = delete; // we don't want to copy other duck's unique_ptr's
Duck & operator=(const Duck &) = delete; //ducks are bred, not cloned
Duck & operator=(Duck &&) = default;
Duck(Duck && other) noexcept = default;
virtual ~ Duck() = default; //{std::cout << "Duck is being destroyed.." << endl;};
virtual void display() = 0;
void performFly() {
flyBehaviour->fly();
} void performQuack() {
quackBehaviour->quack();
}
void swim() const {
std::cout << "All ducks float, and are lighter than wood!" << endl;
} void setFlyBehaviour(std::unique_ptr < FlyBehaviour > fb) {
flyBehaviour = std::move(fb);
}
void setQuackBehaviour(std::unique_ptr < QuackBehaviour > qb) {
quackBehaviour = std::move(qb);
}
private:
std::unique_ptr < FlyBehaviour > flyBehaviour = nullptr;
std::unique_ptr < QuackBehaviour > quackBehaviour = nullptr;
};
class MallardDuck:public Duck {
public:
MallardDuck() {
//flyBehaviour = std::unique_ptr<FlyWithWings>(); //causes seg fault when flyBehaviour is protected (cant use assignment operator with unique_ptr??
setFlyBehaviour(std::make_unique < FlyWithWings > ());
setQuackBehaviour(std::make_unique < Quack > ());
};
// ~MallardDuck() override { std::cout << "This mallard is being destroyed" << endl; }
void display() override {
std::cout << "Look at me I'm a mallard" << endl;
}
};
class ModelDuck:public Duck {
public:
ModelDuck() {
setFlyBehaviour(std::make_unique < FlyNoWay > ());
setQuackBehaviour(std::make_unique < Squeak > ());
};
// ~ModelDuck() override { std::cout << "This model is being destroyed" << endl; }
void display() override {
std::cout << "This model duck looks beautiful today!" << endl;
}
};
#endif //OODESIGNEXAMPLES_DUCK_H
The behaviour classes were trivial.
There are some weird things I don't understand, like when I use on the move constructor and assignment operator, the program still works? Other than that, I need to have good principles for an upcoming project in my software engineering course. This is what I was aiming for:
https://i.imgur.com/tNp74ay.png