Skip to content

Instantly share code, notes, and snippets.

@suragch
Last active April 29, 2022 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suragch/b06dc3fedab63f5a9b2e09c6bfd6f6ac to your computer and use it in GitHub Desktop.
Save suragch/b06dc3fedab63f5a9b2e09c6bfd6f6ac to your computer and use it in GitHub Desktop.
encapsulation
// https://www.youtube.com/watch?v=wN0x9eZLix4
#include <iostream>
#include <regex>
using namespace std;
class AbstractEmployee
{
// Returns true if got promotion
virtual bool RequestPromotion() = 0;
};
class Employee : AbstractEmployee
{
private:
string Name;
string Company;
int Age;
public:
Employee(string name, string company, int age)
{
Name = name;
Company = company;
Age = age;
}
// Setters and getters
void setName(string value)
{
if (value.length() > 50)
{
return;
}
auto nameMatch = regex("^[a-zA-Z]+$");
if (!regex_match(value, nameMatch))
{
return;
}
Name = value;
}
string getName()
{
return Name;
}
void setCompany(string company)
{
Company = company;
}
string getCompany()
{
return Company;
}
void setAge(int value)
{
if (value < 0)
{
return;
}
Age = value;
}
int getAge()
{
return Age;
}
void Introduce()
{
cout << Name << ", age " << Age << ", works at " << Company << ".\n";
}
bool RequestPromotion()
{
if (Age < 30){
cout << Name << " did not get promoted.\n";
return false;
}else {
cout << Name << " got promoted.\n";
return true;
}
}
};
int main()
{
Employee bob = Employee("Bob", "Starbucks", 18);
Employee mary = Employee("Mary", "Facebook", 30);
bob.Introduce();
mary.Introduce();
bool isBobPromoted = bob.RequestPromotion();
bool isMaryPromoted = mary.RequestPromotion();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment