c#各种小程序

来源:互联网 发布:现在学java前景怎么样 编辑:程序博客网 时间:2024/05/22 15:16
// my first program in C++//#include <iostream>//using namespace std;////int main() {//    int a,b,result;//    a = 5;//    b = 2;//    a = a + 1;//    result = a - b;//    cout <<a<<endl;//    cout <<result<<endl;//    cout << "Hello World!"<<endl;//    cout << "I'm a C++ program";//    return 0;//}// C++字符串例题//#include <iostream>//#include <string>//using namespace std;////int main ()//{//   string mystring;//  mystring =//    "This is the initial string content";//  cout << mystring << endl;//  mystring =//    "This is a different string content";//  cout << mystring << endl;//  return 0;//}// 赋值符号例子//#include <iostream>//using namespace std;////int main ()//{//  int a, b;         // a:?,  b:?//  a = 10;           // a:10, b:?//  b = 4;            // a:10, b:4//  a = b;            // a:4,  b:4//  b = 7;            // a:4,  b:7////  cout << "a:";//  cout << a;//  cout << " b:"<<b;//  return 0;//}// 组合运算符例子//#include <iostream>//using namespace std;////int main ()//{//  int a, b=3;//  a = b;//  a+=2;             // 相当于 a=a+2//  cout << "a="<<a;//  return 0;//}// 条件运算符例子//#include <iostream>//using namespace std;////int main ()//{//  int a,b,c;////  a=2;//  b=7;//  c = (a>b) ? a : b;////  cout << c;////  return 0;//}// i/o example//#include <iostream>//using namespace std;//int main ()//{//int i,a;//cout << "Please enter an integer value: ";//cin >>i>>a;//cout << "The value you entered is " << i <<" and its double is "<< i*2 <<endl;//cout << "The value you entered is " << a;//cout << " and its double is " << a*2 << ".\n";//return 0;//}// 读取字符串例子//#include <iostream>//#include <string>//using namespace std;////int main ()//{//  string mystr;//  cout << "What's you name? "<<endl;//  getline (cin, mystr);//  cout << "Hello " << mystr << ".\n";//  cout << "What is your favorite color? ";//  getline (cin, mystr);//  cout << "I like " << mystr << " too!\n";//  return 0;//}// 字符串流的使用示例//#include <iostream>//#include <string>//#include <sstream>//using namespace std;////int main ()//{//  string mystr;//  float price=0;//  int quantity=0;////  cout << "Enter price: ";//  getline (cin,mystr);//  stringstream(mystr) >> price;   //将字符串转换成整数//  cout << "Enter quantity: ";//  getline (cin,mystr);//  stringstream(mystr) >> quantity;//  cout << "Total price: " << price*quantity << endl;//  return 0;//}// custom countdown using while//while 循环//#include <iostream>//using namespace std;//int main ()//{//int n;//cout << "Enter the starting number > ";//cin >> n;//while (n>0) {//cout << n << ", ";//--n;//}//cout << "FIRE!";//return 0;//}//do-while 循环// number echoer//#include <iostream>//using namespace std;//int main ()//{//unsigned long n;//do {//cout << "Enter number (0 to end): ";//cin >> n;//cout << "You entered: " << n << "\n";//} while (n != 0);//return 0;//}//for 循环// countdown using a for loop       下面是用for循环实现的倒计数的例子://#include <iostream.h>//int main ()//{//for (int n=10; n>0; n--) {//cout << n << ", ";//}//cout << "FIRE!";//return 0;//}//分支控制和跳转(Bifurcation of control and jumps)//break 语句//通过使用break语句,即使在结束条件没有满足的情况下,我们也可以跳出一个循环。它可以被用来结束一个无限循环(infinite loop),或强迫循环在其自然结束之前结束。例如,我们想要在倒计数自然结束之前强迫它停止(也许因为一个引擎故障):// break loop example//#include <iostream>//using namespace std;//int main ()//{//int n;//for (n=10; n>0; n--) {//cout << n << ", ";//if (n==3)//{//cout << "countdown aborted!";//break;//}//return 0;//}//}//continue 语句//ontinue语句使得程序跳过当前循环中剩下的部分而直接进入下一次循环,就好像循环中语句块的结尾已经到了使得循环进入下一次重复。例如,下面例子中倒计数时我们将跳过数字5的输出:// continue loop example//#include <iostream>//using namespace std;////int main ()//{//for (int n=10; n>0; n--) {//if (n==5) continue;//cout << n << ", ";//}//cout << "FIRE!";//return 0;//}//goto 语句//通常除了底层程序爱好者使用这条语句,它在结构化或面向对象的编程中并不常用。下面的例子中我们用goto来实现倒计数循环:// goto loop example//#include <iostream>//using namespace std;//int main ()//{//int n=10;//loop://cout << n << ", ";//n--;//if (n>0) goto loop;//cout << "FIRE!";//return 0;//}//下面的两段代码段功能相同:/*switch exampleif-else equivalentswitch (x) {case 1:cout << "x is 1";break;case 2:cout << "x is 2";break;default:cout << "value of x unknown";}if (x == 1) {cout << "x is 1";}else if (x == 2) {cout << "x is 2";}else {cout << "value of x unknown";}*//*前面已经提到switch的语法有点特殊。注意每个语句块结尾包含的break语句。这是必须的,因为如果不这样做,例如在语句块block of instructions 1 的结尾没有break,程序执行将不会跳转到switch选择的结尾处 (}) ,而是继续执行下面的语句块,直到第一次遇到break语句或到switch选择结构的结尾。因此,不需要在每一个case 域内加花括号{ } 。这个特点同时可以帮助实现对不同的可能值执行相同的语句块。例如:switch (x) {case 1:case 2:case 3:cout << "x is 1, 2 or 3";break;default:cout << "x is not 1, 2 nor 3";}注意switch只能被用来比较表达式和不同常量的值constants。因此我们不能够把变量或范围放在case之后,例如 (case (n*2):) 或 (case (1..3):) 都不可以,因为它们不是有效的常量。 如果你需要检查范围或非常量数值,使用连续的if 和else if 语句。*/// function example//#include <iostream>//using namespace std;//int addition (int a, int b)//{//    int r;//    r=a+b;//    return (r);//}////int main ()//{//    int z;//    z = addition (5,3);//    cout << "The result is " << z;//    return 0;//}// function example//#include <iostream>//using namespace std;//int subtraction (int a, int b)//{//    int r;//    r=a-b;//    return (r);//}////int main ()//{//    int x=5, y=3, z;//    z = subtraction (7,2);//    cout << "The first result is " << z << '\n';//    cout << "The second result is " << subtraction (7,2) << '\n';//    cout << "The third result is " << subtraction (x,y) << '\n';//    z= 4 + subtraction (x,y);//    cout << "The fourth result is " << z << '\n';//    return 0;//}// void 函数示例//#include <iostream>//using namespace std;//void printmessage ()//{//  cout << "I'm a function!";//}////int main ()//{//  printmessage ();//  return 0;//}//////////参数按数值传递和按地址传递(Arguments passed by value and by reference)/////// passing parameters by reference//#include <iostream>//using namespace std;//void duplicate (int& a, int& b, int& c)//{//a*=2;//b*=2;//c*=2;//}////int main ()//{//int x=1, y=3, z=7;//duplicate (x, y, z);//cout << "x=" << x << ", y=" << y << ", z=" << z;//return 0;//}// more than one returning value//下面是一个函数,它可以返回第一个输入参数的前一个和后一个数值。//#include <iostream>//using namespace std;//void prevnext (int x, int& prev, int& next)//{//prev = x-1;//next = x+1;//}////int main ()//{//int x=100, y, z;//prevnext (x, y, z);//cout << "Previous=" << y << ", Next=" << z;//return 0;//}///////////参数的默认值(Default values in arguments)///////////// default values in functions//#include <iostream>//using namespace std;//int divide (int a, int b=2) {//int r;//r=a/b;//return (r);//}////int main () {//cout << divide (12);//cout << endl;//cout << divide (20,4);//return 0;//}///////////函数重载(Overloaded functions)//////////////两个不同的函数可以用同样的名字,只要它们的参量(arguments)的原型(prototype)不同,也就是说你可以把同一个名字给多个函数,如果它们用不同数量的参数,或不同类型的参数// overloaded function//#include <iostream>//using namespace std;//int divide (int a, int b) {//return (a/b);//}////float divide (float a, float b) {//return (a/b);//}////int main () {//int x=5,y=2;//float n=5.0,m=2.0;//cout << divide (x,y);//cout << "\n";//cout << divide (n,m);//cout << "\n";//return 0;//}///////////递归(Recursivity)/////////// factorial calculator//#include <iostream>//using namespace std;//long factorial (long a){//if (a > 1) return (a * factorial (a-1));//else return (1);//}////int main () {//long l;//cout << "Type a number: ";//cin >> l;//cout << "!" << l << " = " << factorial (l);//return 0;//}////////函数的声明(Declaring functions)/////////// 声明函数原型//#include <iostream>//using namespace std;//void odd (int a);//void even (int a);////int main () {//int i;//do {//cout << "Type a number: (0 to exit)";//cin >> i;//odd (i);//} while (i!=0);//return 0;//}////void odd (int a) {//if ((a%2)!=0) cout << "Number is odd.\n";//else even (a);//}////void even (int a) {//if(a==0)  cout << "Number is science.\n";//else if ((a%2)==0) cout << "Number is even.\n";//else odd (a);//}// arrays example//#include <iostream>//using namespace std;//int billy [ ] = {16, 2, 77, 40, 12071};//int n, result=0;////int main () {//for ( n=0 ; n<5 ; n++ ) {//result += billy[n];//}////cout << result;//return 0;//}//int jimmy [3][5]; 效果上等价于//int jimmy [15]; (3 * 5 = 15)/*唯一的区别是编译器帮我们记住每一个想象中的维度的深度。下面的例子中我们就可以看到,两段代码一个使用2维数组,另一个使用简单数组,都获得同样的结果,即都在内存中开辟了一块叫做jimmy的空间,这个空间有15个连续地址位置,程序结束后都在相同的位置上存储了相同的数值*//*// multidimensional array#include <iostream.h>#define WIDTH 5#define HEIGHT 3int jimmy [HEIGHT][WIDTH];int n,m;int main (){for (n=0; n<HEIGHT; n++) {for (m=0; m<WIDTH; m++)jimmy[n][m]=(n+1)*(m+1);}return 0;}// pseudo-multidimensional array#include <iostream.h>#define WIDTH 5#define HEIGHT 3int jimmy [HEIGHT * WIDTH];int n,m;int main (){for (n=0; n<HEIGHT; n++){for (m=0; m<WIDTH; m++)jimmy[n * WIDTH + m]=(n+1)*(m+1);}return 0;}*/// arrays as parameters//#include <iostream>//using namespace std;//void printarray (int arg[ ], int length) {//for (int n=0; n<length; n++) {//cout << arg[n] << " ";//}//cout << "\n";//}////int main () {//int firstarray[ ] = {5, 10, 15};//int secondarray[ ] = {2, 4, 6, 8, 10};//printarray (firstarray,3);//printarray (secondarray,5);//return 0;//}// setting value to string//#include <iostream>//using namespace std;//#include <string.h>////int main () {//char szMyName [20];//strcpy (szMyName,"J. Soulie");//cout << szMyName;//return 0;//}// setting value to string//#include <iostream>//using namespace std;//void setstring (char szOut [ ], char szIn [ ]) {//int n=0;//do {//szOut[n] = szIn[n];//} while (szIn[n++] != '\0');//}////int main () {//char szMyName [20];//setstring (szMyName,"J. Soulie");//cout << szMyName;//return 0;//}// cin with strings//#include <iostream>//using namespace std;//int main () {//char mybuffer [100];//cout << "What's your name? ";//cin.getline (mybuffer,100);//cout << "Hello " << mybuffer << ".\n";//cout << "Which is your favourite team? ";//cin.getline (mybuffer,100);//cout << "I like " << mybuffer << " too.\n";//return 0;//}// cin and ato* functions/*函数库cstdlib (stdlib.h) 提供了3个有用的函数:atoi: 将字符串string 转换为整型intatol: 将字符串string 转换为长整型longatof: 将字符串string 转换为浮点型float*///#include <iostream>//#include <stdlib.h>//using namespace std;//int main () {//char mybuffer [100];//float price;//int quantity;//cout << "Enter price: ";//cin.getline (mybuffer,100);//price = atof (mybuffer);//cout << "Enter quantity: ";//cin.getline (mybuffer,100);//quantity = atoi (mybuffer);//cout << "Total price: " << price*quantity;//return 0;//}// my first pointer//#include <iostream>//using namespace std;//int main ( ) {//int value1 = 5, value2 = 15;//int * mypointer;//mypointer = &value1;//*mypointer = 10;//mypointer = &value2;//*mypointer = 20;//cout << "value1==" << value1 << "/ value2==" << value2;//return 0;//}// more pointers//#include <iostream>//using namespace std;//int main () {//int value1 = 5, value2 = 15;//int *p1, *p2;//p1 = &value1; // p1 = address of value1//p2 = &value2; // p2 = address of value2//*p1 = 10; // value pointed by p1 = 10//*p2 = *p1; // value pointed by p2 = value pointed by p1//p1 = p2; // p1 = p2 (value of pointer copied)//*p1 = 20; // value pointed by p1 = 20//cout << "value1==" << value1 << "/ value2==" << value2;//return 0;//}// more pointers//#include <iostream>//using namespace std;//int main () {//int numbers[5];//int * p;//p = numbers;//*p = 10;//p++;//*p = 20;//p = &numbers[2];//*p = 30;//p = numbers + 3;//*p = 40;//p = numbers;//*(p+4) = 50;//for (int n=0; n<5; n++)//cout << numbers[n] << ", ";//return 0;//}// integer increaser//空指针void pointers//#include <iostream>//using namespace std;//void increase (void* data, int type) {//switch (type) {//case sizeof(char) : (*((char*)data))++; break;//case sizeof(short): (*((short*)data))++; break;//case sizeof(long) : (*((long*)data))++; break;//}//}////int main () {//char a = 5;//short b = 9;//long c = 12;//increase (&a,sizeof(a));//increase (&b,sizeof(b));//increase (&c,sizeof(c));//cout << (int) a << ", " << b << ", " << c;//return 0;//}/*//////////////////有错///////////////////函数指针Pointers to functions// pointer to functions// pointer to functions#include <iostream>using namespace std;int addition (int a, int b) {    return (a+b);}int subtraction (int a, int b) {    return (a-b);}int (*minus)(int,int) = subtraction;int operation (int x, int y, int (*functocall)(int,int)) {    int g;    g = (*functocall)(x,y);    return (g);}int main () {    int m,n;    m = operation (7, 5, addition);    n = operation (20, m, minus);    cout<<n;    return 0;}*///删除操作符delete// rememb-o-matic//#include<iostream>//using namespace std;//#include<stdlib.h>//int main(){//char input [100];//int i,n;//long * l;//cout << "How many numbers do you want to type in? ";//cin.getline (input,100); i=atoi (input);//l= new long[i];//if (l == NULL) exit (1);//for (n=0; n<i; n++) {//cout << "Enter number: ";//cin.getline (input,100);//l[n]=atol (input);//}//cout << "You have entered: ";//for (n=0; n<i; n++) {//cout << l[n] << ", ";//delete[] l;//return 0;//}//}///////////结果与答案有出入/////////////下面我们看另一个关于电影的例子://// example about structures//#include<iostream>//using namespace std;//#include<string.h>//#include<stdlib.h>//struct movies_t {//char title [50];//int year;//}mine, yours;//void printmovie (movies_t movie);//int main () {//char buffer [50];//strcpy (mine.title, "2001 A Space Odyssey");//mine.year = 1968;//cout << "Enter title: ";//cin.getline (yours.title,50);//cout << "Enter year: ";//cin.getline (buffer,50);//yours.year = atoi (buffer);//cout << "My favourite movie is:\n ";//printmovie (mine);//cout << "And yours:\n";//printmovie (yours);//return 0;//}///////////结果与答案有出入///////////// array of structures//#include<iostream>//using namespace std;//#include<stdlib.h>//#define N_MOVIES 5////struct movies_t {//char title [50];//int year;//} films [N_MOVIES];////void printmovie (movies_t movie);////int main () {//char buffer [50];//int n;//for (n=0; n<N_MOVIES; n++) {//cout << "Enter title: ";//cin.getline (films[n].title,50);//cout << "Enter year: ";//cin.getline (buffer,50);//films[n].year = atoi (buffer);//}////cout << "\nYou have entered these movies:\n";//for (n=0; n<N_MOVIES; n++) {//printmovie (films[n]);//return 0;//}//}//void printmovie (movies_t movie) {//cout << movie.title;//cout << " (" << movie.year << ")\n";//}// pointers to structures//#include<iostream>//using namespace std;//#include<stdlib.h>////struct movies_t {//char title [50];//int year;//};////int main () {//char buffer[50];////movies_t amovie;//movies_t * pmovie;//pmovie = & amovie;////cout << "Enter title: ";//cin.getline (pmovie->title,50);//cout << "Enter year: ";////cin.getline (buffer,50);//pmovie->year = atoi (buffer);////cout << "\nYou have entered:\n";//cout << pmovie->title;//cout << " (" << pmovie->year << ")\n";////return 0;//}  // classes example//    #include <iostream>//    using namespace std;//    class CRectangle {//            int x, y;//        public://            void set_values (int,int);//            int area (void) {return (x*y);}//    };////    void CRectangle::set_values (int a, int b) {//        x = a;//        y = b;//    }////    int main () {//        CRectangle rect;//        rect.set_values (3,4);//        cout << "area: " << rect.area();//    }  // class example//    #include <iostream>//    using namespace std;//    class CRectangle {//            int x, y;//        public://            void set_values (int,int);//            int area (void) {return (x*y);}//    };////    void CRectangle::set_values (int a, int b) {//        x = a;//        y = b;//    }////    int main () {//        CRectangle rect, rectb;//        rect.set_values (3,4);//        rectb.set_values (5,6);//        cout << "rect area: " << rect.area() << endl;//        cout << "rectb area: " << rectb.area() << endl;//    }//构造函数和析构函数 (Constructors and destructors)/* // class example//    #include <iostream>//    using namespace std;//    class CRectangle {//        int width, height;//      public://        CRectangle (int,int);//        int area (void) {return (width*height);}//    };////    CRectangle::CRectangle (int a, int b) {//        width = a;//        height = b;//    }////    int main () {//        CRectangle rect (3,4);//        CRectangle rectb (5,6);//        cout << "rect area: " << rect.area() << endl;//        cout << "rectb area: " << rectb.area() << endl;//    }   // example on constructors and destructors//    #include <iostream>//    using namespace std;//    class CRectangle {//        int *width, *height;//      public://        CRectangle (int,int);//        ~CRectangle ();//        int area (void) {return (*width * *height);}//    };////    CRectangle::CRectangle (int a, int b) {//        width = new int;//        height = new int;//        *width = a;//        *height = b;//    }////    CRectangle::~CRectangle () {//        delete width;//        delete height;//    }////    int main () {//        CRectangle rect (3,4), rectb (5,6);//        cout << "rect area: " << rect.area() << endl;//        cout << "rectb area: " << rectb.area() << endl;//        return 0;//    }*///////////////////////程序有错//////////////构造函数重载(Overloading Constructors) // overloading class constructors//    #include <iostream>//    using namespace std;//    Class CRectangle {//        int width, height;//      public://        CRectangle ();//        CRectangle (int,int);//        int area (void) {return (width*height);}//    };////    CRectangle::CRectangle () {//        width = 5;//        height = 5;//    }////    CRectangle::CRectangle (int a, int b) {//        width = a;//        height = b;//    }////    int main () {//        CRectangle rect (3,4);//        CRectangle rectb;//        cout << "rect area: " << rect.area() << endl;//        cout << "rectb area: " << rectb.area() << endl;//    }//类的指针(Pointers to classes) // pointer to classes example//    #include <iostream>//    using namespace std;//    class CRectangle {//        int width, height;//      public://        void set_values (int, int);//        int area (void) {return (width * height);}//    };////    void CRectangle::set_values (int a, int b) {//        width = a;//        height = b;//    }////    int main () {//        CRectangle a, *b, *c;//        CRectangle * d = new CRectangle[2];//        b= new CRectangle;//        c= &a;//        a.set_values (1,2);//        b->set_values (3,4);//        d->set_values (5,6);//        d[1].set_values (7,8);//        cout << "a area: " << a.area() << endl;//        cout << "*b area: " << b->area() << endl;//        cout << "*c area: " << c->area() << endl;//        cout << "d[0] area: " << d[0].area() << endl;//        cout << "d[1] area: " << d[1].area() << endl;//        return 0;//    }// 操作符重载(Overloading operators)  // vectors: overloading operators example//    #include <iostream>//    using namespace std;//    class CVector {//      public://        int x,y;//        CVector () {};//        CVector (int,int);//        CVector operator + (CVector);//    };////    CVector::CVector (int a, int b) {//        x = a;//        y = b;//    }////    CVector CVector::operator+ (CVector param) {//        CVector temp;//        temp.x = x + param.x;//        temp.y = y + param.y;//        return (temp);//    }////    int main () {//        CVector a (3,1);//        CVector b (1,2);//        CVector c;//        c = a + b;//        cout << c.x << "," << c.y;//        return 0;//    }    //关键字 this      // this//    #include <iostream>//    using namespace std;//    class CDummy {//      public://        int isitme (CDummy& param);//    };////    int CDummy::isitme (CDummy& param) {//        if (¶m == this) return 1;//        else return 0;//    }////    int main () {//        CDummy a;//        CDummy* b = &a;//        if ( b->isitme(a) )//            cout << "yes, &a is b";//        return 0;//    }//静态成员(Static members) // static members in classes//    #include <iostream>//    using namespace std;//    class CDummy {//      public://        static int n;//        CDummy () { n++; };//        ~CDummy () { n--; };//    };////    int CDummy::n=0;////    int main () {//        CDummy a;//        CDummy b[5];//        CDummy * c = new CDummy;//        cout << a.n << endl;//        delete c;//        cout << CDummy::n << endl;//        return 0;//    }//4.3 类之间的关系(Relationships between classes)//友元函数(Friend functions)   // friend functions//    #include <iostream>//    using namespace std;//    class CRectangle {//        int width, height;//      public://        void set_values (int, int);//        int area (void) {return (width * height);}//        friend CRectangle duplicate (CRectangle);//    };////    void CRectangle::set_values (int a, int b) {//        width = a;//        height = b;//    }////    CRectangle duplicate (CRectangle rectparam) {//        CRectangle rectres;//        rectres.width = rectparam.width*2;//        rectres.height = rectparam.height*2;//        return (rectres);//    }////    int main () {//        CRectangle rect, rectb;//        rect.set_values (2,3);//        rectb = duplicate (rect);//        cout << rectb.area();//    }////////////////////////程序有错//////////////友元类 (Friend classes)// friend class//    #include <iostream>//    using namespace std;//    class CSquare;////    class CRectangle {//        int width, height;//      public://        int area (void) {return (width * height);}//        void convert (CSquare a);//    };////    Class CSquare {//      private://        int side;//      public://        void set_side (int a){side=a;}//        friend class CRectangle;//    };////    void CRectangle::convert (CSquare a) {//        width = a.side;//        height = a.side;//    }////    int main () {//        CSquare sqr;//        CRectangle rect;//        sqr.set_side(4);//        rect.convert(sqr);//        cout << rect.area();//        return 0;//    }///////////////////////程序有错///////////////////类之间的继承(Inheritance between classes)   // derived classes//    #include <iostream>//    using namespace std;//    Class CPolygon {//      protected://        int width, height;//      public://        void set_values (int a, int b) { width=a; height=b;}//    };////    class CRectangle: public CPolygon {//      public://        int area (void){ return (width * height); }//    };////    class CTriangle: public CPolygon {//      public://        int area (void){ return (width * height / 2); }//    };////    int main () {//        CRectangle rect;//        CTriangle trgl;//        rect.set_values (4,5);//        trgl.set_values (4,5);//        cout << rect.area() << endl;//        cout << trgl.area() << endl;//        return 0;//    }//什么是从基类中继承的? (What is inherited from the base class?)  // constructors and derivated classes//    #include <iostream>//    using namespace std;//    class mother {//      public://        mother ()//          { cout << "mother: no parameters\n"; }//        mother (int a)//          { cout << "mother: int parameter\n"; }//    };////    class daughter : public mother {//      public://        daughter (int a)//          { cout << "daughter: int parameter\n\n"; }//    };////    class son : public mother {//      public://        son (int a) : mother (a)//          { cout << "son: int parameter\n\n"; }//    };////    int main () {//        daughter cynthia (1);//        son daniel(1);//        return 0;//    }//多重继承(Multiple inheritance)  // multiple inheritance    #include <iostream>    using namespace std;    class CPolygon {      protected:        int width, height;      public:        void set_values (int a, int b)          { width=a; height=b;}    };    class COutput {      public:        void output (int i);    };    void COutput::output (int i) {        cout << i << endl;    }    class CRectangle: public CPolygon, public COutput {      public:        int area (void)          { return (width * height); }    };    class CTriangle: public CPolygon, public COutput {      public:        int area (void)          { return (width * height / 2); }    };    int main () {        CRectangle rect;        CTriangle trgl;        rect.set_values (4,5);        trgl.set_values (4,5);        rect.output (rect.area());        trgl.output (trgl.area());        return 0;    }//4.4 多态 (Polymorphism)/////////////////////////程序有错////////////////基类的指针(Pointers to base class)// pointers to base class//#include <iostream>//using namespace std;//class CPolygon {//protected://    int width, height;//public://    void set_values (int a, int b) {//        width=a; height=b;//    }//};//class CRectangle: public CPolygon {//public://    int area (void) {//return (width * height);//    }//};//class CTriangle: public CPolygon {//public://    int area (void) {//return (width * height / 2);//    }//};//int main () {//    CRectangle rect;//    CTriangle trgl;//    CPolygon * ppoly1 = ▭//    CPolygon * ppoly2 = &trgl;//    ppoly1->set_values (4,5);//    ppoly2->set_values (4,5);//    cout << rect.area() << endl;//    cout << trgl.area() << endl;//    return 0;//}///////////////////////程序有错/////////////////////////虚拟成员(Virtual members)// virtual members//#include <iostream>//using namespace atd;//class CPolygon {//    protected://        int width, height;//    public://        void set_values (int a, int b)  {//            width=a;//            height=b;//        }//        virtual int area (void) { return (0); }//};//class CRectangle: public CPolygon {//    public://        int area (void) { return (width * height); }//};//class CTriangle: public CPolygon {//    public://        int area (void) {//            return (width * height / 2);//        }//};//int main () {//    CRectangle rect;//    CTriangle trgl;//    CPolygon poly;//    CPolygon * ppoly1 = ▭//    CPolygon * ppoly2 = &trgl;//    CPolygon * ppoly3 = &poly;//    ppoly1->set_values (4,5);//    ppoly2->set_values (4,5);//    ppoly3->set_values (4,5);//    cout << ppoly1->area() << endl;//    cout << ppoly2->area() << endl;//    cout << ppoly3->area() << endl;//    return 0;//}/*///////////////////////程序有错/////////////////////抽象基类(Abstract base classes)// virtual members//#include <iostream>//using namespace std;//class CPolygon {//    protected://        int width, height;//    public://        void set_values (int a, int b) {//            width=a;//            height=b;//        }//        virtual int area (void) =0;//};//class CRectangle: public CPolygon {//    public://        int area (void) { return (width * height); }//};//class CTriangle: public CPolygon {//    public://        int area (void) {//            return (width * height / 2);//        }//};//int main () {//    CRectangle rect;//    CTriangle trgl;//    CPolygon * ppoly1 = ▭//    CPolygon * ppoly2 = &trgl;//    ppoly1->set_values (4,5);//    ppoly2->set_values (4,5);//    cout << ppoly1->area() << endl;//    cout << ppoly2->area() << endl;//    return 0;//}// virtual members*/

0 0
原创粉丝点击