| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 921 人关注过本帖
标题:初学c++,这个移动的部分错在哪里呢?
只看楼主 加入收藏
遗情处有诗章
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2017-3-10
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:1 
初学c++,这个移动的部分错在哪里呢?
程序代码:
// ConsoleApplication26.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<cstdlib>
using namespace std;
class myrect
{
public:
    double getArea()  //声明面积
    {
        return height*width;
    }
    int getgirth()   //声明周长
    {
        return height * 2 + width * 2;
    }
    myrect(int x, int yl, int h, int w)
    {
        this->x = x;
        y = yl;
        height = h;
        width = w;

    }

    void inflate(int n)//放大倍数
    {
        height *= n;
        width *= n;
    }

    void reduce(int p)//缩小倍数
    {
        height = height / p;
        width = width / p;
    }

    void move(int xm, int ym)//移动
    {
        x += xm;
        y += ym;
    }

private:  //变量
    int x, y;
    int height, width;
};
int main()
{
    int x, y;
    myrect rect1(100, 100, 20, 50);
    cout << rect1.getArea() << endl; //输出面积
    cout << rect1.getgirth() << endl;//输出周长
    rect1.inflate(3);//放大3倍
    cout << "increase" << 3 << ":" << rect1.getArea() << endl;
    rect1.reduce(2);//缩小2倍
    cout << "reduce" << 2 << ":" << rect1.getArea() << endl;
    rect1.move(30, 40);//x,y分别加的坐标
    cout << "x,y" <<x<<y << endl;
    system("pause");
    return 0;
}



void move(int xm, int ym)//移动
    {
        x += xm;
        y += ym;
    }


rect1.move(30, 40);//x,y分别加的坐标
    cout << "x,y" <<x<<y << endl;

这里有问题,但是不知道怎么改?
搜索更多相关主题的帖子: 移动 int void move cout 
2017-10-15 11:39
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
程序代码:
class myrect
{
public:
    myrect( int x, int y, int width, int height ) : x_(x), y_(y), width_(width), height_(height)
    {
    }

    double x() const
    {
        return x_;
    }
    double y() const
    {
        return y_;
    }
    double area() const
    {
        return height_ * width_;
    }
    int girth() const
    {
        return 2*width_ + 2*height_;
    }

    void inflate( int n )
    {
        width_ *= n;
        height_ *= n;
    }
    void reduce( int p )
    {
        width_ /= p;
        height_ /= p;
    }
    void move( int xoffset, int yoffset )
    {
        x_ += xoffset;
        y_ += yoffset;
    }

private:
    int x_, y_;
    int width_, height_;
};

#include <iostream>
using namespace std;

int main( void )
{
    myrect rect(100, 100, 50, 20);
    cout << rect.area() << endl;
    cout << rect.girth() << endl;
    rect.inflate(3);
    cout << "increase " << 3 << ": " << rect.area() << endl;
    rect.reduce(2);
    cout << "reduce " << 2 << ": " << rect.area() << endl;
    rect.move( 30, 40 );
    cout << "x=" << rect.x() << ", y=" << rect.y() << endl;

    return 0;
}
2017-10-16 08:43
快速回复:初学c++,这个移动的部分错在哪里呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016078 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved