Skip to content

Instantly share code, notes, and snippets.

@sunny1304
Created March 1, 2014 15:23
Show Gist options
  • Save sunny1304/9291387 to your computer and use it in GitHub Desktop.
Save sunny1304/9291387 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
class Car
{
static Car* single_instance_declared;
string _name;
string _engine;
int _wheels;
bool _kers; // KERS
// denying initializing through constructor
Car(const Car&);
Car& operator=(const Car&);
Car();
public:
static Car* getInstance();
void set_kers(bool);
bool kers();
};
Car* Car::single_instance_declared=nullptr;
Car::Car()
{
_name = "Ferrari f14T";
_engine = "Ferrari v6 turbo";
_wheels = 4;
_kers = false;
}
void Car::set_kers(bool status)
{
this-> _kers = status;
}
bool Car::kers()
{
return this-> _kers;
}
Car* Car::getInstance()
{
if (!single_instance_declared)
{
single_instance_declared = new Car();
return single_instance_declared;
}
return single_instance_declared;
}
int main(int argc, const char *argv[])
{
Car* ferrari_f1 = Car::getInstance();
Car* fake_ferrari = Car::getInstance();
ferrari_f1-> set_kers(true);
cout << fake_ferrari-> kers() << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment