第十二周实验报告任务1

来源:互联网 发布:1元夺宝源码 编辑:程序博客网 时间:2024/04/26 16:07
/* (程序头部注释开始)* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved.* 文件名称:                              * 作    者:王引琳                              * 完成日期:   2012      年   5    月    7    日* 版 本 号:          * 对任务及求解方法的描述部分* 输入描述: * 问题描述: * 程序输出: * 程序头部的注释结束*//*【任务1】理解基类中成员的访问限定符和派生类的继承方式由下面派生类Student1对基类Student的继承……(1)请修改基类中成员的访问限定符和派生类的继承方式,考虑可能的运行结果或可能出现的错误,并在上机时进行验证、对比,达到理解派生类成员的访问属性的目的。(2)总结(1)的结果,将(1)的结果摘要写到报告博文中;最后用自己的话总结确定派生类成员的访问属性的原则,也写到报告博文中。(代码类似P363例11.5,上机准备阶段可以研究这段代码,BB平台中提供实验用代码。)*/原程序:#include <iostream>#include <string>using namespace std;class Student //(1)修改student类中各数据成员和成员函数的访问限定符,并观察发生的现象{public: Student(int n,string nam,char s) ;void show();~Student( ){ }  protected: int num;string name;char sex ; };class Student1: public Student //(2)修改此处的继承方式,并观察发生的现象{ public: Student1(int n,string nam,char s,int a,string ad);void show1( );~Student1( ){ }  private: int age; string addr; };Student::Student(int n,string nam,char s) {num=n;name=nam;sex=s; }void Student::show(){cout<<"num: "<<num<<endl;cout<<"name: "<<name<<endl;cout<<"sex: "<<sex<<endl<<endl;}Student1::Student1(int n,string nam,char s,int a,string ad):Student(n,nam,s) {age=a; addr=ad;}void Student1::show1( ){cout<<"num: "<<num<<endl;cout<<"name: "<<name<<endl;cout<<"sex: "<<sex<<endl;cout<<"age: "<<age<<endl;cout<<"address: "<<addr<<endl<<endl;}int main( ){Student1 stud1(10010,"Wang-li",'f',19,"115 Beijing Road,Shanghai");Student1 stud2(10011,"Zhang-fun",'m',21,"213 Shanghai Road,Beijing");Student stud3(20010,"He-xin",'m');stud1.show1( ); stud2.show( ); stud3.show( ); system ("pause");return 0;}修改程序后:

class Student //(1)修改student类中各数据成员和成员函数的访问限定符,并观察发生的现象{protected:  Student(int n,string nam,char s) ; void show(); ~Student( ){ }  private:  int num; string name; char sex ; };

class Student1: protected Student //(2)修改此处的继承方式,并观察发生的现象{ public:  Student1(int n,string nam,char s,int a,string ad); void show1( ); ~Student1( ){ }  private:  int age;  string addr; };

出错:
1>------ Build started: Project: 1111, Configuration: Debug Win32 ------
1>Compiling...
1>222.cpp
1>e:\1111\1111\222.cpp(51) : error C2248: 'Student::num' : cannot access private member declared in class 'Student'
1>        e:\1111\1111\222.cpp(16) : see declaration of 'Student::num'
1>        e:\1111\1111\222.cpp(10) : see declaration of 'Student'
1>e:\1111\1111\222.cpp(52) : error C2248: 'Student::name' : cannot access private member declared in class 'Student'
1>        e:\1111\1111\222.cpp(17) : see declaration of 'Student::name'
1>        e:\1111\1111\222.cpp(10) : see declaration of 'Student'
1>e:\1111\1111\222.cpp(53) : error C2248: 'Student::sex' : cannot access private member declared in class 'Student'
1>        e:\1111\1111\222.cpp(18) : see declaration of 'Student::sex'
1>        e:\1111\1111\222.cpp(10) : see declaration of 'Student'
1>e:\1111\1111\222.cpp(61) : error C2248: 'Student::Student' : cannot access protected member declared in class 'Student'
1>        e:\1111\1111\222.cpp(12) : see declaration of 'Student::Student'
1>        e:\1111\1111\222.cpp(10) : see declaration of 'Student'
1>e:\1111\1111\222.cpp(61) : error C2248: 'Student::~Student' : cannot access protected member declared in class 'Student'
1>        e:\1111\1111\222.cpp(14) : see declaration of 'Student::~Student'
1>        e:\1111\1111\222.cpp(10) : see declaration of 'Student'
1>e:\1111\1111\222.cpp(63) : error C2248: 'Student::show' : cannot access protected member declared in class 'Student'
1>        e:\1111\1111\222.cpp(13) : see declaration of 'Student::show'
1>        e:\1111\1111\222.cpp(10) : see declaration of 'Student'
1>e:\1111\1111\222.cpp(64) : error C2248: 'Student::show' : cannot access protected member declared in class 'Student'
1>        e:\1111\1111\222.cpp(13) : see declaration of 'Student::show'
1>        e:\1111\1111\222.cpp(10) : see declaration of 'Student'
1>Build log was saved at "file://e:\1111\1111\Debug\BuildLog.htm"
1>1111 - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

上机感言:公共继承中,派生类可访问基类的公共成员和保护成员,而基类的私有成员对派生类来说不可访问。私有成员只能通过基类的成员函数访问。

私有继承中,派生类的成员函数可以访问私有基类的公共成员,但不能访问私有基类的私有成员。

保护继承中,公用的,派生类内和类外均可访问;受保护的和私有的,派生类内可以访问,派生类外不可以访问

 

原创粉丝点击