用c++编写一个矩形类

作者&投稿:晁响 (若有异议请与网页底部的电邮联系)
采用c++定义并实现一个矩形(Rectangle)类~

代码如下:
#include #include using namespace std;class Rectangle {public:Rectangle() : length(length), width(width), color("") {}int GetLength() {return this->length;}void SetLength(int length) {this->length = length;}int GetWidth() {return this->width;}void SetWidth(int width) {this->width = width;}string GetColor() {return this->color;}void SetColor(const string& color) {this->color = color;}int GetArea() {return this->length * this->width;}bool IsSquare() {return this->length == this->width;}private:int length;int width;string color;};int main(){Rectangle rect;rect.SetLength(100);rect.SetWidth(100);rect.SetColor("红色");cout << "矩形:
长:" << rect.GetLength() << ",宽:" << rect.GetWidth() << ",颜色:" << rect.GetColor() << endl<< "面积:" << rect.GetArea() << endl << "是否是正方形:" << (rect.IsSquare() ? "是" : "否") << endl;system("pause");return 0;}

代码如下:
class Rectangle
{
public:
Rectangle()
{
x1=0;
x2=0;
y1=0;
y2=0;
}
Rectangle(double a1,double b1,double a2,double b2)
{
x1=a1;
y1=b1;
x2=a2;
y2=b2;
}
void input()
{
cout<<"请输入矩形左下角和右上角顶点的坐标:"<<endl;
cin>>x1>>y1>>x2>>y2;
}
void output()
{
cout<<"该矩形的面积为:"<<((x2-x1)*(y2-y1))<<endl;
}
private:
double x1,x2,y1,y2;
};

扩展资料
类是现实世界在计算机中的反映,它将数据和对这些数据的操作封装在一起(并没有开空间)。
类的三大特性:
1、封装
2、继承
3、多态
封装:函数的封装是一种形式,隐藏对象的属性和实现细节(函数内部),仅仅对外提高函数的接口和对象进行交互。
类的访问限定符可以协助其完成封装。
类的三个访问限定符:
1、public:公有的,类的成员可以从类外直接访问
2、private/protected:类的成员不能从类外直接访问
3、类的每个访问限定符可以多次在类中使用,作用域为从该限定符开始到下一个限定符之前/类结束
4、类中如果没有定义限定符,则默认为私有的(private)
5、类的访问限定符体现了类的封装性
参考资料来源:
百度百科——类

#include <iostream>
using namespace std;

class Rectangle {
  private:
    double length;
    double height;
    double area;
    double circumference;
  public:
    Rectangle() {}
    Rectangle(double l, double h) {
      length = l; height=h;
    }
    Rectangle(const Rectangle &r) {
        length = r.length;
        height = r.height;
    }

    ~Rectangle() {
        cout << "destruct function ... " << endl;
    }

    void setLength(double l) {length = l;}
    void setHeight(double h) {height = h;}
    double calcCircumference() {circumference = 2*(length+height); return circumference;}
    double calcArea() {area=length*height;return area;}
    double getCircumference() {return circumference;}
    double getArea() {return area;}

};

int main(){
    Rectangle a(100,20);
    Rectangle b(a);

    cout << "Area : " << a.calcArea() <<endl;
    cout << "Circumference : " << a.calcCircumference() << endl;
}



C语言编写一个矩形求微积分的程序
\/*被积函数由宏定义给出*\/ define f(x) ((x)*(x)+2*(x))main(){ int i,n;float a,b,t,m,s=0;printf("请输入积分上下限");scanf("%f%f",&a,&b);printf("请输入精确度m");scanf("%f",&m);for(n=1;;n++){ t=(b-a)\/n;if(t<=m)break;} for(i=0;i<n;i++...

C程序设计 题目 在屏幕上画一个矩形
while(!feof(in))fputc(fgetc(in),stdout);这个循环的结束条件是 fgetc(in) 返回了结束符,就是结尾多出来的那个字符 所以加上 结束符 EOF 的判断 就可以了 include<stdio.h> include"string.h"include"stdlib.h"void main(){ FILE *in;char ch;char *article;char inFile[30];printf("...

编写一个C程序,从键盘上输入矩形的长和宽,屏幕上显示对应的矩形周长和面...
include<stdio.h> int main(){ double a,b ;cin>>a>>b ;printf("S=%f,C=%f",a*b,(a+b)*2) ;return 0 ;}

用C语言写个计算矩形的面积
include<stdio.h> int main(){ int c,a;char b=1;while(b!=0){ printf("请输入矩形长:");scanf("%d",&c);printf("请输入矩形宽:");scanf("%d",&a);printf("矩形面积:%d\\n请选择:\\n0:返回主菜单 任意键:再来一次",a*c);scanf("%c",&b);} return 0;} ...

用c语言画矩形
每行起始和结束字符均是你的第3个参数 矩形第1行和最后一行中间是第3个参数,其他行根据第4个参数决定是空格或者第3个参数 程序可以这样写:...for ( m=0;m<a;m++ ){ printf("%c",c); \/\/第1列 if ( m==0 || m==a-1 ) \/\/第1行和最后一行 for ( n=1;n<b-1;n++ ) ...

c语言作业(´Д`) 编写一个程序 从键盘输入矩形的两条边长,计算该...
include<stdio.h>int main(void){ int a,b; int s ; scanf("%d%d",&a,&b); s = a*b; printf("s = a*b = %d\\n",s); return 0;}

c语言中怎样算矩形面积
在这个程序中,我们首先定义了三个变量:`length`、`width` 和 `area`,分别用来存储矩形的长、宽和面积。然后我们使用 `printf` 和 `scanf` 函数从用户处获取输入的长和宽。我们用长和宽相乘来计算面积,并使用 `printf` 函数打印结果。要运行这个程序,你需要将代码粘贴到一个 `.c` 文件中(...

C语言如何编写旋转矩阵源代码
这里以顺时针旋转90°为例:include<stdio.h>#include<stdlib.h>#include#define N 4void main(){int i,j;int a[N*N],b[N][N];\/\/这里设置旋转为4*4的矩形,自己在这里改成其它的矩形int *p=a;\/\/用指针来指向这个一维数组。这样在旋转赋值的时候会轻松很多srand(time(NULL));for(i=0;...

1、定义一个矩形类CRectangle,要求具有下列成员函数: 1) CRectangle(i...
p1=f1.getPerim();p2=f2.getPerim();if(p1==p2)cout<<"两个图形的周长相等,"<<endl;else if(p1>p2)cout<<"第一个个图形的周长大,"<<endl;else cout<<"第二个图形的周长大,"<<endl;} void main(){ Rectangel r(3,4);Circle c(2);Figure &Ff1=r,&Ff2=c;Ap(Ff1,...

C语言,给定一个矩形和一个点,判断这个点与矩形的位置关系(顶点 边 矩形...
y>a+c+d))return 1;else if((x>a)&&(xb)&&(y

玛多县19812516158: C++:定义一个描述矩形的类Rectangle,该类种包括: 两个数据成员:宽(width)和长(length); 3个成员函数:赋初值函数、计算矩形的周长、计算矩形的面积 -
豆卢袁枣参: #include <iostream.h> class CFigure{//基类CFigure,图形类 double width;//共同属性 public: CFigure(double w){ width = w; cout << "创建图形:"; } double GetWidth(void){return width;}//返回共同属性 double Area(void); }; class CRect:public ...

玛多县19812516158: 采用C++定义一个矩形 类(Rectangle). -
豆卢袁枣参: class Rectangle { private:int length; int width; public:Rectangle(int nl = 0,int nw = 0):length(nl),width(nw) {} ~Rectangle() {} public:void set(int nl,int nw ) {length = nl;width = nw;} void get(int& nl,int& nw) {nl = length;nw = width;} int area() {return length * width;} };

玛多县19812516158: 用c++编写一个矩形类 -
豆卢袁枣参: //main.cpp #include using namespace std; #include "rectangle.h" int main(void); int main(void) { double li; double wi; cout<<"input the width :"< cin>>wi; cout<<"input the length :"< cin>>li; rectangle mc(li,wi); mc.print(); return 0; } //rectangle....

玛多县19812516158: C++设计一个矩形类(Rectangle),可以求矩形的周长和面积?急急急!!!求高手帮忙? -
豆卢袁枣参: #include<stdio.h> class Rectangle{ private:int m_nWidth;int m_nHeight; public:Rectangle(){;}~Rectangle(){;}void SetWidth( const int width ){ m_nWidth = width; }void SetHeight( const int height){ m_nHeight = height; }int GetGirth(){ return 2 * ...

玛多县19812516158: 编写矩形类,要求可以提示用户输入矩形的长度和宽度,并显示该矩形的长度和宽度和面积.(c++程序) -
豆卢袁枣参: #include<iostream> using namespace std;int main() {int w,l;cout<<"输入长度";cin>>l;cout<<"输入宽度";cin>>w;cout<<"长度="<<l<<",宽度="<<w<<".面积="<<w*l<<endl;return 0;}

玛多县19812516158: 请教一个C++问题. 定义矩形类 -
豆卢袁枣参: /*左上角坐标:(10,10) 长: 16 高: 9 左上角坐标:(12,8) 长: 16 高: 9 左上角坐标:(10,10) 长: 16 高: 9 Press any key to continue */ #include <iostream>using namespace std;class Rectangle {double x;double y;double width;...

玛多县19812516158: 用C++语言“定义并实现一个矩形类,有长、宽两个属性,用成员函数计算矩形的面积”.(编出程序拿分) -
豆卢袁枣参: //main.cpp文件内容 #include #include #include "rectangle.h" using namespace std; int main(int argc, char *argv[]) { Rectangle rect1; rect1.setLengthAndWidth(); rect1.showArea(); system("PAUSE"); return 0; } //Rectangle.h文件内容 #include ...

玛多县19812516158: C++问题:设计一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角两个点的坐标,能计算举行的面积 -
豆卢袁枣参: class Point{ protected:double x;double y; public:Point(double a,double b){x=a;y=b;}Point(){x=0.0;y=0.0;}double GetX(){return x;}double GetY(){return y;} }; class Rect{ protected:Point p1;Point p2;//p1是左下角,p2是右上角 public:...

玛多县19812516158: 用C++定义一个矩形类,类中包括矩形的定义及相关操作,如移动,旋转和缩放! 谢谢解答! -
豆卢袁枣参:[答案] 我说下思路吧.画一个矩阵只需要知道左上点和右下点的坐标,故类中需相应的成员变量保存这两点.不知道你说的旋转是以什么点为中心来转,就假定为矩形的中心吧,旋转还要有个旋转角度吧?另外图形的旋转牵涉到矩阵的乘法,...

玛多县19812516158: C++编程题,急~!题目:1.定义一个矩形类Crectangle
豆卢袁枣参: 1.定义一个矩形类Crectangle,要求具有下列成员函数:1)实现矩形的周长:2)Area(girth():):实现矩形的面积classCrectangle{ ();floatgirth();Crectangle();virtual~Crectangle(); };Crectangle::Crectangle(){cout>lenght;cout>width;}Crectangle::~Crectangle(){}floatCrectangle::girth(){cout 全部

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网