Computer-Science

Inheritance

  1. Introduction
  2. Base Classes and Derived Classes
  3. Protected Members
  4. CommissionEmployee Class
  5. BasePlusCommissionEmployeeClass WITHOUT Using Inheritance
  6. Creating an Inheritance Hierarchy
  7. Inheritance Hierarchy Using protected Data
  8. Inheritance Hierarchy Using private Data
  9. ์ƒ์† ๊ตฌ์กฐ์—์„œ ์ƒ์„ฑ์ž, ์†Œ๋ฉธ์ž์˜ ์‹คํ–‰ ์ˆœ์„œ
  10. public, protected and private Inheritance

1. Introduction

๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„ (object-oriented design)

Inheritance (์ƒ์†)

2. Base Classes and Derived Classes

ํด๋ž˜์Šค ๊ณ„์ธต (class hierarchy)

์ƒ์†์˜ ์„ธ ์ข…๋ฅ˜

์ƒ์† ๊ด€๊ณ„์˜ ์˜๋ฏธ

Software Engineering Observation

Member functions of a derived class cannot directly access private members of the base class.

๊ธฐ๋ณธ ํด๋ž˜์Šค๋Š” ํŒŒ์ƒ ํด๋ž˜์Šค๋ณด๋‹ค ๋” ๋„“์€ ๋ฒ”์œ„์˜ ๊ฐ์ฒด๋ฅผ ํ‘œํ˜„

Base class Derived classes
Student GraduateStudent, UndergraduateStudent
Shape Circle, Triangle, Rectangle, Sphere, Cube
Loan CarLoan, HomeImprovementLoan, MortgageLoad
Employee Faculty, Staff
Account CheckingAccount, SavingAccount

์ƒ์† ๊ณ„์ธต ๊ตฌ์กฐ (Inheritance hierarachy)

3. Protected Members

Public ์ƒ์†

์ ‘๊ทผ ์ง€์ •์ž protected

Protected๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๊ฐ€ ํ—ˆ์šฉํ•˜๋Š” ์ ‘๊ทผ์˜ ๋ฒ”์œ„

IMG_FF955B32D458-1

4. CommissionEmployee Class

๊ธฐ๋ณธ ํด๋ž˜์Šค์™€ ํŒŒ์ƒ ํด๋ž˜์Šค ์˜ˆ์ œ ๊ฐœ์š”

CommissionEmployee ํด๋ž˜์Šค ๊ตฌ์„ฑ

CommissionEmployee ํด๋ž˜์Šค ์˜ˆ์ œ

CommissionEmployee.h

#ifndef COMMISSION_H
#define COMMISSION_H

#include <string>
using std::string;

class CommissionEmployee
{
public:
    CommissionEmployee(const string &, const string &, const string &, double = 0.0, double = 0.0);

    void setFirstName(const string &);
    string getFirstName() const;

    void setLastName(const string &);
    string getLastName() const;

    void setSocialSecurityNumber(const string &);
    string getSocialSecurityNumber() const;

    void setGrossSales(double);
    double getGrossSales() const;

    void setCommissionRate(double);
    double getCommissionRate() const;

    double earnings() const;
    void print() const;

private:
    string firstName;
    string lastName;
    string socialSecurityNumber;
    double grossSales;
    double commissionRate;
};

#endif

CommissionEmployee.cpp

#include <iostream>
using std::cout;

#include "CommissionEmployee.h"

CommissionEmployee::CommissionEmployee(
    const string &first, const string &last, const string &ssn,
    double sales, double rate)
{
    firstName = first;
    lastName = last;
    socialSecurityNumber = ssn;
    setGrossSales(sales);
    setCommissionRate(rate);
}

void CommissionEmployee::setFirstName(const string &first)
{
    firstName = first;
}

string CommissionEmployee::getFirstName() const
{
    return firstName;
}

void CommissionEmployee::setLastName(const string &last)
{
    lastName = last;
}

string CommissionEmployee::getLastName() const
{
    return lastName;
}

void CommissionEmployee::setSocialSecurityNumber(const string &ssn)
{
    socialSecurityNumber = ssn;
}

string CommissionEmployee::getSocialSecurityNumber() const
{
    return socialSecurityNumber;
}

void CommissionEmployee::setGrossSales(double sales)
{
    grossSales = (sales < 0.0) ? 0.0 : sales;
}

double CommissionEmployee::getGrossSales() const
{
    return grossSales;
}

void CommissionEmployee::setCommissionRate(double rate)
{
    commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0;
}

double CommissionEmployee::getCommissionRate() const
{
    return commissionRate;
}

double CommissionEmployee::earnings() const
{
    return commissionRate * grossSales;
}

void CommissionEmployee::print() const
{
    cout << "commission employee: " << firstName << ' ' << lastName
         << "\nsocial security number: " << socialSecurityNumber
         << "\ngross sales: " << grossSales
         << "\ncommission rate: " << commissionRate;
}

main.cpp

#include <iostream>
using std::cout;
using std::endl;
using std::fixed;

#include <iomanip>
using std::setprecision;

#include "CommissionEmployee.h"

int main()
{
    CommissionEmployee employee(
        "Sue", "Jones", "222-22-2222", 10000, .06);

    cout << fixed << setprecision(2);

    cout << "Employee information obtained by get functions: \n"
         << "\nFirst name is " << employee.getFirstName()
         << "\nLast name is " << employee.getLastName()
         << "\nSocial security number is " << employee.getSocialSecurityNumber()
         << "\nGross sales is " << employee.getGrossSales()
         << "\nCommission rate is " << employee.getCommissionRate() << endl;

    employee.setGrossSales(8000);
    employee.setCommissionRate(.1);

    cout << "\nUpdate employee information output: " << endl;
    employee.print();

    cout << "\n\nEmployee's earnings: $" << employee.earnings() << endl;

    return 0;
}

5. BasePlusCommissionEmployeeClass WITHOUT Using Inheritance

์ƒ์†์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ BasePlusCommissionEmployee ํด๋ž˜์Šค

BasePlusCommissionEmployee.h

#ifndef BASEPLUS_H
#define BASEPLUS_H

#include <string>
using std::string;

#include "CommissionEmployee.h"

class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
    BasePlusCommissionEmployee(const string &, const string &, const string &,
                               double = 0.0, double = 0.0, double = 0.0);

    void setBaseSalary(double);
    double getBaseSalary() const;

    double earnings() const;
    void print() const;

private:
    double baseSalary;
};

#endif

BasePlusCommissionEmployee.cpp

#include <iostream>
using std::cout;

#include "BasePlusCommissionEmployee.h"

BasePlusCommissionEmployee::BasePlusCommissionEmployee(
    const string &first, const string &last, const string &ssn,
    double sales, double rate, double salary)
    : CommissionEmployee(first, last, ssn, sales, rate)
{
    setBaseSalary(salary);
}

void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
    baseSalary = (salary < 0.0) ? 0.0 : salary;
}

double BasePlusCommissionEmployee::getBaseSalary() const
{
    return baseSalary;
}

double BasePlusCommissionEmployee::earnings() const
{
    return baseSalary + (commissionRate * grossSales);
}

void BasePlusCommissionEmployee::print() const
{
    cout << "base-salaried commission emplyee: " << firstName << ' '
         << lastName << "\nsocial security number: " << socialSecurityNumber
         << "\ngross sales: " << grossSales
         << "\ncommission rate: " << commissionRate
         << "\nbase salary: " << baseSalary;
}

์ƒ์†์€ ์ž˜ ๋˜์—ˆ์œผ๋‚˜, Base class์˜ Private ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์—์„œ ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

6. Creating an Inheritance Hierarchy

7. Inheritance Hierarchy Using protected Data

Protected ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ

Protected ๋ฉค๋ฒ„๋ฅผ ์ด์šฉํ•œ CommissionEmployee ํด๋ž˜์Šค ๊ฐœ์„ 

CommissionEmployee.h

#ifndef COMMISSION_H
#define COMMISSION_H

#include <string>
using std::string;

class CommissionEmployee
{
public:
    CommissionEmployee(const string &, const string &, const string &, double = 0.0, double = 0.0);

    void setFirstName(const string &);
    string getFirstName() const;

    void setLastName(const string &);
    string getLastName() const;

    void setSocialSecurityNumber(const string &);
    string getSocialSecurityNumber() const;

    void setGrossSales(double);
    double getGrossSales() const;

    void setCommissionRate(double);
    double getCommissionRate() const;

    double earnings() const;
    void print() const;

protected:
    string firstName;
    string lastName;
    string socialSecurityNumber;
    double grossSales;
    double commissionRate;
};

#endif

BasePlusCommissionEmployee.h

#ifndef BASEPLUS_H
#define BASEPLUS_H

#include <string>
using std::string;

#include "CommissionEmployee.h"

class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
    BasePlusCommissionEmployee(const string &, const string &, const string &,
                               double = 0.0, double = 0.0, double = 0.0);

    void setBaseSalary(double);
    double getBaseSalary() const;

    double earnings() const;
    void print() const;

private:
    double baseSalary;
};

#endif

BasePlusCommissionEmployee.cpp

#include <iostream>
using std::cout;

#include "BasePlusCommissionEmployee.h"

BasePlusCommissionEmployee::BasePlusCommissionEmployee(
    const string &first, const string &last, const string &ssn,
    double sales, double rate, double salary)
    // explicitly call base-class constructor
    : CommissionEmployee(first, last, ssn, sales, rate)
{
    setBaseSalary(salary);
}

void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
    baseSalary = (salary < 0.0) ? 0.0 : salary;
}

double BasePlusCommissionEmployee::getBaseSalary() const
{
    return baseSalary;
}

double BasePlusCommissionEmployee::earnings() const
{
    // can access protected data of base class
    return baseSalary + (commissionRate * grossSales);
}

void BasePlusCommissionEmployee::print() const
{
    // can access protected data of base class
    cout << "base-salaried commission emplyee: " << fFirstName << ' '
         << lastName << "\nsocial security number: " << socialSecurityNumber
         << "\ngross sales: " << grossSales
         << "\ncommission rate: " << commissionRate
         << "\nbase salary: " << baseSalary;
}

Protected ์ ‘๊ทผ ์ง€์ •์ž ์‚ฌ์šฉ์˜ ์žฅ๋‹จ์ 

8. Inheritance Hierarchy Using private Data

๊ณ„์ธต ๊ตฌ์กฐ์˜ ๊ฐœ์„ 

CommissionEmployee ํด๋ž˜์Šค ๊ฐœ์„  (์ตœ์ข…๋ฒ„์ „)

CommissionEmployee.h

#include <iostream>
using std::cout;

#include "CommissionEmployee.h"

// constructor
CommissionEmployee::CommissionEmployee(
    const string &first, const string &last, const string &ssn,
    double sales, double rate)
    : firstName(first), lastName(last), socialSecurityNumber(ssn)
{
    setGrossSales(sales);
    setCommissionRate(rate);
}

void CommissionEmployee::setFirstName(const string &first)
{
    firstName = first;
}

string CommissionEmployee::getFirstName() const
{
    return firstName;
}

void CommissionEmployee::setLastName(const string &last)
{
    lastName = last;
}

string CommissionEmployee::getLastName() const
{
    return lastName;
}

void CommissionEmployee::setSocialSecurityNumber(const string &ssn)
{
    socialSecurityNumber = ssn;
}

string CommissionEmployee::getSocialSecurityNumber() const
{
    return socialSecurityNumber;
}

void CommissionEmployee::setGrossSales(double sales)
{
    grossSales = (sales < 0.0) ? 0.0 : sales;
}

double CommissionEmployee::getGrossSales() const
{
    return grossSales;
}

void CommissionEmployee::setCommissionRate(double rate)
{
    commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0;
}

double CommissionEmployee::getCommissionRate() const
{
    return commissionRate;
}

double CommissionEmployee::earnings() const
{
    return getCommissionRate() * getGrossSales();
}

void CommissionEmployee::print() const
{
    cout << "commission employee: " << getFirstName() << ' ' << getLastName()
         << "\nsocial security number: " << getSocialSecurityNumber()
         << "\ngross sales: " << getGrossSales()
         << "\ncommission rate: " << getCommissionRate();
}

BasePlusCommissionEmployee.cpp

#include <iostream>
using std::cout;

#include "BasePlusCommissionEmployee.h"

BasePlusCommissionEmployee::BasePlusCommissionEmployee(
    const string &first, const string &last, const string &ssn,
    double sales, double rate, double salary)
    // explicitly call base-class constructor
    : CommissionEmployee(first, last, ssn, sales, rate)
{
    setBaseSalary(salary);
}

void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
    baseSalary = (salary < 0.0) ? 0.0 : salary;
}

double BasePlusCommissionEmployee::getBaseSalary() const
{
    return baseSalary;
}

double BasePlusCommissionEmployee::earnings() const
{
    // can access protected data of base class
    return getBaseSalary() + CommissionEmployee::earnings();
}

void BasePlusCommissionEmployee::print() const
{
    // can access protected data of base class
    cout << "base-salaried ";

    // invoke CommissionEmployee's print function
    CommissionEmployee::print();

    cout << "\nbase salary: " << getBaseSalary();
}

์„ค๊ณ„๋œ ํด๋ž˜์Šค ํ™œ์šฉ (driver)

#include <iostream>
#include <iomanip>

using namespace std;

#include "BasePlusCommissionEmployee.h"

int main()
{
    // instantiate BasePlusCommissionEmployee object
    BasePlusCommissionEmployee
        employee("Bob", "Lewis", "333-33-3333", 5000, .04, 300);

    cout << fixed << setprecision(2);

    cout << "Employee information obtained by get function: \n"
         << "\nFirst name is " << employee.getFirstName()
         << "\nLast name is " << employee.getLastName()
         << "\nsocial security number is " << employee.getSocialSecurityNumber()
         << "\nGross sales is " << employee.getGrossSales()
         << "\ncommission rate is " << employee.getCommissionRate()
         << "\nBase salary is " << employee.getBaseSalary() << endl;

    employee.setGrossSales(1000);

    // employee.setGrossSales(8000);
    // employee.setCommissionRate(.1);

    cout << "\nUpdated employee information ouput bt print function: \n"
         << endl;
    employee.print();

    cout << "\n\nEmployee's earning: $" << employee.earnings() << endl;

    return 0;
}

์„ค๊ณ„๋œ ํด๋ž˜์Šค ํ™œ์šฉ (์‹คํ–‰ ๊ฒฐ๊ณผ)

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-06 แ„‹แ…ฉแ„’แ…ฎ 8 31 03

9. ์ƒ์† ๊ตฌ์กฐ์—์„œ ์ƒ์„ฑ์ž, ์†Œ๋ฉธ์ž์˜ ์‹คํ–‰ ์ˆœ์„œ

ํŒŒ์ƒ ํด๋ž˜์Šค ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๊ณผ์ •

ํŒŒ์ƒ ํด๋ž˜์Šค ๊ฐ์ฒด์˜ ์†Œ๋ฉธ ๊ณผ์ •

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-06 แ„‹แ…ฉแ„’แ…ฎ 8 36 49

์ƒ์„ฑ์ž ๋ฐ ์†Œ๋ฉธ์ž ํ˜ธ์ถœ ์ˆœ์„œ ์˜ˆ์ œ

CommissionEmployee.cpp

#include <iostream>
using std::cout;

#include "CommissionEmployee.h"

// constructor
CommissionEmployee::CommissionEmployee(
    const string &first, const string &last, const string &ssn,
    double sales, double rate)
    : firstName(first), lastName(last), socialSecurityNumber(ssn)
{
    setGrossSales(sales);
    setCommissionRate(rate);

    cout << "CommissionEmployee constuctor: " << endl;
    print();
    cout << "\n\n";
}

// destructor
CommissionEmployee::~CommissionEmployee()
{
    cout << "CommissionEmployee desturctor: " << endl;
    print();
    cout << "\n\n";
}

void CommissionEmployee::setFirstName(const string &first)
{
    firstName = first;
}

string CommissionEmployee::getFirstName() const
{
    return firstName;
}

void CommissionEmployee::setLastName(const string &last)
{
    lastName = last;
}

string CommissionEmployee::getLastName() const
{
    return lastName;
}

void CommissionEmployee::setSocialSecurityNumber(const string &ssn)
{
    socialSecurityNumber = ssn;
}

string CommissionEmployee::getSocialSecurityNumber() const
{
    return socialSecurityNumber;
}

void CommissionEmployee::setGrossSales(double sales)
{
    grossSales = (sales < 0.0) ? 0.0 : sales;
}

double CommissionEmployee::getGrossSales() const
{
    return grossSales;
}

void CommissionEmployee::setCommissionRate(double rate)
{
    commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0;
}

double CommissionEmployee::getCommissionRate() const
{
    return commissionRate;
}

double CommissionEmployee::earnings() const
{
    return getCommissionRate() * getGrossSales();
}

void CommissionEmployee::print() const
{
    cout << "commission employee: " << getFirstName() << ' ' << getLastName()
         << "\nsocial security number: " << getSocialSecurityNumber()
         << "\ngross sales: " << getGrossSales()
         << "\ncommission rate: " << getCommissionRate();
}

BasePlusCommissionEmployee.cpp

#include <iostream>
using std::cout;

#include "BasePlusCommissionEmployee.h"

BasePlusCommissionEmployee::BasePlusCommissionEmployee(
    const string &first, const string &last, const string &ssn,
    double sales, double rate, double salary)
    // explicitly call base-class constructor
    : CommissionEmployee(first, last, ssn, sales, rate)
{
    setBaseSalary(salary);

    cout << "BasePlusCommissionEmployee constructor: " << endl;
    print();
    cout << "\n\n";
}

// destructor
BasePlusCommissionEmployee::~BasePlusCommissionEmployee()
{
    cout << "BasePlusCommissionEmployee destructor: " << endl;
    print();
    cout << "\n\n";
}

void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
    baseSalary = (salary < 0.0) ? 0.0 : salary;
}

double BasePlusCommissionEmployee::getBaseSalary() const
{
    return baseSalary;
}

double BasePlusCommissionEmployee::earnings() const
{
    // can access protected data of base class
    return getBaseSalary() + CommissionEmployee::earnings();
}

void BasePlusCommissionEmployee::print() const
{
    // can access protected data of base class
    cout << "base-salaried ";

    // invoke CommissionEmployee's print function
    CommissionEmployee::print();

    cout << "\nbase salary: " << getBaseSalary();
}

driver

#include <iostream>
#include <iomanip>

using namespace std;

#include "BasePlusCommissionEmployee.h"

int main()
{
    cout << fixed << setprecision(2);

    { // begin new scope
        CommissionEmployee employee1("Bob", "Lewis", "111-11-1111", 1000, .01);

    } // end scope

    cout << endl;
    BasePlusCommissionEmployee employee2("Peter", "Lee", "221-12-2222", 2000, .02, 222);

    cout << endl;
    BasePlusCommissionEmployee employee3("JJang-gu", "Shin", "331-13-3113", 3000, .03, 333);

    cout << endl;
    return 0;
}

์‹คํ–‰๊ฒฐ๊ณผ

CommissionEmployee constuctor:
commission employee: Bob Lewis
social security number: 111-11-1111
gross sales: 1000.00
commission rate: 0.01

CommissionEmployee desturctor:
commission employee: Bob Lewis
social security number: 111-11-1111
gross sales: 1000.00
commission rate: 0.01


CommissionEmployee constuctor:
commission employee: Peter Lee
social security number: 221-12-2222
gross sales: 2000.00
commission rate: 0.02

BasePlusCommissionEmployee constructor:
base-salaried commission employee: Peter Lee
social security number: 221-12-2222
gross sales: 2000.00
commission rate: 0.02
base salary: 222.00


CommissionEmployee constuctor:
commission employee: JJang-gu Shin
social security number: 331-13-3113
gross sales: 3000.00
commission rate: 0.03

BasePlusCommissionEmployee constructor:
base-salaried commission employee: JJang-gu Shin
social security number: 331-13-3113
gross sales: 3000.00
commission rate: 0.03
base salary: 333.00


BasePlusCommissionEmployee destructor:
base-salaried commission employee: JJang-gu Shin
social security number: 331-13-3113
gross sales: 3000.00
commission rate: 0.03
base salary: 333.00

CommissionEmployee desturctor:
commission employee: JJang-gu Shin
social security number: 331-13-3113
gross sales: 3000.00
commission rate: 0.03

BasePlusCommissionEmployee destructor:
base-salaried commission employee: Peter Lee
social security number: 221-12-2222
gross sales: 2000.00
commission rate: 0.02
base salary: 222.00

CommissionEmployee desturctor:
commission employee: Peter Lee
social security number: 221-12-2222
gross sales: 2000.00
commission rate: 0.02

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-06 แ„‹แ…ฉแ„’แ…ฎ 8 56 00 แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-06 แ„‹แ…ฉแ„’แ…ฎ 8 56 26

10. Public, protected and private Inheritance

Public ์ƒ์†, Protected ์ƒ์† ๋ฐ private ์ƒ์†

Public ์ƒ์†

protected ์ƒ์†

private ์ƒ์†

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-06 แ„‹แ…ฉแ„’แ…ฎ 8 58 20 แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-06 แ„‹แ…ฉแ„’แ…ฎ 8 58 35

ํŒŒ์ƒ ํด๋ž˜์Šค์—์„œ์˜ ๊ธฐ๋ณธ ํด๋ž˜์Šค ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„ ์ ‘๊ทผ์„ฑ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-06 แ„‹แ…ฉแ„’แ…ฎ 8 59 02