C++中继承使用实例
来源:互联网 发布:java在线订餐系统源码 编辑:程序博客网 时间:2024/05/16 09:42
一、包含
Gloam类包含Frabjous类
头文件14-1.h
#include <iostream>#include <cstring>using namespace std; class Frabjous{private:char fab[20];public: Frabjous(const char *s="c++") // ;fab(s) {} {strncpy(fab,s,20-1);fab[20]='\0';} virtual void tell() {cout<<fab<<endl;} };class Gloam{private: int glip;Frabjous fb;public:Gloam(int g=0,const char*s="C++");Gloam(int g,const Frabjous & f);//Gloam(const Frabjous & f,int g=0);void tell();};
实现文件14-1.c
#include <iostream>#include "14-1.h"using namespace std; Gloam ::Gloam(int g,const char *s){ glip=g; fb=s;}Gloam ::Gloam(int g,const Frabjous &f)//Gloam ::Gloam(const Frabjous &f,int g) { glip=g; fb=f;}void Gloam::tell(){cout<<"glip:"<<glip;cout<<",fb:";fb.tell(); }
应用文件14-1appi.c
#include <iostream>using namespace std;#include "14-1.h"int main(){Frabjous A;A.tell();Gloam B;// Gloam C(A,32);Gloam C(32,A);B.tell();C.tell();return 0;}
g++ -o 14-1.exe 14-1.c 14-1appi.c
结果:
[root@localhost C]# ./14-1.exec++glip:0,fb:C++glip:32,fb:c++[root@localhost C]#
二、 私有继承
头文件14-2.h
#include <iostream>#include <cstring>using namespace std; class Frabjous{ private:char fab[20];public: Frabjous(const char *s="C..") // ;fab(s) {} {strncpy(fab,s,20-1);fab[20]='\0';} virtual void tell() {cout<<fab<<endl;} };class Gloam :private Frabjous{private: int glip;//Frabjous fb;public:Gloam(int g=0,const char*s="C++");Gloam(int g,const Frabjous & f);//Gloam(const Frabjous & f,int g=0);void tell();};
实现文件14-2.c
#include <iostream>#include "14-2.h"using namespace std; Gloam ::Gloam(int g,const char *s):Frabjous(s){ glip=g; //fb=s;}Gloam ::Gloam(int g,const Frabjous &f):Frabjous(f){ glip=g;// fb=f;}void Gloam::tell(){cout<<"glip:"<<glip;cout<<",fab:";Frabjous::tell(); }
应用文件 14-2appi.c
#include <iostream>using namespace std;#include "14-2.h"int main(){Frabjous A;Frabjous A1("wengp");A.tell();A1.tell();Gloam B;// Gloam C(A,32);Gloam C(32,A1);B.tell();C.tell();return 0;}
g++ -o 14-2.exe 14-2.c 14-2appi.c
结果:
[root@localhost C]# ./14-2.exeC..wengpglip:0,fab:C++glip:32,fab:wengp
- C++中继承使用实例
- C++中私有继承实例
- c+中继承的使用
- C#继承使用实例
- objective-c中继承
- 《Effective C#》Item 19:推荐在继承中使用接口
- 《Effective C#》Item 19:推荐在继承中使用接口
- 继承 Objective-C中public、protected、private的使用
- 【c++】实例演示类继承中派生类到基类的转换及虚函数
- 【C++】C++的输入输出、循环、条件、字符串、数组、类、继承的使用实例
- 【C++】C++的输入输出、循环、条件、字符串、数组、类、继承的使用实例
- objective-c : 构造类、继承及实例
- Object-C: 学习实例之继承
- Objective-C - 继承的经典实例
- C#----使用继承选择器创建继承窗体
- C#中类的继承
- c ++中继承问题小结
- C#--继承的基本使用
- OSX “open” command to create new files with an app
- java中的时间计算和格式处理方法
- GCC 编译选项(转)
- JAVA从入门到精通-程序设计基础03
- linux设备驱动归纳总结(四):5.SMP下的竞态和并发
- C++中继承使用实例
- 最全Android UI汇总【绝对有你需要的!!!】
- PHP中的(伪)多线程与多进程
- eclipse的project 名称上有红色感叹号
- 委托和事件
- 宏定义的编译链接
- 数据挖掘中的经典算法(一)汇总--谁说做系统的就不能研究数据挖掘?
- commons-lang包中常用的类的作用
- C++ string函数 与 C字符串处理函数(整理)