Class inheritance
This presentation is the property of its rightful owner.
Sponsored Links
1 / 14

Class Inheritance PowerPoint PPT Presentation


  • 59 Views
  • Uploaded on
  • Presentation posted in: General

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.

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Class inheritance

Class Inheritance

Dr. Leon Jololian


Class inheritance

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

Constructors

Person::Person(string na, int ag)

{

name = na;

age = ag;

}

Person::Person(string na)

{

name = na;

}

Dr.Jololian


Get ter functions

“Get”-ter Functions

string Person::getName()

{

return name;

}

int Person::getAge()

{

return age;

}

Dr.Jololian


Set ter functions

“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

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

Output

Name: John Doe

Age: 18

Name: Jane Smith

Age: 21

Name: Jane Johnson

Age: 21

Dr.Jololian


Class inheritance

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


Class inheritance

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


Class inheritance

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 inheritance

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


Class inheritance

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


Class inheritance

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


Class inheritance

Dr.Jololian


  • Login