【解决问题】
1.如何声明基类和派生类的关系
2.如何在派生类中调用基类中的数据成员(两种方式)
3.如何在派生类中调用基类中的函数
【问题描述】
通过一个雇员的继承层次结构来讨论基类与派生类之间的关系。这个层次结构包含了一个公司的工资发放系统所涉及到的多种类型的雇员。佣金雇员(commission employee)将被表示为基类的对象,它们的薪水完全是销售提成;带底薪佣金雇员(base-salaried commission employee)将被表示为派生类的对象,他们的薪水由底薪和销售提成组成。
【输出样例】
Employee information obtained by get functions:
First name is Bob
Last name is Lewis
Social security number is 333-33-3333
Gross sales is 5000.00
Commission rate is 0.04
Base salary is 300.00
Updated employee information output by print function:
base-salaried commission employee: Bob Lewis
social security number: 333-33-3333
gross sales: 5000.00
commission rate: 0.04
base salary: 1000.00
Employee's earnings: $1200.00
【代码】:
【类BasePlusCommissionEmployee的实现文件】
// Lab 2: BasePlusCommissionEmployee.cpp
// Member-function definitions of class BasePlusCommissionEmployee
// using composition.
#include <iostream>
using namespace std;
// BasePlusCommissionEmployee class definition
#include "BasePlusCommissionEmployee.h"
// constructor
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary )
// initialize composed object
: CommissionEmployee( first,last,ssn,sales,rate)//初始化(填充的代码)
{
setBaseSalary( salary ); // validate and store base salary
} // end BasePlusCommissionEmployee constructor
// set commission employee's first name
void BasePlusCommissionEmployee::setFirstName( const string &first )
{
//调用基类中的first的设置函数
CommissionEmployee::setFirstName(first); /* Call commissionEmployee's setFirstName function */
} // end function setFirstName
// return commission employee's first name
string BasePlusCommissionEmployee::getFirstName() const
{
//调用基类中FirstName的获取函数
return CommissionEmployee::getFirstName();/* Call commissionEmployee's getFirstName function */
} // end function getFirstName
// set commission employee's last name
void BasePlusCommissionEmployee::setLastName( const string &last )
{
//调用基类中LastName的设置函数
CommissionEmployee::setLastName(last);/* Call commissionEmployee's setLastName function */
} // end function setLastName
// return commission employee's last name
string BasePlusCommissionEmployee::getLastName() const
{
//调用基类LastName的获取函数
return CommissionEmployee::getLastName();/* Call commissionEmployee's getLastName function */
} // end function getLastName
// set commission employee's social security number
void BasePlusCommissionEmployee::setSocialSecurityNumber(
const string &ssn )
{
CommissionEmployee::setSocialSecurityNumber(ssn);/* Call commissionEmployee's setSocialSecurity function */
} // end function setSocialSecurityNumber
// return commission employee's social security number
string BasePlusCommissionEmployee::getSocialSecurityNumber() const
{
//调用基类的SocialSeucrityNumber获取函数
return CommissionEmployee::getSocialSecurityNumber();/* Call commissionEmployee's getSocialSecurity function */
} // end function getSocialSecurityNumber
// set commission employee's gross sales amount
void BasePlusCommissionEmployee::setGrossSales( double sales )
{
CommissionEmployee::setGrossSales(sales); /* Call commissionEmployee's setGrossSales function */
} // end function setGrossSales
// return commission employee's gross sales amount
double BasePlusCommissionEmployee::getGrossSales() const
{
//调用基类GrossSales的获取函数
return CommissionEmployee::getGrossSales();/* Call commissionEmployee's getGrossSales function */
} // end function getGrossSales
// set commission employee's commission rate
void BasePlusCommissionEmployee::setCommissionRate( double rate )
{
CommissionEmployee::setCommissionRate(rate);/* Call commissionEmployee's setCommissionRate function */
} // end function setCommissionRate
// return commission employee's commission rate
double BasePlusCommissionEmployee::getCommissionRate() const
{
//调用基类CommissionRate的获取函数
return CommissionEmployee::getCommissionRate();/* Call commissionEmployee's getCommissionRate function */
} // end function getCommissionRate
// set base salary
void BasePlusCommissionEmployee::setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
} // end function setBaseSalary
// return base salary
double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
} // end function getBaseSalary
// calculate earnings
double BasePlusCommissionEmployee::earnings() const
{
return getBaseSalary() + CommissionEmployee::earnings();//调用基类中的earnings函数
/* Call commissionEmployee's earnings function */;
} // end function earnings
// print BasePlusCommissionEmployee object
void BasePlusCommissionEmployee::print() const//打印函数
{
cout << "base-salaried ";
// invoke composed CommissionEmployee object's print function
CommissionEmployee::print();/* Call commissionEmployee's print function */ //调用基类中的打印函数
cout << "\nbase salary: " << getBaseSalary();
} // end function print
【类CommissionEmployee的实现文件】
// Lab 2: CommissionEmployee.cpp
// Class CommissionEmployee member-function definitions.
#include <iostream>
using namespace std;
#include "CommissionEmployee.h" // CommissionEmployee class definition
// constructor
CommissionEmployee::CommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate )
{
firstName = first; // should validate
lastName = last; // should validate
socialSecurityNumber = ssn; // should validate
setGrossSales( sales ); // validate and store gross sales
setCommissionRate( rate ); // validate and store commission rate
} // end CommissionEmployee constructor
// set first name
void CommissionEmployee::setFirstName( const string &first )
{
firstName = first; // should validate
} // end function setFirstName
// return first name
string CommissionEmployee::getFirstName() const
{
return firstName;
} // end function getFirstName
// set last name
void CommissionEmployee::setLastName( const string &last )
{
lastName = last; // should validate
} // end function setLastName
// return last name
string CommissionEmployee::getLastName() const
{
return lastName;
} // end function getLastName
// set social security number
void CommissionEmployee::setSocialSecurityNumber( const string &ssn )
{
socialSecurityNumber = ssn; // should validate
} // end function setSocialSecurityNumber
// return social security number
string CommissionEmployee::getSocialSecurityNumber() const
{
return socialSecurityNumber;
} // end function getSocialSecurityNumber
// set gross sales amount
void CommissionEmployee::setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
} // end function setGrossSales
// return gross sales amount
double CommissionEmployee::getGrossSales() const
{
return grossSales;
} // end function getGrossSales
// set commission rate
void CommissionEmployee::setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
} // end function setCommissionRate
// return commission rate
double CommissionEmployee::getCommissionRate() const
{
return commissionRate;
} // end function getCommissionRate
// calculate earnings
double CommissionEmployee::earnings() const
{
return commissionRate * grossSales;
} // end function earnings
// print CommissionEmployee object
void CommissionEmployee::print() const
{
cout << "commission employee: " << firstName << ' ' << lastName
<< "\nsocial security number: " << socialSecurityNumber
<< "\ngross sales: " << grossSales
<< "\ncommission rate: " << commissionRate;
} // end function print
【类CommissionEmployee的测试程序】
// Lab 2: composition.cpp
// Testing class BasePlusCommissionEmployee.
#include <iostream>
#include <iomanip>
using namespace std;
// BasePlusCommissionEmployee class definition
#include "BasePlusCommissionEmployee.h"
int main()
{
// instantiate BasePlusCommissionEmployee object
BasePlusCommissionEmployee
employee( "Bob", "Lewis", "333-33-3333", 5000, .04, 300 );
// set floating-point output formatting
cout << fixed << setprecision( 2 );
// get commission employee data
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()
<< "\nBase salary is " << employee.getBaseSalary() << endl;
employee.setBaseSalary( 1000 ); // set base salary
cout << "\nUpdated employee information output by print function: \n"
<< endl;
employee.print(); // display the new employee information
// display the employee's earnings
cout << "\n\nEmployee's earnings: $" << employee.earnings() << endl;
} // end main
【类BasePlusCommissionEmployee的定义】
// Lab 2: BasePlusCommissionEmployee.h
// BasePlusCommissionEmployee class using composition.
#ifndef BASEPLUS_H
#define BASEPLUS_H
#include <string> // C++ standard string class
using namespace std;
#include "CommissionEmployee.h" // CommissionEmployee class definition
class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
BasePlusCommissionEmployee( const string &, const string &,
const string &, double = 0.0, double = 0.0, double = 0.0 );
void setFirstName( const string & ); // set first name
string getFirstName() const; // return first name
void setLastName( const string & ); // set last name
string getLastName() const; // return last name
void setSocialSecurityNumber( const string & ); // set SSN
string getSocialSecurityNumber() const; // return SSN
void setGrossSales( double ); // set gross sales amount
double getGrossSales() const; // return gross sales amount
void setCommissionRate( double ); // set commission rate
double getCommissionRate() const; // return commission rate
void setBaseSalary( double ); // set base salary
double getBaseSalary() const; // return base salary
double earnings() const; // calculate earnings
void print() const; // print BasePlusCommissionEmployee object
private:
double baseSalary; // base salary //声明基础薪资变量
}; // end class BasePlusCommissionEmployee
#endif
【类CommissionEmployee的定义】
// Lab 2: CommissionEmployee.h
// CommissionEmployee class definition represents a commission employee.
#ifndef COMMISSION_H
#define COMMISSION_H
#include <string> // C++ standard string class
using namespace std;
class CommissionEmployee
{
public:
CommissionEmployee( const string &, const string &, const string &,
double = 0.0, double = 0.0 );
void setFirstName( const string & ); // set first name
string getFirstName() const; // return first name
void setLastName( const string & ); // set last name
string getLastName() const; // return last name
void setSocialSecurityNumber( const string & ); // set SSN
string getSocialSecurityNumber() const; // return SSN
void setGrossSales( double ); // set gross sales amount
double getGrossSales() const; // return gross sales amount
void setCommissionRate( double ); // set commission rate (percentage)
double getCommissionRate() const; // return commission rate
double earnings() const; // calculate earnings
void print() const; // print CommissionEmployee object
private:
string firstName;
string lastName;
string socialSecurityNumber;
double grossSales; // gross weekly sales
double commissionRate; // commission percentage
}; // end class CommissionEmployee
#endif
【解答】
1.在类BasePlusCommissionEmployee的实现文件中,通过基类类名+二元解析运算符的方法调用了基类中的函数。
2.将私有数据成员改为protected,使派生类可以访问基类中的数据成员;或者通过调用该数据成员的get函数,来达到使用该数据成员的目的。