第一范文网 - 专业文章范例文档资料分享平台

内蒙古科技大学面向对象的程序设计实验6 静态成员和友元

来源:用户分享 时间:2025/7/23 13:08:56 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

尝试测试运行结果

该代码包含题目要求的所有功能,运行完毕

五、实验总结

实验源代码:

练习(一)

//修改之后代码 #include #include class CStudent { public:

CStudent(char *n, int a); ~CStudent();

static void SetAge(CStudentstuN); static int CSnTota10bj();

private: char *name; int age;

static intnTotalObj; };

intCStudent::nTotalObj = 0; CStudent::CStudent(char *n, int a) :age(a) {

intnLen = strlen(n); name = new char[nLen+1]; strcpy(name,n); name[nLen] = ’\\0’;

nTotalObj++; }

CStudent::~CStudent() {

delete[] name; nTotalObj--; }

intCStudent::CSnTota10bj() {

int n; n=nTotalObj; return n; }

void CStudent::SetAge(CStudentstuN) {

int a; stuN.age = a; }

void main() {

CStudent stu1(\张三\ CStudent str2(\李四\

cout<<\ }

练习(二)

#include #include

class CStudent { public:

CStudent(char *n, int a); ~CStudent(); private: char *name; public: int age; };

CStudent::CStudent(char *n, int a) :age(a) {

intnLen = strlen(n);

name = new char[nLen+1]; strcpy(name,n); name[nLen] = ’\\0’; }

CStudent::~CStudent() {

delete[] name; }

class CTeacher { public:

CTeacher(char *tn,intta,char *sn,intsa); ~CTeacher();

void SetStuAge(int a); private: char *name; int age;

CStudentstu; };

CTeacher::CTeacher(char *tn,intta,char *sn=\ :age(ta),stu(sn,sa) {

intnLen = strlen(tn); name = new char[nLen+1]; strcpy(name,tn); name[nLen] = ’\\0’; }

CTeacher::~CTeacher() {

delete[] name;

}

void CTeacher::SetStuAge(int a) {

stu.age = a; }

void main() {

CStudentstu(\张三\ CTeacher tea(\李四\ } /*

错误1 CTeacher类中的成员函数SetStuAge不能直接访问CStudent类内嵌成员stu的数据成员age

错误2 CTeacher类构造函数的初始化含有错误,内嵌对象stu的数据成员也应该包含在内 */

练习(三)

#include class Goods { private: int weight;

static inttotalWeight; public:

Goods(intwei);

static void PrintTotalWeight(); void sell(); void buy(); };

intGoods::totalWeight = 0; Goods::Goods(inttwei) {

totalWeight = twei;

cout<<\初始总重量为:\ }

void Goods::PrintTotalWeight() {

cout<<\总重量为:\

内蒙古科技大学面向对象的程序设计实验6 静态成员和友元.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c41zdt84jea28mwx144xk_4.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top