好文档 - 专业文书写作范文服务资料分享网站

用C++解决问题第十版Chapter 15 INHERITANCE

天下 分享 时间: 加入收藏 我要投稿 点赞

Chapter 15

INHERITANCE

1. Solutions to and Remarks on Selected Practice Programs and Programming Projects

Practice Program 1: Derive class Administrator from class SalariedEmployee Remarks:

Ideally the member functions in the base class Employee that are going to be declared in derived classes should be declared \This results in the compiler forcing any directly derived function to define a member with this signature, which, presumably is better suited than any base class member function will be. Print check may well be different for each derived class. The member function void give_raise(double) could be different as well.

Regarding the function that reads the administrator's data, note that a new administrator's data will be entered in response to the base class Employee and derive class SalariedEmployee inquiries, then the administrator class function will make inquiry. My design and implementation follow: Title: Derive class Administrator

Write a program using classes from Display 15.2 - 15.5

Define class Administrator which derives publicly from class SalariedEmployee. Explain why there is a need to change private: to protected:

Answer: To allow the member functions inherited class member to see these data members. Additional protected members required are: string title

1

Copyright ? 2024 Pearson Education, Inc.

Savitch

Problem Solving w/ C++, 10e

Instructor’s Resource Guide

Chapter 15

administrator's title (CEO, Director etc.)

string responsibility (production, accounting, personnel) string supervisor (name of immediate supervisor)

double annual_salary (if not already added in the self test exercise)

Additional public members required are: A function to change name of immediate supervisor

void change_supervisor(string new_super);

A function for reading all of a new administrator's data from the keyboard. Note that the data inherited from class Employee will already be fetched by required base class constructors.

class constructors.

void get_admin_data();

function to output administrator's data to the screen void print();

a function to print a check with appropriate notations on the check void print_check()

//file: ch15p1.h

//This is the INTERFACE for class Administrator which //inherits from class Employee and class SalariedEmployee

#ifndef ADMINISTRATOR_H #define ADMINISTRATOR_H

18

Copyright ? 2024 Pearson Education, Inc.

Savitch

Problem Solving w/ C++, 10e

Instructor’s Resource Guide

Chapter 15

#include \namespace employeessavitch {

class Administrator : public SalariedEmployee { public:

Administrator();

Administrator(string aTitle, string aResponsibilty, string aSupervisor);

// change name of immediate supervisor void change_supervisor(string new_super);

// Read a new administrator's data from the keyboard. void get_admin_data();

//outputs administrator's data to the screen void print();

// prints a check with appropriate notations on the check void print_check();

void give_raise(double amount); protected:

string title; // administrator's title

//(CEO, Director, Vice President)

string responsibility;//production, accounting, personnel string supervisor ; //name of immediate supervisor long double annual_salary; // if not added in the self //test exercise

18

Copyright ? 2024 Pearson Education, Inc.

Savitch

Problem Solving w/ C++, 10e

Instructor’s Resource Guide

Chapter 15

}; } #endif

//File: ch15p1.cc

//Chapter 15 Practice Program 1

//Implementation for the Administrator derived class

#include \

#include \#include namespace employeesavitch { void

Administrator::give_raise(double amount) {

annual_salary += amount; }

Administrator::

Administrator() : SalariedEmployee() {

using namespace std;

cout << \ <<\ get_admin_data(); }

Administrator::

Administrator(string aTitle,

18

Copyright ? 2024 Pearson Education, Inc.

Savitch

Problem Solving w/ C++, 10e

Instructor’s Resource Guide

Chapter 15

string aResponsibilty, string aSupervisor )

: SalariedEmployee(), title( aTitle ),

responsibility(aResponsibilty ), supervisor( aSupervisor ) {

//deliberately empty }

// change name of immediate supervisor

void Administrator::change_supervisor(string new_super) {

supervisor = new_super; }

//Reading all of a new administrator's data from the //keyboard.

void Administrator::get_admin_data() {

using namespace std;

cout << \ getline(cin, title);

cout << \ << \ getline(cin, responsibility);

cout << \

18

Copyright ? 2024 Pearson Education, Inc.

用C++解决问题第十版Chapter 15 INHERITANCE

Chapter15INHERITANCE1.SolutionstoandRemarksonSelectedPracticeProgramsandProgrammingProjectsPracticeProgram1:DeriveclassAdministratorfromclassSal
推荐度:
点击下载文档文档为doc格式
1gcqp1007l47ty70kclt55mbv23ri50057m
领取福利

微信扫码领取福利

微信扫码分享