C++面向对象程序设计教程(第3版)—-陈维兴,林小茶课后习题答案 联系客服

发布时间 : 星期六 文章C++面向对象程序设计教程(第3版)—-陈维兴,林小茶课后习题答案更新完毕开始阅读

void pintStu();函数只有声明,没有定义。 age是私有成员,不能用对象直接调用。

void printStu() 和 void setSno(int s) 没有加限定符 Student:: void setAge(int a)在类中没有声明

构造函数不能定义为私有。否则无法创建对象。

下面是一个计算器类的定义,请完成该类成员函数的实现。

class counter {

public:

counter(int number);

void increment(); ult() << \ \; } }

修改,通过对象指针访问对象数组,使程序以相反的顺序显示每个对象数组元素的qu*price值。

class book {

private:

int qu, price;

public:

book(int qu); int mult(); };

book::book(int q) {

if(q < 1 || q > 5) {

qu = 1; } else {

qu = q; }

price = 10 * qu; }

int book::mult() {

return qu * price; }

int main() {

book books[5] = {1,2,3,4,5}; book *p = books; p += 4;

for(int i = 0; i < 5; i++) {

cout << p->mult() << \ \; --p; }

return 0; }

构建一个类Stock,含字符数组stockcode[]及整型数组成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、p。当定义Stock的类对象时,将对象的第一个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、price。未设置第2和第3个参数时,quan的值为1000,price的值为.成员函数print没有形参,需使用this指针,显示对象数据成员的内容。编写程序显示对象数据成员的值。

#include using namespace std;

class Stock {

private:

char stockcode[25]; int quan; double price; public:

Stock(char na[], int q = 1000, double p = ; Stock(char na[]); void print(); };

Stock::Stock(char na[], int q = 1000, double p = {

strcpy(stockcode, na); quan = q; price = p; }

void Stock::print() {

cout << \ << this->stockcode << \ << this->quan << \ << this->price << endl; }

int main() {

Stock stock1(\, 3000, ; Stock stock2(\); (); ();

return 0; }

编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。

#include using namespace std;

class student {

private:

char name[25], studentNo[10]; int score; static int sum;

static int totalScore; public:

student(char na[], char stuNo[], int sc); void show();

static void showTotal(); };

student::student(char na[], char stuNo[], int sc) {

strcpy(name, na);

strcpy(studentNo, stuNo); score = sc; ++sum;

totalScore += sc; }

void student::show() {

cout << \姓名: \ << name <

cout << \学号: \ << studentNo << endl; cout << \成绩: \ << score << endl; }

void student::showTotal() {

cout << \总人数: \ << sum << endl;

cout << \平均成绩: \ << (double)totalScore/sum <

int student::sum = 0;

int student::totalScore = 0;

int main() {

student s1(\张无忌\, \, 75);