C++第十二周【任务1】理解基类中成员的访问限定符和派生类的继承方式

时间:2021-04-01 15:07:54

/*
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:C++第十二周【任务1】                             
* 作    者:   李洪悬                             
* 完成日期:   2012  年  5  月  8  日

* 对任务及求解方法的描述部分
* 输入描述:学生的个人信息
* 问题描述:理解和运用基类中成员的访问限定符和派生类的继承方式输出学生的个人信息
* 程序输出:学生的个人信息
*/

【任务1】理解基类中成员的访问限定符和派生类的继承方式由下面派生类Student1对基类Student的继承……(1)请修改基类中成员的访问限定符和派生类的继承方式,考虑可能的运行结果或可能出现的错误,并在上机时进行验证、对比,达到理解派生类成员的访问属性的目的。(2)总结(1)的结果,将(1)的结果摘要写到报告博文中;最后用自己的话总结确定派生类成员的访问属性的原则,也写到报告博文中。(代码类似P363例11.5,上机准备阶段可以研究这段代码,BB平台中提供实验用代码。)
 
BB平台中提供的实验用代码:
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):Student(n,nam,s) ;
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( );
return 0;
}

程序源代码:
#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;
}

程序输出结果:

num: 10010
name: Wang-li
sex: f
age: 19
address: 115 Beijing Road,Shanghai

num: 10011
name: Zhang-fun
sex: m

num: 20010
name: He-xin
sex: m

请按任意键继续. . .


 修改基类中成员的访问限定符和派生类的继承方式:

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

将protected改为private后,报错为'Student::name' : cannot access private member declared in class 'Student'派生类无法访问基类的私有成员;

将public改为protected后,报错为~Student' : cannot access protected member declared in class 'Student'析构函数无法执行;

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;
};

将此处的继承方式public改为private,报错为'Student::show' not accessible because 'Student1' uses 'private' to inherit from 'Student'派生类无法调用基类的函数。

                                                           公用基类的成员                                                                   在公用派生类中的访问属性            

                                                                私有成员                                                                                     不可访问

                                                                共有成员                                                                                       公用 

                                                                保护成员                                                                                       保护   
 

类中的函数在类外不能调用!