(二)矩形类求面积

来源:互联网 发布:淘宝上玛雅蓝是真的吗 编辑:程序博客网 时间:2024/04/28 20:40
第一种方案:
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4.   
  5. class Juxing  
  6. {  
  7. public:     
  8.    Juxing(int Hwidth,int Hheight);  
  9.    ~Juxing() {}  
  10.    int GetWidth() { return width; }  
  11.    int GetHeight() { return height; }  
  12.    void SetWidth(int Hwidth) { width=Hwidth; }  
  13.    void SetHeight(int Hheight) { height=Hheight; }  
  14.   
  15.   
  16.    int GetArea() const;  
  17.   
  18.   
  19. private:  
  20.    int width,height;  
  21. };  
  22.   
  23.   
  24. Juxing::Juxing(int Hwidth,int Hheight)  
  25. {  
  26.    width=Hwidth;  
  27.    height=Hheight;  
  28. }  
  29.   
  30.   
  31. int Juxing::GetArea() const  
  32. {  
  33.    return width*height;  
  34. }  
  35.   
  36.   
  37. int main()  
  38. {  
  39.    Juxing MyArea(20,30);  
  40.    int Area1,Area2,Area3;  
  41.    int width,height;  
  42.   
  43.   
  44.    Area1=MyArea.GetArea();  
  45.    cout<<"默认矩形的面积Area1: "<<Area1<<endl<<endl;  
  46.      
  47.    MyArea.SetWidth(40);  
  48.    MyArea.SetHeight(50);  
  49.      
  50.    Area2=MyArea.GetArea();  
  51.    cout<<"设定矩形的面积Area2: "<<Area2<<endl<<endl;  
  52.   
  53.   
  54.    cout<<"请输入矩形的长: ";  
  55.    cin>>width;  
  56.    cout<<"请输入矩形的宽: ";  
  57.    cin>>height;  
  58.   
  59.   
  60.    Juxing YouArea(width,height);  
  61.    Area3=YouArea.GetArea();  
  62.    cout<<"\n输入矩形的面积Area3: "<<Area3<<endl<<endl;  
  63.   
  64.   
  65.    return(0);  
  66. }  


第二种方案

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4.   
  5. class Juxing  
  6. {  
  7. public:  
  8.    Juxing(int Hwidth,int Hheight);  
  9.    Juxing()  
  10.    {  
  11.       width=20;  
  12.       height=30;  
  13.    }  
  14.    ~Juxing() {}  
  15.    Juxing(Juxing &YouArea);//拷贝函数  
  16.    void input();//输入人员信息  
  17.    void INPUT(int Hwidth,int Hheight);//输入人员信息  
  18.    void OUTPUT();//输出人员信息  
  19.     
  20.    int GetArea() const;//求面积  
  21.   
  22.   
  23. private:  
  24.    int width,height;  
  25. };  
  26.   
  27.   
  28.   
  29.   
  30. Juxing::Juxing(int Hwidth,int Hheight)  
  31. {  
  32.    width=Hwidth;  
  33.    height=Hheight;  
  34. }  
  35.   
  36.   
  37. void Juxing::INPUT(int Hwidth=10,int Hheight=35)  
  38. {  
  39.    width=Hwidth;  
  40.    height=Hheight;  
  41. }  
  42.   
  43.   
  44. int Juxing::GetArea() const  
  45. {  
  46.    return width*height;  
  47. }  
  48.   
  49.   
  50. void Juxing::input()  
  51. {  
  52.    cout<<"请输入矩形的长: ";  
  53.    cin>>width;  
  54.    cout<<"请输入矩形的宽: ";  
  55.    cin>>height;  
  56. }  
  57.   
  58.   
  59. void Juxing::OUTPUT()  
  60. {  
  61.    cout<<"长度为 "<<width<<" 宽度为 "<<height<<" 的面积为: ";  
  62. }  
  63.   
  64.   
  65. Juxing::Juxing(Juxing &YouArea)  
  66. {  
  67.    width=YouArea.width;  
  68.    height=YouArea.height;  
  69.    
  70.    cout<<"拷贝函数被调用!!"<<endl;  
  71. }  
  72.   
  73.   
  74. int main()  
  75. {  
  76.    Juxing MyArea,YouArea;  
  77.      
  78.    cout<<"默认数值的面积为: "<<endl;  
  79.    MyArea.INPUT();  
  80.    MyArea.OUTPUT();  
  81.    cout<<MyArea.GetArea()<<endl;  
  82.   
  83.   
  84.    cout<<"\n输入数值矩形的面积: "<<endl;  
  85.    MyArea.INPUT(40,80);  
  86.    MyArea.OUTPUT();  
  87.    cout<<MyArea.GetArea()<<endl;  
  88.   
  89.   
  90.    cout<<"\n输入数值矩形的面积: "<<endl;  
  91.    YouArea.input();  
  92.    YouArea.OUTPUT();  
  93.    cout<<YouArea.GetArea()<<endl;  
  94.   
  95.   
  96.    return(0);  
  97. }  

第三种方案

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4.   
  5. class Juxing  
  6. {  
  7. public:  
  8.     Juxing(int len,int wid)  
  9.     {  
  10.         Length=len;  
  11.         Width=wid;  
  12.     }  
  13.     ~Juxing() {}  
  14.     int GetArea() { return Length*Width; }  
  15.     int GetLength() { return Length; }  
  16.     int GetWidth() { return Width; }  
  17.       
  18. private:  
  19.     int Length;  
  20.     int Width;  
  21. };  
  22.   
  23.   
  24. int main()  
  25. {  
  26.     int length,width;  
  27.     cout<<"请输入矩形的长度: ";  
  28.     cin>>length;  
  29.     cout<<"请输入矩形的宽度: ";  
  30.     cin>>width;  
  31.       
  32.     Juxing MyJuxing(length,width);  
  33.     cout<<"长为 "<<length<<" 宽为 "<<width<<" 的矩形的面积为: "<<MyJuxing.GetArea()<<endl;  
  34.       
  35.     return(0);  
  36. }  

转自:http://blog.csdn.net/jqh2002_blog/article/details/24847849
0 0