rectangle函数参数,用函数定义和调用的方法求矩形的面积。+(长宽为整数,从键盘输入)?
rectangle函数参数,用函数定义和调用的方法求矩形的面积。+(长宽为整数,从键盘输入)?详细介绍
本文目录一览: 定义一个rectangle 类,它包含两个数据成员 length 和 width ;以及包含用于求长方形面积的成员函数。
定义矩形类;
声明长和宽两个属性;
写出计算面积的函数。
class Rectangle{ private double length, width; public Rectangle(){} public Rectangle(double length, double width){ this.length = length; this.width = width; } public void setLength(double length){ this.length = length; } public double getLength(){ return this.length; } public void setWidth(double width){ this.width= width; } public double getWidth(){ return this.width; } //计算面积的函数 public double getRecArea(){ return length * width; }}
class Rectangular extend Rectangle{ private double height; public void setHeight(double height){ this.height = height; } public double getHeight(){ return height; } //计算体积的函数 public double getRecVol(){ return super.getLength * super.getWidth * height; }}public class TestRec{ public static void main(String[] args){ double vol, area = 0.0; Rectangle rec = new Rectangle(2, 3); area = rec.getRecArea(); Rectangular recr = new Rectangular(); recr.setLength(2); recr.setWidth(3); recr.setHeight(4); vol = recr.getRecVol(); System.out.println("长方形面积:" + area + ",长方体体积:" + vol); }}
#include
using namespace std;class rectangle{public: //构造函数 rectangle(int length, int width) { this->length = length; this->width = width; } //求面积 void area() { cout << "面积:" << length*width << endl; }protected: //长和宽 int length; int width;};int main(){ rectangle object(100, 200); object.area(); system("pause"); return 0;}
#include
class Rectangle{public: double Length;//长度 double Width;//宽度 Rectangle(double length, double width) {//定义一个有两个参数的构造函数,用于设置长方形的宽度和长度 this->Length = length; this->Width = width; } double Area() { //求面积函数 return Width * Length;//返回长度和宽度的乘积 }};class Rectangular :Rectangle{public: double Height; Rectangular(double length, double width, double height) :Rectangle(length, width) {//定义一个有三个参数的构造函数,用于设置立方体的宽度和长度以及高度 this->Height = height; } double Volume() {//求体积函数 return this->Area() * Height;//返回长度和宽度及高度的乘积 }};int main() { Rectangle rect(3, 6);//定义长方体类实例 printf("面积:%lf \n", rect.Area());//调用求面积方法并输出结果 Rectangular rguar(5, 10, 3);//定义立方体类实例 printf("体积:%lf", rguar.Volume());//调用求体积方法并输出结果 char c; scanf("%c", &c);//这句是为了防止控制台退出 return 0;}
以上是在Visual Studio 里写的C++语言例子
按题目要求写了上述代码,由于程序功能跟要求相同我就不再赘述了。
/* .h文件 头文件*/#ifndef __RECTANGLE_H__#define __RECTANGLE_H__class rectangle{public: rectangle(double fLength, double fWidth); ~rectangle(void);private: double m_fLength; double m_fWidth; public: double Area();};class rectangular : public rectangle{public: rectangular(double fLength, double fWidth, double fHeight); ~rectangular();private: double m_fHeight;public: double Volume();};#endif/* .cpp文件 实现文件 */#include "rectangle.h"rectangle::rectangle(double fLength, double fWidth){ m_fLength = fLength; m_fWidth = fWidth;}rectangle::~rectangle(void){}double rectangle::Area(){ return m_fLength * m_fWidth;}rectangular::rectangular(double fLength, double fWidth, double fHeight):rectangle(fLength, fWidth){ m_fHeight = fHeight;}rectangular::~rectangular(){}double rectangular::Volume(){ return Area() * m_fHeight;}#include
#include "rectangle.h"int main(void){ rectangle regle(10,20); rectangular relar(3,4,5); printf("Area = %.2f\nVolume=%.2f\n", regle.Area(), relar.Volume()); return 0;}
注意:我用的语言是C#
class Rectangle{
int length;
int width;
//自定义构造函数
public Rectangle(int length,int width){
this.length=length;
this.width=width;
}
public void Area(){
int area = length * width;
Console.WriteLine("长方形面积为" + area);
}
}
在Main函数中:
int length=?;//长度可自己设定
int width=?;//宽可自己设定int型
Rectangle rec = new Rectangle(length,width);//创建矩形对象
rec.Area();//调用函数,输出结果
#include
class Rectangle
{
public:
double Length;//长度
double Width;//宽度
Rectangle(double length, double width) {//定义一个有两个参数的构造函数,用于设置长方形的宽度和长度
this->Length = length;
this->Width = width;
}
double Area() { //求面积函数
return Width * Length;//返回长度和宽度的乘积
}
};
class Rectangular :Rectangle
{
public:
double Height;
Rectangular(double length, double width, double height) :Rectangle(length, width) {//定义一个有三个参数的构造函数,用于设置立方体的宽度和长度以及高度
this->Height = height;
}
double Volume() {//求体积函数
return this->Area() * Height;//返回长度和宽度及高度的乘积
}
};
int main() {
Rectangle rect(3, 6);//定义长方体类实例
printf("面积:%lf \n", rect.Area());//调用求面积方法并输出结果
Rectangular rguar(5, 10, 3);//定义立方体类实例
printf("体积:%lf", rguar.Volume());//调用求体积方法并输出结果
char c;
scanf("%c", &c);//这句是为了防止控制台退出
return 0;
}
public class TestRec{
public static void main(String[] args){
double vol, area = 0.0;
Rectangle rec = new Rectangle(2, 3);
area = rec.getRecArea();
Rectangular recr = new Rectangular();
recr.setLength(2);
recr.setWidth(3);
recr.setHeight(4);
vol = recr.getRecVol();
System.out.println("长方形面积:" + area + ",长方体体积:" + vol);
}
}
扩展资料:
数据成员 m_hDC CDC对象使用的输出设备上下文。
m_hAttribDC CDC对象使用的输出属性上下文。
构造函数 CDC 构造一个CDC对象。
初始化 CreateDC 为指定设备创建设备上下文。
CreateIC 为指定设备创建信息上下文。这提供了一种不创建设备上下文即获取有关设备信息的快速方式。
CreateCompatibleDC 创建内存设备上下文,与另一个设备上下文匹配。可以用它在内存中准备图像。
DeleteDC 删除CDC对象对应的Windows设备上下文。
FromHandle 给定设备上下文句柄时,返回指向CDC对象的指针。如果CDC对象未附加到句柄,则创建并附加一个临时CDC对象。
参考资料来源:百度百科-成员函数
java rectangle
pre{overflow-x: auto} ? ?
java rectangle是什么?让我们一起来了解一下吧!
java rectangle是一个“区域”类,它的最大作用就是定义一个矩形的区域。Rectangle 指定坐标空间中的一个区域,通过坐标空间中Rectangle对象左上方的点 (x,y)、宽度和高度可以定义这个区域。
其构造函数Rectangle(int x, int y, int width, int height)
height? Rectangle?的高度。? width? Rectangle?的宽度。? x Rectangle?左上角的?X?坐标。? y? Rectangle?左上角的?Y?坐标。
以下两个是其比较常用的方法:
boolean?contains(int?x,int?y)-->判定点(x,y)是否包含在指定区域内 boolean?contains(int?x,int?y,int?width,int?height)-->判定指定区域是否在其指定区域内
实战操作,具体步骤如下: public?class?Rectangle?{ ?private?double?height; ?private?double?width; ?private?String?color; ?public?double?getHeight()?{ ??return?height; ?} ?public?void?setHeight(double?height)?{ ??this.height?=?height; ?} ?public?double?getWidth()?{ ??return?width; ?} ?public?void?setWidth(double?width)?{ ??this.width?=?width; ?} ?public?String?getColor()?{ ??return?color; ?} ?public?void?setColor(String?color)?{ ??this.color?=?color; ?} ?public?Rectangle(double?width,double?height,String?color){ ??this.setColor(color); ??this.setHeight(height); ??this.setWidth(width); ?} ?public?void?getArea(){ ??double?area=0; ??area=this.height*this.width; ??System.out.println("矩形的面积为"+area); ?} ?public?String?toString(){ ??String?recStr="矩形的高度:"+this.getHeight()+"宽度:"+this.getWidth() ??+"颜色:"+this.getColor(); ??return?recStr; ?} ?/** ??*?测试函数 ??*?@param?args ??*/ ?public?static?void?main(String[]?args)?{ ??Rectangle?rec=new?Rectangle(3,?4,?"红色"); ??rec.getArea(); ??System.out.println(rec.toString()); ?} }
求助,C语言中画矩形的函数是哪个?
用lineto函数画矩形
#include
main()
{int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc");
cleardevice();
moveto(160,120);
lineto(480,120);
lineto(160,360);
lineto(160120);
getch();
closegraph();
}
图形和图像函数包含在graphics.h里面
rectangle() 画矩形函数
功能: 函数rectangle() 用当前绘图色、线型及线宽,画一个给定左上角与右下角的矩形(正方形或长方形)。
用法: 此函数调用方式为void rectangle(int left,int top,int right,int bottom);
说明: 参数left,top是左上角点坐标,right,bottom是右下角点坐标。如果有一个以上角点不在当前图形视口内,且裁剪标志clip设置的是真(1),那么调用该函数后,只有在图形视口内的矩形部分才被画出。
这个函数对应的头文件为graphics.h
返回值: 无
例: 下面的程序画一些矩形实例:
#i nclude
void main()
{
int driver,mode;
driver=DETECT;
mode=0;
initgrpah(&driver,&mode,"");
rectangle(80,80,220,200);
rectangle(140,99,180,300);
rectangle(6,6,88,88);
rectangle(168,72,260,360);
getch();
restorecrtmode();
}
rectangle命令特点和用法
Rectangle
指定坐标空间中的一个区域,通过坐标空间中
Rectangle
对象左上方的点
(x,y)、宽度和高度可以定义这个区域。
其构造函数Rectangle(int
x,
int
y,
int
width,
int
height)
height
Rectangle
的高度。
width
Rectangle
的宽度。
x
Rectangle
左上角的
X
坐标。
y
Rectangle
左上角的
Y
坐标。
Rectangle
对象的
width
和
height
是
public
字段。创建
Rectangle
的构造方法,以及可以修改该对象的方法,都允许将
width
和
height
设置为负值。
对于
width
或
height
正好为
0
的
Rectangle,它在相应轴上存在维数为
0
的位置,但在这种情况下,也可将其视为空的
Rectangle。对于这种
Rectangle,isEmpty()
方法将返回
ture。
如果两个维数中任一维数为
0,则测试空
Rectangle
是否包含某个点或是否与某个矩形相交的方法将始终返回
false。用点或矩形合并这种
Rectangle
的方法将在结果中包含
Rectangle
在该轴上的位置,正如调用
add(Point)
方法一样。
对于
width
或
height
为负值的
Rectangle,它在相应轴上并不存在维数为负的位置或维数。这种
Rectangle
将被视为不存在相应的轴上。对于包含计算,这种
Rectangle
也为空,测试它是否包含某个点或是否与某个矩形相交的方法将始终返回
false。用点或矩形合并这种
Rectangle
的方法将在生成结果时完全忽略
Rectangle。如果合并了两个
Rectangle
对象,且每个对象都具有一个负维数,则结果中至少有一个负维数。
对于仅影响
Rectangle
位置的方法,无论
Rectangle
是否沿任一轴具有负维数或
0
维数,都将在其位置上进行操作。
注意,用默认不带参数的构造方法构造的
Rectangle
将具有
0x0
维数,因此为空。Rectangle
仍然具有
(0,0)
的位置,且将该位置用于合并和添加操作。因此,尝试累积点集合范围的代码最初应使用具体的负
width
和负
height
来构造
Rectangle,或者应使用该集合中的第一个点构造
Rectangle。
solidrectangle函数是什么意思
Rectangle是长方形的意思,同时也是一个函数,使用该函数画一个矩形,可以用当前的画笔画矩形轮廓,用当前画刷进行填充。
函数原型:
BOOL Rectangle(HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
参数:
hdc:设备环境句柄。
nLeftRect:指定矩形左上角的逻辑X坐标。
nTopRect:指定矩形左上角的逻辑Y坐标。
nRightRect:指定矩形右下角的逻辑X坐标。
nBottomRect:指定矩形右下角的逻辑Y坐标。
返回值:如果函数调用成功,返回值非零,否则返回值为0。
Windows NT:若想获得更多错误信息,请调用GetLastError函数。
备注:此函数不使用和改变当前位置。
速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:1.0及以上版本;头文件:wingdi.h;库文件:gdi32.lib。
示例:
Rectangle(hdc,0,0,100,100);
“rectangle()函数”的问题。
这个函数画出来的“矩形”是在窗口里面的,窗口本身也是一 个“矩形”。
你先这样理解。
窗口是一个很小的点,通过不断放大,变成我们所看到的窗口。
当他最小时,也就是上,下,左,右都会“0”时,就只剩一个点。
然后,随各个参数的扩大而变大。
当你调用这个函数,并画出图形时,会 另外弹出一个窗口。
假设这个窗口的大小为,100,100,100,100;
此时,这个rectangle,你调用它时,它的参照物,就是这个窗口。
你可以看到,你rectangle(50,50,50,50)的 时候,
这个图形的 起始位置就是在宽为50的地方开始绘出。
能理解不?
此函数调用方式为void rectangle(int left,int top,int right,int bottom)。
1、rect结构定义了一个矩形框左上角以及右下角的坐标,RECT结构通常用于Windows编程。
typedef struct _RECT {
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT, *PRECT;
2、成员
left : 指定矩形框左上角的x坐标
top: 指定矩形框左上角的y坐标
right: 指定矩形框右下角的x坐标
bottom:指定矩形框右下角的y坐标
3、备注:
当rect传递给fillrect函数时,该矩形框将被填充满,但不包括右栏和底行的像素。
该结构与rectl结构是同一结构。
用函数定义和调用的方法求矩形的面积。+(长宽为整数,从键盘输入)?
Rectangle a = new Rectangle(2,2)是在实例化一个Rectangle对象
那么他调用的是Rectangle类的构造函数。
但是你自己写的Rectangle类没有自己实现构造函数,所以它只有一个无参的构造函数(所有的类在C#里面会有一个无参的构造函数)。那么你调用Rectangle a = new Rectangle()是可以编译通过的。
但是如果你希望在实例化的时候就能设置矩形的长宽的话,你需要自己在Rectangle里面自己实现一个带两个参数的构造函数:
public Rectangle(int x, int y)
{
this.x=x;
this.y=y;
}
这样在初始化这个矩形对象的时候也可以初始化它的长宽。
但是这个时候隐式的那个无参的构造函数将会破坏掉。这是后话,跟这个问题没什么关系了。
不知道我解释清楚没有。
请参见注释
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp03
{
class Program
{
static void Main(string[] args)
{
// 实例化矩形a
Rectangle a = new Rectangle(2,2);
// 实例化矩形b
Rectangle b = new Rectangle(3,3);
// 矩形a与矩形b相加(后面重载了操作符‘+’)
Console.WriteLine(a+b);
}
}
//矩形类
class Rectangle
{
// x,y分别代表长,宽
public int x;
public int y;
public Rectangle(int x, int y)
{
this.x=x;
this.y=y;
}
public int X
{
get {return x;}
set { this.x = value; }
}
public int Y
{
get { return y; }
set { this.y = value; }
}
// 这里重载操作符号‘+’,上面的主函数为什么能够两个矩形//相加就是因为这个原因。
//这里这个+号的意义变成了两个矩形的面积和。
public static double operator +(Rectangle R1, Rectangle R2)
{
return R1.X * R1.Y + R2.X * R2.Y;
}
}
}
C#的Rectangle坐标只能是Int么?有没有可以是double的?
可以用RectangleF,它的参数是float类型的
只能是Int型
System.Drawing.Rectangle的构造函数是Rectangle(Int32, Int32, Int32, Int32)和Rectangle(Point, Size)(Point和Size也是)
如果你需要更精确的,可以使用Region或者直接是RectangleF
定义一个类rectangle,描述一个矩形,包含有长、宽两种属性,以及计算面积的方法;
class Recangle{
private:
int width;
int length;
public:
Recangle()
{width = 0; length = 0;};
Recangle(int w,int h)
{width = w; height = h;};
int circumference()
{return 2*(width+height)};
int Area()
{return width * height};
void changeRec(int w, int h)
{width = w; height = h;};
};
参数:
hdc:设备环境句柄。
nLeftRect:指定矩形左上角的逻辑X坐标。
nTopRect:指定矩形左上角的逻辑Y坐标。
nRightRect:指定矩形右下角的逻辑X坐标。
nBottomRect:指定矩形右下角的逻辑Y坐标。
返回值:如果函数调用成功,返回值非零,否则返回值为0。
Windows NT:若想获得更多错误信息,请调用GetLastError函数。
以上内容参考:百度百科-Rectangle
import java.util.Scanner;
import java.lang.Math;
public class rectangClass{
double x=0,y=0,x1=0,y1=0;
public rectangClass(double x,double y,double x1,double y1){
Scanner input=new Scanner(System.in);
this.x=x;
this.y=y;
this.x1=x1;
this.y1=y1;
}
public double area()
{
double area=0;
System.out.println("x="+x);
System.out.println("y="+y);
System.out.println("x1="+x1);
System.out.println("y1="+y1);
area=((x1-x)*(y1-y));
System.out.println("area="+area);
return area;
}
public static void main(String[] args){
rectangClass pt=new rectangClass(0,0,8.5,9);
pt.area();
}
}
class Rect{private int _len;
private int _width;public Rect(int len,int width){this._len = len;
this._width = width;}//定义面积只读属性
public int Area{Get{return _lenth * _width。
扩展资料:
花括号内为函数体。如果没有返回值类型名为"void", 整数类型int,类型返回值为整数类型int,以此类推。类型名有:void int long float int* long* float* 。
C++中函数的调用:函数必须声明后才可以被调用。调用格式为:函数名(实参)。调用时函数名后的小括号中的实参必须和声明函数时的函数括号中的形参个数相同。有返回值的函数可以进行计算,也可以做为右值进行赋值。
参考资料来源:百度百科-Rectangle