1 / 14

Class Inheritance

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(); };. Constructors.

napua
Download Presentation

Class Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class Inheritance Dr. Leon Jololian

  2. 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

  3. Constructors Person::Person(string na, int ag) { name = na; age = ag; } Person::Person(string na) { name = na; } Dr.Jololian

  4. “Get”-ter Functions string Person::getName() { return name; } int Person::getAge() { return age; } Dr.Jololian

  5. “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

  6. 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

  7. Output Name: John Doe Age: 18 Name: Jane Smith Age: 21 Name: Jane Johnson Age: 21 Dr.Jololian

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Dr.Jololian

More Related