欢迎来到天天文库
浏览记录
ID:37918973
大小:109.50 KB
页数:14页
时间:2019-06-02
《C++程序设计例题》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C++程序设计例题1#include//类的定义格式classjk1{public:inta;intb;voidf1(){cout<<"注意函数的书写格式";}voidf2(){cout<<"注意中英文标点符号";}};voidmain(){jk1x;x.a=66;x.b=88;cout<2、ain(){classjk1{public:char*str;doublex;}x;x.str="五羊开泰";x.x=3.1415926;cout<//公有属性、私有属性、保护属性classok2{public:inta;voidfa(){cout<<"在任何函数中都可以访问它";}private:intb;voidfb(){cout<<"只有成员函数才可以访问";}protected:intc;voidfc(){cout<<"除了成员函数可以访问外,派生类的3、成员函数才可以访问";}};voidmain(){ok2x;x.a=11;cout<//成员的私有属性使用#includeclassok3{chara[20];intb;voidkk(){cout<<"我是私有函数,不可随随便便调用哟。";}public:voidinab(charaa[],intbb){strcpy(a,aa);b=bb;}void4、outab(){cout<//成员函数的另一种书写格式classok4{inta;intb;public:voidf(intx){a=x;cout<5、有函数声明语句"<//内联函数classok5{public:inta;voidcina(intaa){a=aa;cout<<"在类中定义的成员函数默认为内联函数";}voidout();};inlinevoidok5::out(){cout<<"在类外定义的成员函数要用inline声明才能为内联函数";}voidmain(){ok5x;x.cina(88);cout<6、);}6#include"iostream.h"//构造函数一classok6{private:inta;doubleb;public:ok6(){a=0;b=0.0;cout<<"我是构造函数";}voidcinb(intaa,doublebb){a=aa;b=bb;}voidoutab(){cout<//构造函数二classok7{private:inta;double7、b;public:ok7(intx,doubley){a=x;b=y;cout<<"二个参数"<//拷贝构造函数classok8{inta;public:ok8(inta8、a){a=aa;cout<<"我是构造函数";}ok8(ok8&x){a=x.a;cout<<"我是拷贝构造函数";}voidout(){cout<
2、ain(){classjk1{public:char*str;doublex;}x;x.str="五羊开泰";x.x=3.1415926;cout<//公有属性、私有属性、保护属性classok2{public:inta;voidfa(){cout<<"在任何函数中都可以访问它";}private:intb;voidfb(){cout<<"只有成员函数才可以访问";}protected:intc;voidfc(){cout<<"除了成员函数可以访问外,派生类的
3、成员函数才可以访问";}};voidmain(){ok2x;x.a=11;cout<//成员的私有属性使用#includeclassok3{chara[20];intb;voidkk(){cout<<"我是私有函数,不可随随便便调用哟。";}public:voidinab(charaa[],intbb){strcpy(a,aa);b=bb;}void
4、outab(){cout<//成员函数的另一种书写格式classok4{inta;intb;public:voidf(intx){a=x;cout<5、有函数声明语句"<//内联函数classok5{public:inta;voidcina(intaa){a=aa;cout<<"在类中定义的成员函数默认为内联函数";}voidout();};inlinevoidok5::out(){cout<<"在类外定义的成员函数要用inline声明才能为内联函数";}voidmain(){ok5x;x.cina(88);cout<6、);}6#include"iostream.h"//构造函数一classok6{private:inta;doubleb;public:ok6(){a=0;b=0.0;cout<<"我是构造函数";}voidcinb(intaa,doublebb){a=aa;b=bb;}voidoutab(){cout<//构造函数二classok7{private:inta;double7、b;public:ok7(intx,doubley){a=x;b=y;cout<<"二个参数"<//拷贝构造函数classok8{inta;public:ok8(inta8、a){a=aa;cout<<"我是构造函数";}ok8(ok8&x){a=x.a;cout<<"我是拷贝构造函数";}voidout(){cout<
5、有函数声明语句"<//内联函数classok5{public:inta;voidcina(intaa){a=aa;cout<<"在类中定义的成员函数默认为内联函数";}voidout();};inlinevoidok5::out(){cout<<"在类外定义的成员函数要用inline声明才能为内联函数";}voidmain(){ok5x;x.cina(88);cout<6、);}6#include"iostream.h"//构造函数一classok6{private:inta;doubleb;public:ok6(){a=0;b=0.0;cout<<"我是构造函数";}voidcinb(intaa,doublebb){a=aa;b=bb;}voidoutab(){cout<//构造函数二classok7{private:inta;double7、b;public:ok7(intx,doubley){a=x;b=y;cout<<"二个参数"<//拷贝构造函数classok8{inta;public:ok8(inta8、a){a=aa;cout<<"我是构造函数";}ok8(ok8&x){a=x.a;cout<<"我是拷贝构造函数";}voidout(){cout<
6、);}6#include"iostream.h"//构造函数一classok6{private:inta;doubleb;public:ok6(){a=0;b=0.0;cout<<"我是构造函数";}voidcinb(intaa,doublebb){a=aa;b=bb;}voidoutab(){cout<//构造函数二classok7{private:inta;double
7、b;public:ok7(intx,doubley){a=x;b=y;cout<<"二个参数"<//拷贝构造函数classok8{inta;public:ok8(inta
8、a){a=aa;cout<<"我是构造函数";}ok8(ok8&x){a=x.a;cout<<"我是拷贝构造函数";}voidout(){cout<
此文档下载收益归作者所有