After finishing reading a tutorial on bit flags, i decided to do some practicing.
Here's what I came up with:
Code:
#include <iostream>
#include <string>
using namespace std;
enum PersonSettings
{
Works = 1,
Studies = 2,
HasKids = 4,
LivesAlone = 8
};
class Person
{
public:
Person(string n, PersonSettings s);
void printSettings();
private:
string name;
PersonSettings settings;
};
Person::Person(string n, PersonSettings s) : name(n), settings(s)
{
}
void Person::printSettings()
{
cout<<"WORKS? ";
if(settings & Works) cout<<"YES"; else cout<<"NO";
cout<<endl;
cout<<"STUDIES? ";
if(settings & Studies) cout<<"YES"; else cout<<"NO";
cout<<endl;
cout<<"HAS KIDS? ";
if(settings & HasKids) cout<<"YES"; else cout<<"NO";
cout<<endl;
cout<<"LIVES ALONE? ";
if(settings & LivesAlone) cout<<"YES"; else cout<<"NO";
cout<<endl;
}
int main()
{
Person someone("Jack",LivesAlone | HasKids);
someone.printSettings();
cout<<endl;
}
It works well ONLY if set one bit flag on the constructor, if i try to set more than one bit flag (like in the code above) it displays the following error report:
error: invalid conversion from `int' to `PersonSettings'|
line of error:
Person someone("Jack",LivesAlone | HasKids);
Oh, and BTW, is this a correct usage of bit flags?