Last active
April 29, 2022 03:08
-
-
Save suragch/ee761bc2d5eb4977a3a97c8fec359a3d to your computer and use it in GitHub Desktop.
Inheritance
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 <regex> | |
using namespace std; | |
class AbstractEmployee | |
{ | |
// Returns true if got promotion | |
virtual bool RequestPromotion() = 0; | |
}; | |
class Employee : AbstractEmployee | |
{ | |
private: | |
string Company; | |
int Age; | |
protected: | |
string Name; | |
public: | |
// constructor | |
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; | |
} | |
} | |
}; | |
// Developer = child class, subclass | |
// Employee = parent class, super class | |
class Developer : public Employee | |
{ | |
string FavoriteLanguage; | |
public: | |
Developer(string name, string company, int age, string favoriteLanguage) | |
: Employee(name, company, age) | |
{ | |
FavoriteLanguage = favoriteLanguage; | |
} | |
string getFavoriteLanguage() | |
{ | |
return FavoriteLanguage; | |
} | |
void fixBug() | |
{ | |
// Jonathan fixed a bug using Dart. | |
cout << Name << " fixed a bug using " << FavoriteLanguage << ".\n"; | |
} | |
void Introduce() | |
{ | |
cout << Name << "'s favorite language is " << FavoriteLanguage << ".\n"; | |
} | |
}; | |
// Teacher | |
class Teacher : public Employee { | |
string Subject; | |
public: | |
Teacher(string name, string company, int age, string subject) | |
: Employee(name, company, age) | |
{ | |
Subject = subject; | |
} | |
void prepareLesson() { | |
cout << Name << " is preparing a " << Subject << " lesson.\n"; | |
} | |
}; | |
int main() | |
{ | |
Employee bob = Employee("Bob", "Starbucks", 18); | |
Employee mary = Employee("Mary", "Facebook", 30); | |
Developer me = Developer("Jonathan", "SEU", 44, "Dart"); | |
bob.Introduce(); | |
me.fixBug(); | |
me.Introduce(); | |
Teacher susan = Teacher("Susan", "Super High School", 51, "math"); | |
susan.prepareLesson(); | |
// Susan is preparing a math lesson. | |
susan.Introduce(); | |
// Susan, age 51, works at Super High School. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment