南京邮电大学程序设计类课程教辅平台c++第四章编程题答案 您所在的位置:网站首页 私有静态数据成员 南京邮电大学程序设计类课程教辅平台c++第四章编程题答案

南京邮电大学程序设计类课程教辅平台c++第四章编程题答案

2023-07-22 09:30| 来源: 网络整理| 查看: 265

第一题.定义一个学生类,有如下基本成员。 (1)私有数据成员: 年龄 int age; 姓名 string name; (2)公有静态数据成员: 学生人数:static int count; (3)公有成员函数 构造函数 带参数的构造函数Student(int m,string n); 不带参数的构造函数Student(); 析构函数:~Student() 输出函数:void Print()const; 主函数的定义及函数的运行结果如下,请完成类的定义及类中各函数的实现代码,补充成一个完整的程序。 提示:Print()函数中的输出语句如下:

coutprivate: int age; string Name; public: Student(int m,string n); Student(); ~Student(); static int count; void Print()const; }; int Student::count=0; Student::Student(int m,string n) {age=m; Name=n; count++; } Student::Student() {age=0; Name="unnamed"; count++; } Student::~Student() {count--; } void Student::Print()const {cout char name[20]="C++"; Book book1(name,23.5,3); strcpy(name,"Data Structure"); Book book2(name,28.8,7); book1.borrow(); book1.display(); book2.restore(); book2.display(); return 0; }

参考程序:

#include #include #include using namespace std; class Book {public: string bookname; double price; int number; Book(string a,double b,int c); int borrow(); int restore(); void display()const; }; Book::Book(string a,double b,int c) {bookname=a; price=b; number=c; } int Book::borrow() {return number--; } int Book::restore() {return number++; } void Book::display()const {cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有