Skip to content

Instantly share code, notes, and snippets.

@rapando
Created March 23, 2021 19:11
Show Gist options
  • Save rapando/76d3594fd430e5345d05a497cbb5fac7 to your computer and use it in GitHub Desktop.
Save rapando/76d3594fd430e5345d05a497cbb5fac7 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
// base (parent class)
class Person {
private:
string name;
string gender;
int age;
public:
void setName(string n);
void setGender(string g);
void setAge(int a);
void printPersonInfo();
};
void Person::setName(string n) {
Person::name = n;
}
void Person::setGender(string g) {
Person::gender = g;
}
void Person::setAge(int a) {
Person::age = a;
}
void Person::printPersonInfo() {
cout << "Your name is " << Person::name << ", you are a " << Person::gender << " who is " << Person::age << " years old\n";
}
// -- Teacher Class (derived/ child class)
class Teacher: public Person {
private:
string school;
float salary;
public:
void setSchool(string s);
void setSalary(float s);
void printTeacherInfo();
};
void Teacher::setSchool(string s) {
Teacher::school = s;
}
void Teacher::setSalary(float s) {
Teacher::salary = s;
}
void Teacher::printTeacherInfo() {
cout << "The teacher teaches at "<< Teacher::school << " and earns Ksh " << Teacher::salary << endl;
}
// derived/child/grandchild class
class HeadTeacher: public Teacher {
public:
void printStatus();
};
void HeadTeacher::printStatus() {
cout << "I am the freaking principal\n";
}
// derived/child class
class Carpenter: public Person {
public:
void printWork();
};
void Carpenter::printWork() {
cout << "I work with wood\n";
}
int main() {
Teacher t;
t.setSalary(800.00);
t.setSchool("Kilimo High");
t.printTeacherInfo();
t.setAge(30);
t.setName("Sam");
t.setGender("male");
t.printPersonInfo();
cout << "--------------------------" << endl;
Carpenter c;
c.printWork();
c.setAge(30);
c.setGender("Male");
c.setName("John");
c.printPersonInfo();
cout << "--------------------------" << endl;
HeadTeacher h;
h.printStatus();
h.setSalary(1000000.0);
h.setSchool("Molo High");
h.printTeacherInfo();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment