首页 / C++ / C++ 的重要特性展示
C++ 的重要特性展示
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ 的重要特性展示,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2944字,纯文字阅读大概需要5分钟。
内容图文
![C++ 的重要特性展示](/upload/InfoBanner/zyjiaocheng/852/42dd491506fd4d97a765b35ac18408fb.jpg)
比较懒,直接代码展示吧
如下代码展示C++ 的特性。
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/types.h>
/*C++ 覆盖,隐藏, 重载, 拷贝构造, 赋值函数特性例子*/
class Iclass{
public:
Iclass(){}
Iclass(int a, int b){
ad =a;
bc =b;
}
virtual ~Iclass(){}
virtual int getbc(){
printf("Iclass getbc\n");
return bc;
}
int printff(char *msg){
printf("printff%s\n",msg);
return 0;
}
int printff( char *msg, char *msg1){
printf("printff%s %s\n",msg, msg1);
return 0;
}
int hide(int a, int b){
printf("hide%d %d\n",a,b);
return 0;
}
Iclass(Iclass &other){
ad = other.ad;
bc = other.bc;
}
Iclass& operator=(Iclass &other){
if(this == &other){
return *this;
}
ad = other.ad;
bc = other.bc;
return *this;
}
int getad(){
printf("getad :%d\n", ad);
return ad;
}
private:
int ad;
int bc;
};
class IclassEx : public Iclass{
public:
IclassEx(){}
IclassEx(int a, int b, int c):
Iclass(a,b){
cd = c;
}
~IclassEx(){}
virtual int getbc(){
printf("IclassEx getbc\n");
return Iclass::getbc();
}
int hide(int a, int b){
printf("hide%d %d\n",a,b);
printf("hide%d %d\n",a,b);
return 0;
}
IclassEx(IclassEx & other):
Iclass(other){
cd = other.cd;
}
IclassEx& operator=(IclassEx &other){
if(this == &other){
return *this;
}
Iclass::operator=(other);
cd = other.cd;
return *this;
}
private:
int cd;
};
/*C++ 模板函数*/
template <typename T, typename D>
int templatefun(T a, D b){
return 0;
}
/*C++ 模板类*/
/*C++ 虚拟继承*/
int main(int argc, char** argv)
{
int ret = 0;
Iclass* pthis = NULL;
IclassEx* pthisEx = NULL;
IclassEx a(1,2,3);
pthis = &a;
pthis->getbc(); //覆盖也就是所谓多态
pthis->hide(5,6); //pthis 属于Iclass 类型调用的是基类的hide函数
pthis->printff("hello"); //重载
pthis->printff("hello", "world"); //重载
IclassEx b(a); //拷贝构造
pthis = &b;
pthis->getbc(); //覆盖也就是所谓多态
pthis->hide(5,6); //pthis 属于Iclass 类型调用的是基类的hide函数
pthisEx = &b;
pthisEx->hide(5,6); //隐藏特性, 派生类函数继承了基类的hide函数但是该接口不是virtual的, 派生类直接隐藏基类接口(当做看不见)调用自己的hide函数。
pthis = &b;
pthis->getad();
IclassEx c(7,8,9);
c= b; //赋值操作
pthis = &c;
pthis->getbc();
pthis->hide(5,6);
pthis->getad();
while(1){
sleep(10);
}
return 0;
}
//未完待续, 有时候需要重新看下基础特性知识
内容总结
以上是互联网集市为您收集整理的C++ 的重要特性展示全部内容,希望文章能够帮你解决C++ 的重要特性展示所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。