用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

c++ 嵌套类

2012-09-02 作者: 神马举报

[c++]代码库

#include <iostream>
using namespace std;
class rectangle
{
public:
    class point
    {
    public:
        void setx ( int x ) {itsx=x;}
        void sety ( int y ) {itsy=y;}
        int getx() const {return itsx;}
        int gety() const {return itsy;}
    private:
        int itsx;
        int itsy;
    };
    point GetUpLeft() const {return upleft;}
    point GetLowLeft() const {return lowerleft;}
    point GetUpRight() const {return upright;}
    point GetLowRight() const {return lowerright;}
 
    rectangle ( int top,int left,int bottom,int right );
    ~rectangle() {}
 
    int GetTop() const {return Top;}
    int GetLeft() const {return Left;}
    int GetBottom() const {return Bottom;}
    int GetRight() const {return Right;}
 
    void SetUpLeft() {upleft.setx ( Left ); upleft.sety ( Top );}
    void SetLowLeft() {lowerleft.setx ( Left ); lowerleft.sety ( Bottom );}
    void SetUpRight() {upright.setx ( Right ); upright.sety ( Top );}
    void SetLowRight() {lowerright.setx ( Right ); lowerright.sety ( Bottom );}
 
    void SetTop ( int top ) {Top=top;}
    void SetLeft ( int left ) {Left=left;}
    void SetRight ( int right ) {Right=right;}
    void SetBottom ( int bottom ) {Bottom=bottom;}
 
    int GetArear() const {int width=Right-Left; int height=Bottom-Top; return ( width*height );}
 
private:
    point upleft;
    point lowerleft;
    point upright;
    point lowerright;
 
    int Top;
    int Left;
    int Bottom;
    int Right;
};
rectangle::rectangle ( int top,int left,int bottom,int right )
{
    Top=top;
    Left=left;
    Bottom=bottom;
    Right=right;
 
    upleft.setx ( Left );
    upleft.sety ( Top );
 
    upright.setx ( Right );
    upright.sety ( Top );
 
    lowerright.setx ( Right );
    lowerright.sety ( Bottom );
 
    lowerleft.setx ( Left );
    lowerleft.sety ( Bottom );
}
class point
{
public:
    int GetArear ( rectangle &rec ) {return rec.GetArear();}
};
 
int main()
{
    rectangle date ( 40,50,60,80 );
    cout<<"左边为:"<<date.GetLeft() <<endl;
    cout<<"下边为:"<<date.GetBottom() <<endl;
    cout<<"左下的X坐标为:"<<date.GetLowLeft().getx() <<endl;
    cout<<"左下的y坐标为:"<<date.GetLowLeft().gety() <<endl;
    cout<<"矩形面积为:"<<date.GetArear() <<endl;
    cout<<"重新设置Left和Bottom值"<<endl;
    date.SetLeft ( 0 );
    date.SetBottom ( 100 );
    date.SetLowLeft();
    cout<<"左边为:"<<date.GetLeft() <<endl;
    cout<<"下边为:"<<date.GetBottom() <<endl;
    cout<<"左下的X坐标为:"<<date.GetLowLeft().getx() <<endl;
    cout<<"左下的y坐标为:"<<date.GetLowLeft().gety() <<endl;
    point pt;
    cout<<"矩形面积为:"<<pt.GetArear ( date ) <<endl;
    system ( "pause" );
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...