c++实践参考:存储班长信息的学生类
2016-06-11 19:07
351 查看
/* *Copyright(c)2016,烟台大学计算机学院 *All right reserved. *文件名称:test.cpp *作者:黄金婵 *完成日期:2016年6月11日 *版本号:v1.0 * *问题描述:请完成类中成员函数的定义 *输入描述: *程序输出: */ #include <iostream> #include <string> using namespace std; class Stu { public: Stu(int n, string nam ); void display( ); protected: int num; string name; }; Stu::Stu(int n, string nam ): num(n),name(nam){} void Stu::display( ) { cout<<"学号: "<<num<<endl; cout<<"姓名: "<<name<<endl; } class StuDetail: public Stu { public: StuDetail(int n, string nam,int a, string ad,int n1, string nam1); void show( ); void show_monitor( ); private: Stu monitor; int age; string addr; }; StuDetail::StuDetail(int n, string nam,int a,string ad,int n1,string nam1): Stu(n,nam),monitor(n1,nam1),age(a),addr(ad){} void StuDetail::show( ) { cout<<"学生信息:"<<endl; cout<<"学号: "<<num<<endl; cout<<"姓名: "<<name<<endl; cout<<"年龄: "<<age<<endl; cout<<"住址: "<<addr<<endl<<endl; } void StuDetail::show_monitor( ) { cout<<"班长信息:"<<endl; monitor.display( ); } int main( ) { StuDetail s(10010,"Wang-li",19,"115 Beijing Road,Shanghai",10001,"Li-sun"); s.show( ); s.show_monitor(); return 0; }
知识点总结:
无论是派生类的成员还是派生类的对象都无法直接访问基类的私有成员。
相关文章推荐
- C++中typename关键字的使用方法和注意事项
- c++单链表【构造函数、运算符重载、析构函数、增删查改等】
- c++双链表【构造函数、运算符重载、析构函数、增删查改及逆置等】
- c++顺序表【构造函数、运算符重载、析构函数、增删查改等】
- C++用模板实现顺序表和栈
- C++用模板实现双链表和队列
- 【C++】智能指针shared_ptr 定位删除器(仿函数)
- 浅析c++异常
- C++之虚继承
- C语言 在屏幕上输出菱形图案
- C语言 求出100~999之间的所有“水仙花数”并输出
- C语言:编写strcpy函数
- C语言打印100 ——200之间的素数
- C语言判断1000—2000年之间的闰年
- C语言中不允许创建临时变量,交换两个数的内容
- C语言中写一个函数返回参数二进制中 1 的个数
- C语言求两个数中最大公约数
- 第14周项目3--数组类模板
- T2:滑雪(cowski.pas/c/cpp)
- notepad++搭建C语言环境(Dev c++)