Accelerated C++:通过示例进行编程实践——练习解答(第13章)

我的Github地址:https://github.com/lanbeilyj/Accerlerated-C-plus-plus

13-0. Compile, execute, and test the programs in this chapter.

Ans:见Github。

13-1. Annotate the Core and Grad constructors to write the constructor's name and argument list when the constructor is executed. For example, you should add a statement such as

cerr << "Grad::Grad(istream&)" << endl;

to the Grad constructor taking an istream& parameter. Then write a small program that exercises each constructor. Predict beforehand what the output will be. Revise your program and predictions until your predictions match what is actually written.

Ans:在Core和Grad类的构造函数添加相应输出即可。

13-2. Given the Core and Grad classes defined in this chapter, indicate which function is called for each of these invocations:

Core* p1 = new Core;
Core* p2 = new Grad;
Core s1;
Grad s2;

p1->grade();
p1->name();

p2->grade();
p2->name();

s1.grade();
s1.name();

s2.name();
s2.grade();

Check whether you are correct by adding output statements to the name andgrade functions that indicate which function is being executed.

Ans:在Core和Grad类的grade和name中添加输出语句,在main中将上述代码添加即可。

13-3. The class that we built in Chapter 9 included a valid member that allowed users to check whether the object held values for a student record or not. Add that functionality to the inheritance-based system of classes.

Ans:

static bool compare(const Student_info& x,const Student_info& y)
{
        return x.name()<y.name();
}

13-4. Add to these classes a function that will map a numeric grade to a letter grade according to the grading policy outlined in §10.3/177.

Ans:

double grade(double midterm,double fin,double homework)

{

	return grade(0.2*midterm+0.4*fin+0.4*homework);

}
double grade(double midterm,double fin,const vector<double>& hw)

{

	return grade(midterm,fin,median(hw));

}
string grade(double grade)
{
    static const double numbers[]={90,85,75,60,0};
    static const char *letters[]={"A","B","C","D","E"};
    static const size_t ngrades=sizeof(numbers)/sizeof(*numbers);
    for(size_t i=0;i<ngrades;++i)
    {
        if(numbers[i]<=grade)
            return letters[i];
    }
    return "?\?\?";
}

13-5. Write a predicate to check whether a particular student met all the relevant requirements. That is, check whether a student did all the homework, and if a graduate student, whether the student wrote a thesis.

Ans:见Github。

13-6. Add a class to the system to represent students taking the course for pass/fail credit. Assume that such students need not do the homework, but might do so. If they do, the homework should participate in determining whether they passed or failed, according to the normal formula. If they did no homework, then the grade is the average of their midterm and final grades. A passing grade is 60 or higher.

Ans:见Github。

13-7. Add a class to the system to represent students auditing the course.

Ans:见Github。

13-8. Write a program to generate a grade report that can handle all four kinds of students.

Ans:见Github。

13-9. Describe what would happen if the assignment operator in §13.4.2/247 failed to check for self-assignment.

Ans:该句作用判断是否是自我复制,因为如果是自我复制,那么左右操作数都会指向相同的一个对象,那么赋值时先删除做操作数对象并释放所占用的内存也即删除右操作数对象释放其内存,对已经释放内存的对象引用将是未知的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值