Last active
September 18, 2015 09:13
-
-
Save jpartogi/9acda44e1595ba5e69b4 to your computer and use it in GitHub Desktop.
Bad OO Design in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
class BankAccount { | |
private: | |
string m_accountNumber; | |
double m_accountBalance; | |
static const double interestRate = 5.0; | |
public: | |
BankAccount(string accountNumber){ | |
m_accountNumber = accountNumber; | |
} | |
double calculateInterest(){ | |
return (interestRate * m_accountBalance)/100; | |
} | |
// Save money into current account business logic | |
double save(double amount){ | |
return m_accountBalance + amount; | |
} | |
// Withdraw money from current account business logic | |
double withdraw(double amount){ | |
return m_accountBalance - amount; | |
} | |
double accountBalance() { | |
return m_accountBalance; | |
} | |
string accountNumber(){ | |
return m_accountNumber; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <math.h> | |
using namespace std; | |
class Shape{ | |
private: | |
static const int CIRCLE = 1; | |
static const int RECTANGLE = 2; | |
int TYPE; | |
double radius, width, length; | |
public: | |
double area(){ | |
switch (TYPE) { | |
case CIRCLE: | |
return M_PI * radius; | |
case RECTANGLE: | |
return width * length; | |
default: | |
return 0; | |
} | |
} | |
void createCircle(double radius){ | |
this->radius = radius; | |
this->TYPE = CIRCLE; | |
} | |
void createRectangle(double length, double width) { | |
this->width = width; | |
this->length = length; | |
this->TYPE = RECTANGLE; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <math.h> | |
using namespace std; | |
class Shape { | |
public: | |
virtual double area() = 0; | |
}; | |
class Rectangle : public Shape { | |
private: | |
double width, length; | |
public: | |
Rectangle(double width, double length) { | |
this->width = width; | |
this->length = length; | |
} | |
virtual double area(){ | |
return this->width * this->length; | |
} | |
}; | |
class Square : public Rectangle { | |
public: | |
double edge; | |
Square(double edge) : Rectangle(edge, edge) { | |
this->edge = edge; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <stdexcept> | |
using namespace std; | |
class Vehicle { | |
public: | |
virtual void fly() = 0; | |
virtual void drive() = 0; | |
}; | |
class Car : public Vehicle { | |
public: | |
virtual void drive() { | |
// Implement drive here | |
} | |
virtual void fly () { | |
throw invalid_argument("Can not fly"); | |
} | |
}; | |
class Plane : public Vehicle { | |
public: | |
virtual void drive() { | |
throw invalid_argument("Not a good idea"); | |
} | |
virtual void fly() { | |
// Implement fly here | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
class IOutputter { | |
public: | |
virtual void print(string output) = 0; | |
}; | |
class ConsoleOutputter : public IOutputter { | |
public: | |
virtual void print(string output) { | |
cout << output; | |
} | |
}; | |
class MockOutputter : public IOutputter { | |
public: | |
virtual void print(string output) { | |
// do nothing | |
} | |
}; | |
class CommandInvoker { | |
public: | |
void invoke(ConsoleOutputter *outputter) { | |
outputter->print("Hello World"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment