(二)矩形类求面积

第一种方案:
  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. }  


第二种方案

  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. }  

第三种方案

  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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道了。这里是用 C 语言编写的一个简单的矩形类,可以根据两点来计算矩形面积和周长: ```c #include <stdio.h> #include <math.h> struct point { float x; float y; }; struct rectangle { struct point pt1; struct point pt2; }; float distance(struct point a, struct point b) { float dx = a.x - b.x; float dy = a.y - b.y; return sqrt(dx * dx + dy * dy); } float perimeter(struct rectangle rect) { float width = distance(rect.pt1, rect.pt2); float height = distance((struct point){rect.pt1.x, rect.pt2.y}, (struct point){rect.pt2.x, rect.pt1.y}); return 2 * (width + height); } float area(struct rectangle rect) { float width = distance(rect.pt1, rect.pt2); float height = distance((struct point){rect.pt1.x, rect.pt2.y}, (struct point){rect.pt2.x, rect.pt1.y}); return width * height; } int main() { struct rectangle rect = {{0,0},{2,2}}; printf("The perimeter of the rectangle is %f\n", perimeter(rect)); printf("The area of the rectangle is %f\n", area(rect)); return 0; } ``` 在这个示例中,我们首先定义了一个 `point` 结构体来表示维平面上的一个点,然后定义了一个 `rectangle` 结构体来表示一个由两个点确定的矩形。接下来我们实现了一个 `distance` 函数来计算两个点之间的距离,这个函数用到了勾股定理。最后我们定义了 `perimeter` 和 `area` 函数来分别计算矩形的周长和面积,这里用到了上面定义的 `distance` 函数。 在 `main` 函数中,我们定义了一个矩形 `rect`,并且输出了它的周长和面积。 希望这个示例能够帮助你理解如何用 C 语言编写简单的矩形类!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值