140 likes | 260 Vues
This document provides a detailed overview of object-oriented programming principles using C++. It illustrates inheritance with several classes, including Person, Student, and Employee, all implemented by Dr. Leon Jololian. Each class demonstrates the use of constructors, getters, setters, and the print function to display object attributes. Examples include creating instances of Person, Student, and Employee, showcasing their attributes, and modifying them. This serves as a practical example for understanding class hierarchies and encapsulation in C++.
E N D
Class Inheritance Dr. Leon Jololian
class Person { private: string name; int age; public: Person(string na, int ag); Person(string na); string getName(); int getAge(); void setName(string na); void setAge(int ag); void print(); }; Dr.Jololian
Constructors Person::Person(string na, int ag) { name = na; age = ag; } Person::Person(string na) { name = na; } Dr.Jololian
“Get”-ter Functions string Person::getName() { return name; } int Person::getAge() { return age; } Dr.Jololian
“Set”-ter Functions void Person::setName(string na) { name = na; } void Person::setAge(int ag) { age = ag; } void Person::print() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } Dr.Jololian
Main Function void main() { Person p("John Doe", 18); p.print(); Person q("Jane Smith"); q.setAge(21); q.print(); q.setName("Jane Johnson"); q.print(); } Dr.Jololian
Output Name: John Doe Age: 18 Name: Jane Smith Age: 21 Name: Jane Johnson Age: 21 Dr.Jololian
class Student : public Person { private: string major; float gpa; public: Student(string na, int ag, string ma, float gp); string getMajor() { return major; } float getGpa() { return gpa; } void setMajor(string ma) { major = ma; } void setGpa(float gp) { gpa = gp; } void print(); }; Dr.Jololian
Student::Student(string na, int ag, string ma, float gp) : Person(na, ag) { major = ma; gpa = gp; } void Student::print() { Person::print(); cout << "Major: " << major << endl; cout << "GPA: " << gpa << endl; } Dr.Jololian
Student s("Max Gomez", 19, "Business", float(3.8)); s.print(); s.setAge(20); s.setGpa(3.75); s.print(); Name: Max Gomez Age: 19 Major: Business GPA: 3.8 Name: Max Gomez Age: 20 Major: Business GPA: 3.75 Dr.Jololian
class Employee : public Person { private: string company; float salary; public: Employee(string na, int ag, string co, float sa); string getCompany(){ return company; } float getSalary() { return salary; } void setCompany(string co) { company = co; } void setSalary(float sa) {salary = sa; } void print(); }; Dr.Jololian
Employee::Employee(string na, int ag, string co, float sa) :Person(na, ag) { company = co; salary = sa; } void Employee::print(){ Person::print(); cout << "Company: " << company << endl; cout << "Salary: " << salary << endl; } Dr.Jololian
Employee e("Joe Brown", 34, "IBM", float(54320.95)); e.print(); e.setAge(38); e.setSalary(float(64300.52)); e.print(); Name: Joe Brown Age: 34 Company: IBM Salary: 54320.9 Name: Joe Brown Age: 38 Company: IBM Salary: 64300.5 Dr.Jololian