0
点赞
收藏
分享

微信扫一扫

C++编程练习||1.GradeBook类2.Employee类 题目:创建一个名为Employee(雇员)的类,包括了作为数据成员的3部分信息

1.GradeBook类

  题目: 按如下要求修改GradeBook类。

a) 包括第二个string数据成员,它表示授课教师的名字。

b) 提供一个可以改变教师姓名的设置函数,以及一个可以得到该名字的获取函数。

c) 修改构造函数,它指定了两个形参:一个针对课程名称,一个针对教师名称。

d) 修改成员函数displayMessage,使得它首先输出欢迎信息和课程名称,然后输出”This course is presented by:”,后跟教师姓名。

   输出格式:

   cout << "This course is presented by: " << ...

   Sample Input:

   无

   Sample Output:

   gradeBook instructor name is: Professor Smith

   new gradeBook instructor name is: Assistant Professor Bates

   Welcome to the grade book for

   CS101 Introduction to C++ Programming!

   This course is presented by: Assistant Professor Bates

   注意:上面的每个冒号后面都有一个空格, 倒数第二行尾有叹号!

#include <iostream>
#include <string>
using namespace std;

class GradeBook {
	private:
		string course;
		string tName;
	
	public:
		GradeBook(string c, string n) {
			course = c;
			tName = n;
		}
		
		string getInstructorName() {
			return tName;
		}
		
		void setInstructorName(string n) {
			tName = n;
		}
		
		void displayMessage() {
			cout << "Welcome to the grade book for" << endl;
			cout << course << "!" << endl;
			cout << "This course is presented by: " << tName;
		}
		
};


int main() {
	// create a GradeBook object; pass a course name and instructor name
	GradeBook gradeBook("CS101 Introduction to C++ Programming", "Professor Smith" );

	// display initial value of instructorName of GradeBook object
	cout << "gradeBook instructor name is: " << gradeBook.getInstructorName() << "\n";

	// modify the instructorName using set function
	gradeBook.setInstructorName( "Assistant Professor Bates" );

	// display new value of instructorName
	cout << "new gradeBook instructor name is: " << gradeBook.getInstructorName() << "\n";

	// display welcome message and instructor's name
	gradeBook.displayMessage();
	return 0; // indicate successful termination
} // end main

运行结果:

C++编程练习||1.GradeBook类2.Employee类 题目:创建一个名为Employee(雇员)的类,包括了作为数据成员的3部分信息_数据


2.Employee类

题目:创建一个名为Employee(雇员)的类,包括了作为数据成员的3部分信息:名(类型string)、姓(类型string)、月薪(类型int)。这个类必须具有一个初始化前述的3个数据成员的构造函数。对每个数据成员都提供一个设置函数和一个获取函数。如果月薪是负数,那么应该设置为0。

编写一个演示Employee类性能的测试程序。创建两个Employee对象,显示每个对象的年薪,然后,对每个Employee对象增薪10%,再显示他们的年薪。

#include <iostream>
#include <cstring>

using namespace std;

class Employee {
	private:
		string fn;
		string ln;
		int salary;
		
	public:
		Employee (string a_fn, string a_ln, int a_salary) {
			fn = a_fn;
			ln = a_ln;
			if(a_salary) {
				salary = a_salary; 
			} else {
				salary = 0;
			}
		}
		
		string getFirstName() {
			return fn;
		}
		
		string getLastName() {
			return ln;
		}
		
		int getMonthlySalary() {
			return salary;
		}
		
		void setMonthlySalary(int a_salary) {
			salary = a_salary;
		}
		
	
};

// function main begins program execution
int main() {
	// create two Employee objects
	Employee employee1( "Lisa", "Roberts", 4500 );
	Employee employee2( "Mark", "Stein", 4000 );

	// display each Employee's yearly salary
	cout << "Employees' yearly salaries: " << endl;

	// retrieve and display employee1's monthly salary multiplied by 12
	int monthlySalary1 = employee1.getMonthlySalary();
	cout << employee1.getFirstName() << " " << employee1.getLastName()
	     << ": $" << monthlySalary1 * 12 << endl;

	// retrieve and display employee2's monthly salary multiplied by 12
	int monthlySalary2 = employee2.getMonthlySalary();
	cout << employee2.getFirstName() << " " << employee2.getLastName()
	     << ": $" << monthlySalary2 * 12 << endl;

	// give each Employee a 10% raise
	employee1.setMonthlySalary( monthlySalary1 * 1.1 );
	employee2.setMonthlySalary( monthlySalary2 * 1.1 );

	// display each Employee's yearly salary again
	cout << "\nEmployees' yearly salaries after 10% raise: " << endl;

	// retrieve and display employee1's monthly salary multiplied by 12
	monthlySalary1 = employee1.getMonthlySalary();
	cout << employee1.getFirstName() << " " << employee1.getLastName()
	     << ": $" << monthlySalary1 * 12 << endl;

	monthlySalary2 = employee2.getMonthlySalary();
	cout << employee2.getFirstName() << " " << employee2.getLastName()
	     << ": $" << monthlySalary2 * 12 << endl;
	return 0; // indicate successful termination
} // end main

运行结果:

C++编程练习||1.GradeBook类2.Employee类 题目:创建一个名为Employee(雇员)的类,包括了作为数据成员的3部分信息_C++_02


举报

相关推荐

创建一个Employee类

0 条评论