以下是笔者在ubuntu系统下编译运行通过的一些反应c++基础特性的例子,包括类,结构体,构造函数,析构函数,函数的重载,this指针,类的继承,虚函数,函数的覆盖和隐藏等。(由于格式问题代码中的乱码是一些文字描述)
/*#include<iostream>
#include<string.h>
using namespace std;
struct Person
{
char name[20];
int num;
};
int main()
{
int i;
int j;
Person leader_name[3]={"li",0,"jia",0,"wang",0};
char leader[20];
for(i=0;i<10;i++)
{
cin>>leader;
for(j=0;j<3;j++)
if(strcmp(leader,leader_name[j].name)==0)
leader_name[j].num++;
}
for(i=0;i<3;i++)
{
cout<<leader_name[i].name<<":"<<leader_name[i].num<<endl;
}
}
杩欎釜鏄叧浜庣粨鏋勪綋鏁扮粍鐨勪緥瀛?************************/
/*
#include <iostream>
using namespace std;
class point
{
public: int x;
public: int y;
public: void out()
{
cout<<x<<y<<endl;
}
};
int main()
{
point pt;
pt.x=0;
pt.y=0;
pt.out();
return 0;
}
杩欎釜鏄竴涓畝鍗曠殑鑷畾涔夌被鐨勫簲鐢?*************************/
/*
#include<iostream>
using namespace std;
class point
{
public:
int x;
int y;
point()//杩欎釜鏄釜鏋勯€犲嚱鏁板畠涓昏瀹屾垚鐨勬槸瀵圭被鐨勬垚鍛樿繘琛屽垵濮嬪寲瀹氫箟
{
x=10;
y=12;
}
void out()
{
cout<<x<<y<<endl;
}
};
int main()
{
point pt;
pt.out();
return 0;
}
鏋勯€犲嚱鏁扮殑绠€鍗曞簲鐢?**************************/
/*********************
#include<iostream>
using namespace std;
class point
{
public:
int x;
int y;
point()//鏋勯€犲嚱鏁?{
x=0;
y=0;
}
point(int a,int b)//鏋勯€犲嚱鏁?锛岃繖涓湁鍙傛暟
{
x=a;
y=b;
}
void out()
{
cout<<x<<endl;
cout<<y<<endl;
}
};
int main()
{
point pt(2,2);
point pt1;
pt.out();
pt1.out();//鏍规嵁涓嶅悓鐨勫畾涔夎皟鐢ㄥ搴旂殑鍑芥暟
}
鍑芥暟鐨勯噸杞界殑绠€鍗曠粌涔?/
/************************************
#include<iostream>
using namespace std;
class point
{
public:
int x;
int y;
point()
{
x=0;
y=0;
}
point(int a,int b)
{
x=a;
y=b;
}
void out()
{
cout<<x<<endl;
cout<<y<<endl;
}
void init(int x, int y)//this鎸囬拡锛屼粬鎬绘槸鎸囧悜瀵硅薄鏈韩锛屼唬琛ㄥ璞$殑鍦板潃
{
x=x;
y=y;
}
};
int main()
{
point pt1(5,5);
point pt;
pt.init(10,10);//铏界劧init鍑芥暟鏈変袱涓弬鏁帮紝鍥犱负this鎸囬拡鐨勫師鍥犲湪璋冪敤鍑芥暟鏃舵槸榛樿璋冪敤瀵硅薄鏈韩鐨勫湴鍧€鐨勩€?pt.out();
pt1.out();
return 0;
}
//this鎸囬拡鐨勭畝鍗曞簲鐢?*???????????????????????????????/????????????????*/
/*******************************************************
#include<iostream>
using namespace std;
class animal
{
public:
void eat()
{
cout<<"鍚冨悆鍚?<<endl;
}
void sleep()
{
cout<<"鐫¤"<<endl;}
void breath()
{
cout<<"鍛煎惛"<<endl;
}
};
class fish:public animal//绫荤殑缁ф壙鎬э紝fish绫荤户鎵夸簡animal杩欎釜绫讳腑鐨勫嚱鏁?{};
int main()
{
animal an;
fish fh;
an.eat();
fh.sleep();
fh.eat();
fh.breath();
return 1;
}
//鍏充簬绫荤殑缁ф壙鎬х殑涓€涓皬渚嬪瓙
**********************************************************/
/*
#include<iostream>
using namespace std;
class animal
{
public:
animal()
{
cout<<"鐖剁被鐨勬瀯閫犲嚱鏁板紑濮?<<endl;
}
~animal()
{
cout<<"鐖剁被鐨勬瀽鏋勫嚱鏁?<<endl;
}
};
class fish:animal
{
public:
fish()
{
cout<<"瀛愮被鏋勯€犲嚱鏁?<<endl;
}
~fish()
{
cout<<"瀛愮被鐨勬瀽鏋勫嚱鏁?<<endl;
}
};
int main()
{
fish fh;
return 0;
}
//瀛愮被鍦ㄨ皟鐢ㄨ嚜宸辩殑鏋勯€犲嚱鏁版椂娴佺▼搴旇鏄厛璋冪敤鐖剁被鐨勬瀯閫犲嚱鏁扮劧鍚庡瓙绫荤殑鏋勯€犲嚱鏁板瓙绫荤殑鏋愭瀯鍑芥暟鏈€鍚庢槸鐖剁被鐨勬瀽鏋勫嚱鏁般€傚浜庣埗绫荤殑鐨勬瀯閫犲嚱鏁板惈鏈夊弬鏁版儏鍐靛瓙绫荤殑鏋勯€犺鏄剧ず鐨勫幓璋冪敤鐖剁被鐨勬瀯閫犲嚱鏁版墠琛屻€?/绫讳腑鐨勬垚鍛樻湁涓夌绫诲瀷public private protect 鍦ㄧ被鐨勭户鎵垮叧绯讳腑锛屽瓙绫诲鏋滄槸浠ublic缁ф壙鐖剁被锛屽熀绫荤殑鎴愬憳浠嶇劧鏄互鍘熸潵鐨勮闂潈闄愬湪瀛愮被涓璁块棶銆傞粯璁ょ殑鎯呭喌鏄槸浠rivate绫诲舰璁块棶鐨勶紝杩欑鎯呭喌鍩虹被鐨勬垚鍛樺皢浠rivate绫诲瀷鍦ㄥ瓙绫讳腑琚闂紝褰撲互public绫诲瀷璁块棶鏃讹紝鍩虹被鐨刾ublic鍜宲rotect鍙橀噺鍦ㄥ瓙绫讳腑閮藉彉鎴愪簡public绫诲瀷鐨勮闂潈闄愩€?//鐖剁被涓殑private绫诲瀷鍙橀噺涓嶈兘琚瓙绫昏闂€?
///
#include<iostream>
using namespace std;
class A
{
public:
void out();
};
class B
{
public:
void out1();
};
void A::out()
{
cout<<"A绫讳腑鐨勫嚱鏁?<<endl;
}
void B::out1()
{
cout<<"B绫荤殑鍑芥暟"<<endl;
}
class C:public A,public B//瀛愮被鍚屾椂缁ф壙涓や釜鐖剁被
{
public:
void show();
};
void C::show()
{
cout<<"瀛愮被涓殑鍑芥暟"<<endl;
}
int main()
{
C c1;
c1.out();
c1.out1();
c1.show();
return 0;
}
//瀛愮被鐨勫閲嶇户鎵夸緥瀛?
#include<iostream>
using namespace std;
class animal
{
public:
void eat()
{cout<<""<<endl;}
void breath()
{cout<<"鍔ㄧ墿鍛煎惛"<<endl;}
};
class fish:public animal
{
public:
void breath()
{cout<<"fish breath"<<endl;}
};
int main()
{
fish fh;
fh.breath();
}
///
//鍏充簬鍑芥暟鐨勮鐩栵紝闅愯棌锛岄噸杞戒箣闂寸殑鍏崇郴锛氬嚱鏁扮殑瑕嗙洊瑕佹眰鐖剁被鍜屽瓙绫荤殑鍑芥暟瀹屽叏涓€鏍峰寘鎷弬鏁版儏鍐碉紝鍚屾椂鍩虹被鍑芥暟瑕佹槸铏氬嚱鏁帮紝涓や釜鍑芥暟涓€涓湪鍩虹被涓€涓湪瀛愮被銆傚嚱鏁扮殑闅愯棌涔熸槸鍦ㄥ熀绫讳笌瀛愮被涓紝涓嶅悓涔嬪鏄熀绫荤殑鍑芥暟涓嶆槸铏氬嚱鏁帮紝鍩虹被鍜屽瓙绫昏涓€鏍凤紙鍙傛暟鍙互蹇界暐锛夎繖绉嶆儏鍐典笅瀛愮被涓殑鍑芥暟灏嗕細鎶婂熀绫讳腑鐨勫嚱鏁伴殣钘忋€傚嚱鏁扮殑閲嶈浇鏄湪涓€涓被涓彲浠ユ湁澶氫釜鍑芥暟鍚嶄竴鏍风殑鍑芥暟瀛樺湪銆?*/
#include<iostream>
using namespace std;
class A
{
public:
virtual void fn(int i)
{
cout<<"鍩虹被铏氬嚱鏁?<<endl;
}
void yfn(int f)
{
cout<<"鍩虹被鍑芥暟"<<endl;
}
};
class B:public A
{
public:
void fn(int i)
{
cout<<"瀛愮被鍑芥暟"<<endl;//瑕嗙洊浜嗗熀绫荤殑铏氬嚱鏁?}
void yfn(int c)
{
cout<<"瀛愮被鍑芥暟1"<<endl;//闅愯棌浜嗗熀绫荤殑鍑芥暟
}
};
int main()
{
B b;//瀹氫箟涓€涓瓙绫诲彉閲?A *pa=&b;//瀹氫箟涓€涓熀绫荤殑鎸囬拡鎸囧悜瀛愮被鐨勫湴鍧€
B *pb=&b;//瀹氫箟涓€涓瓙绫荤殑鎸囬拡鎸囧悜瀛愮被鐨勫湴鍧€
A a;
A *pA=&a;
pa->fn(5);
pb->A::fn(5);
pA->fn(5);
pa->yfn(2);
pb->yfn(2);
return 0;
}
///鎺屾彙c++,涓昏鏄痗++璇硶鐨勪竴浜涘熀鏈壒寰?