当前位置:文档之家› 实验四 类和对象

实验四 类和对象


"<<"2--> 卖 出
学长只能帮你到这了
问题一:以上程序编译能通过吗,为什么? 答:不能通过,this->age = age;这一句访问了类中的私有成员;最后一行中不能直接 访问类中的私有成员。最后一句中不能直接访问私有静态成员。 问题二:成员变量 nTotalObj 在程序中起什么作用,它是如何实现的? 答:起到计数的作用。根据创建了多少个对象来计数。创建一个对象就计数一个。 问题三:如果规定该程序的主函数和类 CStudent 中的成员变量的属性不允许改变,应该如 何改正该程序? 答:this->age = age;改成 age=age,在类中再创建一个函数,把计数结果传回主函
要想学习好,努力少不了
C++面向对象程序设计实验指导书
实验四
void CTeacher::SetStuAge(int a) { stu.age = a; } void main() { CStudent stu("张三",25); CTeacher tea("李四",26); }
问题一:以上程序有两大错误,试指出来,并改正之?
#include <iostream.h> #include <string.h> class CStudent { public: CStudent(char *n, int a); ~CStudent();
要想学习好,努力少不了
C++面向对象程序设计实验指导书
实验四
private: char *name; int age; }; CStudent::CStudent(char *n, int a) :age(a) { int nLen = strlen(n); name = new char[nLen+1]; strcpy(name,n); name[nLen] = '\0'; } CStudent::~CStudent() { delete[] name; } class CTeacher { public: CTeacher(char *tn, int ta); ~CTeacher(); void SetStuAge(int a); private: char *name; int age; CStudent stu;
3.理解下面的程序,并在 VC++6.0 下运行查看结果,回答程序后面的问题。
#include "iostream.h" class Date { private: const int year; const int month; const int day; public: Date(int y,int m,int d); void showdate() const; }; Date::Date(int y,int m,int d):year(y),month(m),day(d) { } void Date::showdate()const { cout<<year<<"/"<<month<<"/"<<day<<endl; } void main() { const Date obj(2007,10,30); obj.showdate();
#include <iostream.h> #include <string.h> class CStudent { public: CStudent(char *n, int a); ~CStudent(); static void SetAge(int age); int count(int b){ b=nTotalObj ;return b;} private: char *name; int age; static int nTotalObj; }; int CStudent::nTotalObj = 0; CStudent::CStudent(char *n, int a) :age(a) { int nLen = strlen(n); name = new char[nLen+1]; strcpy(name,n);
学长只能帮你到这了
要想学习好,努力少不了
C++面向对象程序设计实验指导书
实验四
}
问题一:以上程序有两大错误,试指出来,并改正之(要求主函数和类 Date 的数据成员属 性不可以改动)?
1 Date::Date(int y,int m,int d)函数中是不能允许直接地更改常对象的数据成员 答: ○
学长只能帮你到这了
要想学序设计实验指导书
实验四
cout<<"卖出总重量为:"<<a*b<<endl; n=n-a*b; if(n<0) cout<<"---------库存不够啦,请先购进-------"<<endl; else cout<<"现在库存总重量为:"<<n<<endl; } void main() { int x,y=1; cout<<"请输入原有库存总量"<<endl; cin>>x; store p(x); while(y) { cout<<" 请 选 择 操 作 类 型 :0--> 退 出 "<<" "<<"1--> 购 进 "<<" "<<endl; cin>>y; switch(y) { case 0:break; case 1:p.in();break; case 2:p.out();break; } } }
1 构造函数CStudent::CStudent(char *n, int a) 答:○
:age(a)参数的初始化错误应该是CTeacher::CTeacher(char *tn, int ta,char *a1,int a2):age(ta),stu(a1,a2),同时把CTeacher这个函数的参数也该成相应CTeacher(char *tn, int ta,char *a1,int a2),主函数的调用,参数传递也是。 2 void CTeacher::SetStuAge(int a){ stu.age = a;}这个函数不能直接访问类的私 ○ 有成员,把stu中的age改成公有成员即可。
学长只能帮你到这了
}; CTeacher::CTeacher(char *tn, int ta) :age(ta) { int nLen = strlen(tn); name = new char[nLen+1]; strcpy(name,tn); name[nLen] = '\0'; } CTeacher::~CTeacher() { delete[] name; }
要想学习好,努力少不了
C++面向对象程序设计实验指导书
实验四
实验四 类和对象—对象传递与静态成员
4.1 实验目的
1.理解静态成员(静态数据成员、静态成员函数)的作用与使用; 2.理解友元(友元函数、友元类)的作用于使用; 3.理解常类型的使用。
4.2 实验内容 4.2.1 程序阅读
1.理解下面的程序,并在 VC++6.0 下运行查看结果,回答程序后面的问题。
学长只能帮你到这了
要想学习好,努力少不了
C++面向对象程序设计实验指导书
实验四
name[nLen] = '\0'; nTotalObj++; } CStudent::~CStudent() { delete[] name; nTotalObj--; } void CStudent::SetAge(int age) { this->age = age; } void main() { CStudent stu1("张三",25); CStudent str2("李四",26); cout<<"CStudent::nTotalObj="<<CStudent::nTotalObj<<endl; }
学长只能帮你到这了
数即可:。。。。static int count(int b){ b=nTotalObj ;return b;} 。。。。cout<<"CStudent::nTotalObj="<<CStudent::count(0)<<endl;
2.理解下面的程序,并在 VC++6.0 下运行查看结果,回答程序后面的问题。
的。改成Date::Date(int y,int m,int d):year(y),month(m),day(d)。 2 obj.showdate();, ○ 不允许常对象调用普通的成员函数。 在成员函数后面加上const 变成常成员函数即可。
4.2.2 程序设计
1.某商店经销一种货物。货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的 重量不一样,因此,商店需要记录下目录库存的总重量。试用 C++模拟商店货物购进和卖出 的情况。 #include<iostream.h> class store { int n; public: store(int); void in(); void out(); }; store::store(int k=0) { n=k; } void store::in() { int a,b; cout<<"请输入购进的规格和数量"<<endl<<"规格(kg):"; cin>>a; cout<<"数量(箱):"; cin>>b; cout<<"购进总重量为:"<<a*b<<endl; n=n+a*b; cout<<"现在库存总重量为:"<<n<<endl; } void store::out() { int a,b; cout<<"请输入卖出的规格和数量"<<endl<<"规格(kg):"; cin>>a; cout<<"数量(箱):"; cin>>b;
相关主题