. input>>dt.yr; cout<<\ input>>dt.mo; cout<<\ input>>dt.da; return input; } //定义\运算符重载函数 ostream& operator<< ( ostream& output, Date& dt ) { output<< dt.yr << '/' << dt.mo << '/' << dt.da<>)运算符 void main() { //声明对象 Date dt1(2002,5,1),dt2; //显示dt1对象 cout<>dt2; cout< //定义ex类 class ex_class { int a; double b; public: ex_class(int n=1,double x=1.0):a(n),b(x) {} void show_value(char *name) { cout<show_value(\ //p指向obj2并显示 Word 资料
p=&obj2; (*p).show_value(\ //p指向动态创建的对象并显示 p=new ex_class; p->show_value(\ delete p; //删除对象 } #include //基类Box class Box { int width,height; public: void SetWidth(int w) { width=w; } void SetHeight(int h) { height=h; } int GetWidth() {return width;} int GetHeight() {return height;} }; //派生类ColoredBox class ColoredBox:public Box { int color; public: void SetColor(int c){ color=c; } int GetColor() {return color;} }; // 在main()中测试基类和派生类 main(void) { //声明并使用ColoredBox类的对象 ColoredBox cbox; cbox.SetColor(3); //使用自己的成员函数 cbox.SetWidth(150); //使用基类的成员函数 cbox.SetHeight(100); //使用基类的成员函数 cout<<\ cout<<\ //使用自己的成员函数 cout<<\ //使用基类的成员函数 cout<<\ //使用基类的成员函数 //cout< //基类First class First { int val1; public: SetVal1(int v) { val1=v; } . void show_First(void) { cout<<\ } }; //派生类Second class Second:private First { //默认为private模式 int val2; public: void SetVal2(int v1,int v2) { SetVal1(v1); //可见,合法 val2=v2; } void show_Second(void) { // cout<<\不能访问First私有成员 show_First(); cout<<\ } }; main() { Second s1; //s1.SetVal1(1); //不可见,非法 s1.SetVal2(2,3); //合法 //s1.show_First(); //不可见,非法 s1.show_Second(); } #include //基类First class First { int val1; public: SetVal1(int v) { val1=v; } void show_First(void) { cout<<\ } }; //派生类Second class Second:public First { //默认为private模式 int val2; public: void SetVal2(int v1,int v2) { SetVal1(v1); //可见,合法 val2=v2; } void show_Second(void) { // cout<<\不能访问First私有成员 show_First(); cout<<\ } }; main() { Second s1; //调用Second类定义的成员函数 s1.SetVal2(2,3); cout<<\ s1.show_Second(); //调用First类定义的成员函数 s1.SetVal1(10); Word 资料
cout<<\ s1.show_First(); } #include //定义最低层基类,它作为其他类的基类 class First { int val1; public: First(void) { cout<<\ } }; //定义派生类,它作为其他类的基类 class Second :public First { int val2; public: Second(void) { cout<<\ } }; //定义最上层派生类 class Three :public Second { int val3; public: Three() { cout<<\ } }; //定义各基类的对象,测试构造函数的执行情况 //定义各基类的对象,测试构造函数的执行情况 main() { cout<<\ First f1; cout<<\ Second s1; cout<<\ Three t1; } #include //定义基类First class First { int num; float grade; public: //构造函数带参数 First(int n,float v ) : num(n),grade(v) { cout<<\ } DispFirst(void) { cout<<\ cout<<\ } }; //定义派生类Second class Second :public First { double val; public: //无参数构造函数,要为基类的构造函数设置参数 . Second(void):First(10000,0) { val=1.0; cout<<\ } //带参数构造函数,为基类的构造函数设置参数 Second(int n,float x,double dx):First(n,x) { val=dx; cout<<\ } Disp(char *name){ cout< //定义最低层基类First,它作为其他类的基类 class First { int val1; public: First() { cout<<\ } ~First() { cout<<\ } }; //定义派生类Second,它作为其他类的基类 class Second :public First { //默认为private模式 int val2; public: Second() { cout<<\ } ~Second() { cout<<\ } }; //定义最上层派生类Three class Three :public Second { int val3; public: Three() { cout<<\ } Word 资料
~Three() { cout<<\ } }; //main()函数中测试构造函数和析构函数的执行情况 main() { Three t1; cout<<\} #include //基类 class First { int val1; protected: void SetVal1(int v) { val1=v; } public: show_First(void) { cout<<\ } }; //派生类 class Second:public First { int val2; protected: void SetVal2(int v) { SetVal1(v); //使用First 基类的保护成员 val2=v; } public: show_Second(void) { show_First(); cout<<\ } }; //派生类 class Third:public Second { int val3; public: void SetVal3(int n) { SetVal1(n); //使用First 基类的保护成员 SetVal2(n); //使用Second基类的保护成员 val3=n; } show_Third(void) { show_Second(); cout<<\ } }; //main()函数的定义 main(void) { First f1; //f1.SetVal1(1); 不可访问 Second s1; //s1.SetVal1(1); 不可访问 //s1.SetVal2(2); 不可访问 . Third t1; //t1.SetVal1(1); 不可访问 //t1.SetVal2(2); 不可访问 t1.SetVal3(10); //显示t1对象的数据 cout<<\ t1.show_Third(); cout<<\ t1.show_Second(); cout<<\ t1.show_First(); } #include enum Color {Red,Yellow,Green,White}; //圆类Circle的定义 class Circle { float radius; public: Circle(float r) {radius=r;} float Area() { return 3.1416*radius*radius; } }; //桌子类Table的定义 class Table { float height; public: Table(float h) {height=h;} float Height() { return height; } }; //圆桌类RoundTable的定义 class RoundTable:public Table,public Circle { Color color; public: RoundTable(float h,float r,Color c); //构造函数 int GetColor() { return color; } }; //圆桌构造函数的定义 RoundTable::RoundTable(float h,float r,Color c):Table(h),Circle(r) { color=c; } //main()函数的定义 main() { RoundTable cir_table(15.0,2.0,Yellow); cout<<\ //调用Height类的成员函数 cout<<\ //调用circle类的成员函数 cout<<\ //调用RoundTable类的成员函数 Word 资料
cout<<\ } #include //定义一个枚举类型 enum Color {Red,Yellow,Green,White}; //圆类Circle的定义 class Circle { float radius; public: Circle(float r) { radius=r; cout<<\ } ~Circle() { //析构函数 cout<<\ destroyed!\ } float Area() { return 3.1416*radius*radius; } }; //桌子类Table的定义 class Table { float height; public: Table(float h) { height=h; cout<<\ } ~Table() { //构造函数 cout<<\ } float Height() { return height; } }; //圆桌类RoundTable的定义 class RoundTable:public Table,public Circle { Color color; public: RoundTable(float h,float r,Color c); //构造函数 int GetColor() { return color; } ~RoundTable() { //构造函数 cout<<\ } }; //圆桌构造函数的定义 RoundTable::RoundTable(float h,float r,Color c):Table(h),Circle(r) { color=c; cout<<\} //测试多继承中构造函数和析构函数的执行方式 main() { RoundTable cir_table(15.0,2.0,Yellow); cout<<\ //调用Height类的成员函数 . cout<<\ //调用circle类的成员函数 cout<<\ //调用RoundTable类的成员函数 cout<<\ } #include //定义有两个虚函数的基类 class Base { public: //定义两个虚函数 virtual void aFn1(void){ cout<<\ } virtual void aFn2(void) { cout<<\ } //定义非虚函数 void aFn3(void) { cout<<\ } }; //派生类Derived_1中重新定义了基类中的虚函数aFn1 class Derived_1:public Base { public: void aFn1(void) { //覆盖aFn1()函数 cout<<\ } // void aFn3(void) { 语法错误 // cout<<\ //} }; //派生类Derived_2中重新定义了基类中的虚函数aFn2 class Derived_2:public Base{ public: void aFn2(void){ //覆盖aFn2()函数 cout<<\ } // void aFn3(void) { 语法错误 // cout<<\is in Second derived class.\ //} }; //main()函数的定义 main(void) { //创建和使用基类Base的对象 Base b; cout<<\ b.aFn1(); b.aFn2(); b.aFn3(); cout<<\ //创建和使用派生类Derived_1的对象 Word 资料
Derived_1 d1; cout<<\ d1.aFn1(); d1.aFn2(); d1.aFn3(); cout<<\ //创建和使用派生类Derived_2的对象 Derived_2 d2; cout<<\ d2.aFn1(); d2.aFn2(); d2.aFn3(); } #include //定义抽象类 class Base { public: //定义两个纯虚函数 virtual void aFn1(void)=0; virtual void aFn2(void)=0; }; //派生类Derived_1中覆盖了基类中的纯虚函数 class Derived_1:public Base { public: void aFn1(void) { cout<<\ } void aFn2(void) { cout<<\ } }; //派生类Derived_2中覆盖了基类中的纯虚函数 class Derived_2:public Base{ public: virtual void aFn1(void){ cout<<\ } void aFn2(void){ cout<<\ } }; //main()函数中测试抽象类及其派生类的对象 main(void) { //用抽象类不能创建对象 // Base b; 语法错误 // b.aFn1(); // b.aFn2(); //创建和使用Derived_1类的对象 Derived_1 d1; cout<<\ d1.aFn1(); d1.aFn2(); cout<<\