类练习1 rectangle类

题目:建rectangle类,有长和宽,自定义构造方法,自定义求周长函数

 

目录 

一、qt入门使用注意:

二、代码实现

1.Rectangle.h文件

 2.Rectangle.cpp文件

3.main.cpp文件 

 


一、qt入门使用注意:

  1. 一定要新建项目,没有项目无法编译源文件
  2. 新建的.cpp 和.h文件都要加入到建好的同一个项目中才可以运行!
  3. qt里类.cpp文件自己建好时会加一个类名::类名(),这个必须去掉才没有错误!
  4.  


二、代码实现

 

1.Rectangle.h文件

#ifndef RECTANGLE_H
#define RECTANGLE_H


class Rectangle
{

public:
    Rectangle();
    void setLength(int);
    void setWidth(int);
    int getLength();
    int getWidth();
    int perimeter();

private:
    int width;
    int length;

};

#endif // RECTANGLE_H
 

 

2.Rectangle.cpp文件

#include "Rectangle.h"

void Rectangle::setWidth(int width){
     this->width = width;
}
void Rectangle::setLength(int length){
    this->length = length;
}
int Rectangle::getWidth(){
    return width;
}
int Rectangle::getLength(){
    return length;
}
int Rectangle::perimeter(){
    return (length+width)*2;
}
Rectangle::Rectangle(){
    setLength(0);
    setWidth(0);
}

 

3.main.cpp文件 

#include <iostream>
#include "rectangle.h"
using namespace std;

int main(){
    Rectangle r;
    r.setWidth(5);
    r.setLength(6);
    cout << r.perimeter() << endl;
}

 

 

ps:感谢我的同班同学的帮助!今晚第一次用qt写文件,很多地方都不懂,连需要建项目都不明白,多亏了同学一步一步给我指导,帮我找错误,还教我怎么使用qt,将近一个小时都在耐心帮我解答!真的超级感谢!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <iostream> #include <string> using namespace std; struct CPoint { int x ; int y ; }; class CRectangle { private: const int id;//常量数据成员 static int total;//静态数据成员 const static string sclass; const static int f=1.0f; CPoint lefttop ; CPoint rightdown ; public: CRectangle( ); CRectangle( CPoint& lt, CPoint& rd ); CPoint GetLefttop() const { return lefttop; } CPoint GetRightdown() const { return rightdown; } void SetLefttop(CPoint & pt) { lefttop=pt; } void SetRightdown(CPoint & pt) { rightdown=pt; } int Getid() const { return id; } static int Gettotal() { return total; } int Area( ) const; int Perimeter( ) const; }; int CRectangle::total=0;//静态数据成员必须在类的外部定义(正好一次)。 const string CRectangle::sclass="CRectangle"; CRectangle::CRectangle( ):id(++total) { lefttop.x=0; lefttop.y=0; rightdown.x=1; rightdown.y=1; } CRectangle::CRectangle( CPoint& lt, CPoint& rd ):id(++total) { lefttop = lt ; rightdown = rd ; } int CRectangle::Area( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return wd * ht ; } int CRectangle::Perimeter( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return 2 * ( wd + ht ) ; } int main() { CPoint lt, rd; cin >> lt.x >> lt.y; cin >> rd.x >> rd.y; CRectangle crt(lt,rd);//调用有参构造函数 CRectangle crt2;//调用默认构造函数 //创建常量对象 const CRectangle crt3(lt,rd); cout<<"当前创建的矩形个数为:"; cout<<CRectangle::Gettotal()<<endl; //返回矩形的左上和右下点 CPoint lt1=crt.GetLefttop(); CPoint lt2=crt.GetRightdown(); //显示矩形的坐标 cout<<crt.Getid()<<"号矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示矩形的面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; //修改矩形的左上角点 cout<<"请输入矩形新的左上点坐标:"; cin>> lt.x>>lt.y; crt.SetLefttop(lt); lt1=crt.GetLefttop(); //显示修改后矩形的坐标 cout<<"矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示修改后矩形的面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值