实验4 类中数据的共享与保护.docVIP

  • 91
  • 0
  • 约2.94千字
  • 约 15页
  • 2016-12-28 发布于贵州
  • 举报
实验4 类中数据的共享与保护 一、实验目的与实验要求 (1)掌握友元的定义和应用(2)掌握对象数组的定义、初始化方法及应用。 (3)掌握指针和引用作为函数参数的应用。 掌握。 ()。 程序, ①Circle类中定义4个数据成员:常数据成员PI代表圆周率,静态数据成员count用于统计圆对象的个数,普通的double型数据成员r代表圆的半径,普通的double型数据成员area代表圆的面积,所有数据成员均定义为私有属性。再定义相关的成员函数,用于求单个圆的面积、输出圆的半径及面积、获取静态数据成员的值。 ② 主函数中以一维对象数组定义若干个圆类的对象,调用相应的函数,求面积,并输出每个圆的半径及对应的面积,并且输出一次圆的个数。 。 程序改错,请修改下列程序,尽量减少增行或减行,使程序的运行结果如下: 要求:类中的数据成员连同访问属性均不可以修改。 //错误程序源代码如下: #include iostream using namespace std; class Student { private: char name[20]; static int total; //用来统计学生总人数 public: Student( ) { total++; } ~Student( ) { } Student(char *p=Wang); static int GetTotal( ); }; static int Student::total=0; Student::Student(char *p=Wang) { strcpy(name,p); total++; } static int Student::GetTotal( ) { return total; } int main() { coutThe number of all students: Student::totalendl; Student *p=new Student(Li); coutThe number of all students: p-GetTotal( )endl; delete p; coutThe number of all students: Student::totalendl; Student s[2]; coutThe number of all students: s[0].totalendl; coutThe number of all students: s[1].totalendl; return 0; } #include iostream using namespace std; class Student { private: char name[20]; static int total; public: Student() { total++; } ~Student() { total--; } Student(char*p=Wang); static int GetTotal(); }; int Student::total=0; Student::Student(char*p) { strcpy(name,p); total++; } int Student::GetTotal() { return total; } int main() { coutThe number of all students:Student::GetTotal()endl; Student *p=new Student(Li); coutThe number of all students:p-GetTotal( )endl; delete p; coutThe number of all students:p-GetTotal()/*Student::total*/endl; Student s[2]; coutThe number of all students:s[0].GetTotal()endl; coutThe number of all students:s[1].GetTo

文档评论(0)

1亿VIP精品文档

相关文档