- Introduction
- Base Classes and Derived Classes
- Protected Members
- CommissionEmployee Class
- BasePlusCommissionEmployeeClass WITHOUT Using Inheritance
- Creating an Inheritance Hierarchy
- Inheritance Hierarchy Using protected Data
- Inheritance Hierarchy Using private Data
- ์์ ๊ตฌ์กฐ์์ ์์ฑ์, ์๋ฉธ์์ ์คํ ์์
- public, protected and private Inheritance
- ์ํํธ์จ์ด ์ฌ์ฌ์ฉ์ ์ํ ํจ๊ณผ์ ์ธ ๋ฐฉ๋ฒ
- ๊ธฐ์กด ํด๋์ค๋ก๋ถํฐ ์๋ก์ด ํด๋์ค ์์ฑ
- ๊ธฐ์กด ํด๋์ค์ ์์ฑ๊ณผ ํ๋ ๋ฐฉ์์ ํก์
- ์๋ก์ด ์์ฑ๊ณผ ํ๋ ๋ฐฉ์์ ์ถ๊ฐํ์ฌ ๋ณด๋ค ๊ตฌ์ฒดํํจ
- ๊ธฐ๋ณธ ํด๋์ค๋ก๋ถํฐ ์์๋ฐ์ ํ์ ํด๋์ค๊ฐ ์์ฑ๋๋ค.
- ๊ฐ์ฒด๋ค์ ๋ ํนํ๋(specialized) ๊ทธ๋ฃน
- ๊ธฐ๋ณธ ํด๋์ค (base class)๋ก๋ถํฐ ํ๋ ๋ฐฉ์์ ์์ ๋ฐ์
- ์์ ๋ง์ ํ๋ ๋ฐฉ์์ผ๋ก ํนํ(customize) ๊ฐ๋ฅ
- ์ถ๊ฐ์ ์ธ ํ๋ ๋ฐฉ์๋ ํฌํจ
- ์ง์ ๊ธฐ๋ณธ ํด๋์ค (direct base class)
- ์ง์ ์์ ๋ฐ์ ๋ฐ๋ก ์ ๋จ๊ณ ๊ธฐ๋ณธ ํด๋์ค
- ๊ฐ์ ๊ธฐ๋ณธ ํด๋์ค (indirect base class)
- ๋ ๋จ๊ณ ์ด์ ์์์ ๊ธฐ๋ณธ ํด๋์ค
- ๋จ์ผ ์์ (single inheritance)
- ํ๋์ ๊ธฐ๋ณธ ํด๋์ค๋ก๋ถํฐ ์์
- ๋ค์ค ์์ (multiple inheritance)
-
public
- ํ์ ํด๋์ค์ ๊ฐ์ฒด๋ ๊ธฐ๋ณธ ํด๋์ค์ ๊ฐ์ฒด์ด๊ธฐ๋ ํ๋ค.
- ๊ธฐ๋ณธ ํด๋์ค์ ๊ฐ์ฒด๋ ํ์ ํด๋์ค์ ๊ฐ์ฒด๊ฐ ๋ ์ ์๋ค.
- e.g., ๋ชจ๋ ์๋์ฐจ๋ ํ ๊ฒ์ด๋ค, ๊ทธ๋ฌ๋ ๋ชจ๋ ํ ๊ฒ์ ์ฐจ๊ฐ ์๋๋ค.
- ๊ธฐ๋ณธ ํด๋์ค์ non-private ๋ฉค๋ฒ์ ์ ๊ทผ ๊ฐ๋ฅํ๋ค.
- ๊ธฐ๋ณธ ํด๋์ค์ private ๋ฉค๋ฒ์ ์ ๊ทผํ๊ธฐ ์ํด์๋
- ํ์ ํด๋์ค๋ ๋ฐ๋์ ์์๋ฐ์ non-private ๋ฉค๋ฒ ํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
- ๊ธฐ๋ณธ ํด๋์ค์ private ๋ฉค๋ฒ์ ์ ๊ทผํ๊ธฐ ์ํด์๋
- ํ์ ํด๋์ค์ ๊ฐ์ฒด๋ ๊ธฐ๋ณธ ํด๋์ค์ ๊ฐ์ฒด์ด๊ธฐ๋ ํ๋ค.
-
private, protected ์ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก ๋ณธ ๊ฐ์์์๋ public ์์์ ์ด์
- ์ถ์ํ(abstraction)
- ์์คํ ์ ๋ชจ๋ ๊ฐ์ฒด๋ค์ ๊ณตํต์ฑ์ ์ฃผ๋ชฉ
- โis-aโ ๊ด๊ณ vs. โhas-aโ ๊ด๊ณ
- โis-aโ ๊ด๊ณ โ ์์
- ํ์ ํด๋์ค์ ๊ฐ์ฒด๋ ๊ธฐ๋ณธ ํด๋์ค์ ๊ฐ์ฒด์ฒ๋ผ ๋ค๋ค์ง๋ค.
- e.g., ์ฐจ๋ ํ ๊ฒ์ด๋ค. (Car is a vehicle)
- โhas-aโ ๊ด๊ณ โ ๋ณตํฉ (composition) ๊ด๊ณ
- ๊ฐ์ฒด๊ฐ ๋ค๋ฅธ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๋ฉค๋ฒ๋ก ํฌํจํ๋ ๊ฒ
- e.g., ์ฐจ์๋ ์ด์ ๋๊ฐ ์๋ค. (Car has a steering wheel)
- โis-aโ ๊ด๊ณ โ ์์
Member functions of a derived class cannot directly access private members of the base class.
- e.g.,
- ๊ธฐ๋ณธ ํด๋์ค : Vehicle
- ์ฐจ(car), ํธ๋ญ, ๋ณดํธ, ์์ ๊ฑฐ ๋ฑ์ ํฌํจ
- ํ์ ํด๋์ค : Car
- ์๊ณ ๋ณด๋ค ํนํ๋ ํ ๊ฒ(vehicle)์ ์ข ๋ฅ
- ๊ธฐ๋ณธ ํด๋์ค : Vehicle
Base class | Derived classes |
---|---|
Student | GraduateStudent, UndergraduateStudent |
Shape | Circle, Triangle, Rectangle, Sphere, Cube |
Loan | CarLoan, HomeImprovementLoan, MortgageLoad |
Employee | Faculty, Staff |
Account | CheckingAccount, SavingAccount |
- ์์ ๊ด๊ณ๋ ํธ๋ฆฌ(tree) ํํ์ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ํ์ฑํ๋ค.
- ๊ฐ ํด๋์ค๋ ๋ค์๊ณผ ๊ฐ๋ค.
- ๊ธฐ๋ณธ ํด๋์ค (base class)
- : ํ์ ํด๋์ค์ ์์ฑ / ํ๋ ๋ฐฉ์์ ๊ณต๊ธํ๋ค.
- ํ์ ํด๋์ค (derived class)
- : ๊ธฐ๋ณธ ํด๋์ค๋ก๋ถํฐ ์์ฑ / ํ๋ ๋ฐฉ์์ ์์๋ฐ๋๋ค
- ๊ธฐ๋ณธ ํด๋์ค (base class)
- e.g,
Class TwoDimensionalShape : public Shape
- : TwoDimensionalShape ํด๋์ค๋ Shape ํด๋์ค๋ก๋ถํฐ ์์ ๋ฐ์
- ๊ธฐ๋ณธ ํด๋์ค private ๋ฉค๋ฒ
- protected ์ ๊ทผ
- public ๊ณผ private์ ์ค๊ฐ ๋ณดํธ ๋จ๊ณ
- protected ๋ฉค๋ฒ๋ ๋ค์์ ํตํด ์ ๊ทผ ๊ฐ๋ฅํ๋ค.
- ๊ธฐ๋ณธ ํด๋์ค์ ๋ฉค๋ฒ์ ํ์ ํด๋์ค์ ๋ฉค๋ฒ
- ํ์ ํด๋์ค ๋ฉค๋ฒ
- ๊ธฐ๋ณธ ๋ฉค๋ฒ์ public ๊ณผ protected ๋ฉค๋ฒ์ ์ง์ ์ ๊ทผ ๊ฐ๋ฅ
- ๊ฐ๋จํ ๋ฉค๋ฒ ์ด๋ฆ์ ์ฌ์ฉ
- ํ์ ํด๋์ค์์ ์ฌ์ ์๋ ๊ธฐ๋ณธ ํด๋์ค ๋ฉค๋ฒ๋ ๊ธฐ๋ณธ ํด๋์ค ์ด๋ฆ๊ณผ ์ดํญ ์ค์ฝํ ์๋ณ ์ฐ์ฐ์(
::
) ๋ฅผ ์ด์ฉํ์ฌ ์ ๊ทผํ ์ ์๋ค.- e.g.,
shape::area
- e.g.,
- ๊ธฐ๋ณธ ๋ฉค๋ฒ์ public ๊ณผ protected ๋ฉค๋ฒ์ ์ง์ ์ ๊ทผ ๊ฐ๋ฅ
- CommissionEmployee โ BasePlusCommissionEmployee ์์ ๊ณ์ธต
- CommissionEmployee
- First name, last name, SSN, commission rate, gross sale amount
- BasePlusCommissionEmployee
- First name, last name, SSN, commission rate, gross sale amount
- ๊ธฐ๋ณธ ๋ด๊ธ
- ใ Commission Employee: ํ๋งค ๊ธ์ก์ ์ผ๋ถ๋ฅผ ์๋น์ผ๋ก ๊ฐ์ ธ๊ฐ๋ ์์ ์ฌ์ (๊ธฐ๋ณธ ์๊ธ์ด ์์ ์๋, ์์ ์๋ ์์)
- CommissionEmployee
- CommissionEmployee ํค๋ ํ์ผ (๋ค์ ์์ )
- public services
- ์์ฑ์
- get ๊ณผ set ํจ์
- ๋ฉค๋ฒ ํจ์ earnings, print
- public services
- CommissionEmployee ์์ค ์ฝ๋ ํ์ผ (๋ค์ ์์ )
#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
#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;
}
#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;
}
- CommissionEmployee ํด๋์ค๋ก๋ถํฐ ์์๋ฐ์ ํ์
- is-a CommissionEmployee
- ๋ชจ๋ public ๋ฉค๋ฒ ์
- ์์ฑ์๋ ์์๋์ง ์๋๋ค.
- ๊ธฐ๋ณธ ํด๋์ค์ ๋ฐ์ดํฐ ๋ฉค๋ฒ ์ด๊ธฐํ๋ฅผ ์ํด ๊ธฐ๋ณธ ํด๋์ค ์ด๊ธฐํ ๊ตฌ๋ฌธ์ ์ฌ์ฉ
- ๋ฐ์ดํฐ ๋ฉค๋ฒ baseSalary๋ฅผ ๊ฐ๋๋ค.
#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
#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 ๋ฉค๋ฒ์ ์ ๊ทผํ๋ ๋ฐฉ์์์ ์ปดํ์ผ ์ค๋ฅ ๋ฐ์
- ๊ธฐ๋ณธ ํด๋์ค ํค๋ ํ์ผ์ include ํ๋ค.
- ๊ธฐ๋ณธ ํด๋์ค์ ํค๋ ํ์ผ์ ๋ค์ ์ธ๊ฐ์ง๋ก ์ด์ ๋ก ๋ฐ๋์ ํ์ ํด๋์ค ํค๋ ํ์ผ์ include ๋์ด์ผ ํ๋ค.
- ๊ธฐ๋ณธ ํด๋์ค์ ์กด์ฌ๋ฅผ ์๊ธฐ ์ํด
- ์์๋ ๋ฐ์ดํฐ ๋ฉค๋ฒ์ ํฌ๊ธฐ๋ฅผ ์๊ธฐ ์ํด
- ์์๋ ํด๋์ค์ ๋ฉค๋ฒ๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ฌ์ฉ๋๋์ง ์๊ธฐ ์ํด
- ๊ธฐ๋ณธ ํด๋์ค์ ํค๋ ํ์ผ์ ๋ค์ ์ธ๊ฐ์ง๋ก ์ด์ ๋ก ๋ฐ๋์ ํ์ ํด๋์ค ํค๋ ํ์ผ์ include ๋์ด์ผ ํ๋ค.
- BasePlusCommissionEmployee ํด๋์ค (ํ์ ํด๋์ค)๊ฐ ๊ธฐ๋ณธ ํด๋์ค
- (CommissionEmployee) ๋ฐ์ดํฐ ๋ฉค๋ฒ์ ์ง์ ์ ๊ทผํ๊ธฐ ์ํด์ protected ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
- ๊ธฐ๋ณธ ํด๋์ค์ protected ๋ฉค๋ฒ๋ ๊ธฐ๋ณธ ํด๋์ค์ ๋ชจ๋ ํ์ ํด๋์ค์ ์์๋๋ฉฐ ํ์ ํด๋์ค์ ๋ฉค๋ฒ ํจ์์์ ์ ๊ทผํ ์ ์๋ค.
#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
#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
#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;
}
-
์ฅ์
- ํ์ ํด๋์ค๊ฐ ๊ธฐ๋ณธ ํด๋์ค์ ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ฅผ ๋ฐ๋ก ์กฐ์ ๊ฐ๋ฅ
- set | get ํธ์ถํ๋๋ฐ ์๋ชจ๋๋ ์ค๋ฒํค๋๋ฅผ ํผํ ์ ์๋ค.
- ์ฆ, ์ํ ์๋๊ฐ ์กฐ๊ธ ํฅ์๋๋ค.
- ํ์ ํด๋์ค๊ฐ ๊ธฐ๋ณธ ํด๋์ค์ ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ฅผ ๋ฐ๋ก ์กฐ์ ๊ฐ๋ฅ
-
๋จ์
- ์ ํจ์ฒดํฌ ๋ถ๊ฐ โ set | get ํจ์๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก ํ์ ํด๋์ค์ ๋น์ ์์ ์ธ ๊ฐ์ด ํ ๋น๋ ์ ์๋ค.
- ์คํ ์์กด์
- ํ์ ํด๋์ค ํจ์๋ ๊ธฐ๋ณธ ํด๋์ค ๊ตฌํ์ ๋ ์์กด์ ์ด ๋จ.
- ๊ธฐ๋ณธ ํด๋์ค๋ฅผ ์์ ํ๋ฉด ํ์ ํด๋์ค ๋ํ ์กฐ์ ๋์ด์ผ ํ๋ค.
- ๋ถ์์ง๊ธฐ ์ฌ์ด(fragile) ์ํํธ์จ์ด
-
์ํํธ์จ์ด ๊ณตํ ๊ธฐ๋ฒ์ ๊ถ๊ณ ๋ฅผ ๋ฐ๋ฆ
- ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ private๋ก ์ ์ธ
- public get ๊ณผ set ๋ฉค๋ฒ ํจ์ ์ ๊ณต
- ๊ธฐ๋ณธ ํด๋์ค์์ ์์๋ get ๋ฉค๋ฒ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํ์ ํด๋์ค์์ ๊ธฐ๋ณธ ํด๋์ค์ private ๋ฐ์ดํฐ ๋ฉค๋ฒ์ ๊ฐ์ ์ป๋๋ก ํจ
-
ํ์ ํด๋์ค์์ ์ฌ์ ์๋(override๋) ๋ฉค๋ฒ ํจ์์ ํธ์ถ ๋ฐฉ์
-
ํ์ ํด๋์ค ํจ์ ๋ด๋ถ์์๋ ๊ทธ ํจ์ ์ด๋ฆ๋ง์ผ๋ก๋ ์ฌ์ ์๋ ํ์ ํด๋์ค์ ๋ฉค๋ฒ ํจ์๋ฅผ ํธ์ถํจ
-
๋ฐ๋ผ์, ๊ฐ์ ์ด๋ฆ์ ๊ธฐ๋ณธ ํด๋์ค์ ๋ฉค๋ฒ ํจ์๋ฅผ ํธ์ถํ๊ธฐ ์ํด์๋
๊ธฐ๋ณธ ํด๋์ค์ด๋ฆ::ํจ์ ์ด๋ฆ
๊ณผ ๊ฐ์ด ํธ์ถํด์ผ ํจ
#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();
}
#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();
}
#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;
}
- ๊ธฐ๋ณธ ํด๋์ค์ ์์ฑ์์ ์๋ฉธ์๋ ํ์ํด๋์ค์ ์์๋์ง ์๋๋ค.
- ํ์ ํด๋์ค ๊ฐ์ฒด์ ์์ฑ ๊ณผ์
- ์ฐ์์ ์ธ ์์ฑ์ ํธ์ถ/์คํ/๋ฆฌํด ๋งค์ปค๋์ฆ
- ํ์ํด๋์ค ์์ฑ์๋ ๊ธฐ๋ณธ ํด๋์ค ์์ฑ์๋ฅผ ํธ์ถํ๋ค.
- ๋ช ์์ (explicitly) ํน์ ์์์ (implicitly)
- ์ต์์ ๋จ๊ณ์ ๊ธฐ๋ณธ ํด๋์ค
- ๋ง์ง๋ง์ผ๋ก ์์ฑ์๊ฐ ํธ์ถ๋จ
- ์ฒ์์ผ๋ก ์์ฑ์ ์คํ์ ๋ง์น๊ณ ๋ฆฌํด
- CommissionEmployee/BasePlusCommissionEmployee ๊ณ์ธต
- CommissionEmployee: ๋ง์ง๋ง์ ์์ฑ์ ํธ์ถํ๊ณ ์ฒ์์ผ๋ก ์์ฑ์ ์คํ์ ๋ง์นจ
- ๋ฐ์ดํฐ ๋ฉค๋ฒ ์ด๊ธฐํ
- ๊ฐ ๊ธฐ๋ณธ ํด๋์ค์ ์์ฑ์๋ ํ์ ํด๋์ค์ member initializer์์ ํธ์ถ๋์ด ์์ ์ ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ฅผ ์ด๊ธฐํํ๋ค.
- ํ์ํด๋์ค ์์ฑ์๋ ๊ธฐ๋ณธ ํด๋์ค ์์ฑ์๋ฅผ ํธ์ถํ๋ค.
- ์ฐ์์ ์ธ ์์ฑ์ ํธ์ถ/์คํ/๋ฆฌํด ๋งค์ปค๋์ฆ
- ์ฐ์์ ์๋ฉธ์ ํธ์ถ
- ์ฐ์์ ์์ฑ์ ๊ตฌ์กฐ์ ๋ฐ๋ (ํจ์์ body์ ์คํ์์)
- ํ์ ํด๋์ค์ ์๋ฉธ์๊ฐ ์ฒ์ ํธ์ถ ๋ฐ ์คํ
- ๊ทธ ๋ค์ ์์ ๊ธฐ๋ณธ ํด๋์ค ์๋ฉธ์ ํธ์ถ ๋ฐ ์คํ
- ์ต์ข ๊ธฐ๋ณธ ํด๋์ค์ ๋์ฐฉํ ๋๊น์ง ๊ณ์
- ๋ฆฌํด ์์๋ ๊ธฐ๋ณธ ํด๋์ค -> ํ์ ํด๋์ค์ ์์
- ๊ฐ์ฅ ํ์์ ํ์ ํด๋์ค ์๋ฉธ์๊ฐ ์ต์ข ์ ์ผ๋ก ๋ฆฌํดํ๋ฉด ๋ฉ๋ชจ๋ฆฌ์์ ๊ฐ์ฒด๊ฐ ์ ๊ฑฐ๋๋ค.
#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();
}
#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();
}
#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
- ๊ธฐ๋ณธ ํด๋์ค public ๋ฉค๋ฒ
- ํ์ ํด๋์ค public ๋ฉค๋ฒ
- ๊ธฐ๋ณธ ํด๋์ค protected ๋ฉค๋ฒ
- ํ์ ํด๋์ค protected ๋ฉค๋ฒ
- ๊ธฐ๋ณธ ํด๋์ค private ๋ฉค๋ฒ๋ ์ง์ ์ ๊ทผ ๋ถ๊ฐ๋ฅ
- ๊ธฐ๋ณธ ํด๋์ค์ public ๋ฉค๋ฒ ํจ์๋ฅผ ํตํด ์ ๊ทผ ๊ฐ๋ฅ
- ๊ธฐ๋ณธ ํด๋์ค public ๊ณผ protected ๋ฉค๋ฒ
- ํ์ ํด๋์ค protected ๋ฉค๋ฒ
- ๊ธฐ๋ณธ ํด๋์ค public ๊ณผ protected ๋ฉค๋ฒ
- ํ์ ํด๋์ค private ๋ฉค๋ฒ