Skip to content

Instantly share code, notes, and snippets.

@jpartogi
Last active September 18, 2015 09:13
Show Gist options
  • Save jpartogi/9acda44e1595ba5e69b4 to your computer and use it in GitHub Desktop.
Save jpartogi/9acda44e1595ba5e69b4 to your computer and use it in GitHub Desktop.
Bad OO Design in C++
#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;
}
};
#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;
}
};
#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;
}
};
#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
}
};
#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