编程矩形类
① C++编程问题:编写矩形类,要求可以提示用户输入矩形的长度和宽度,并显示该矩形的长度、宽度和面积。
按照你的要求编写的矩形类的C++程序如下
#include<iostream>
usingnamespacestd;
classRectangle{
private:
floatlength;
floatwidth;
public:
Rectangle(floatlen,floatwid);
floatarea();
};
Rectangle::Rectangle(floatlen,floatwid){
length=len;
width=wid;
}
floatRectangle::area(){
returnlength*width;
}
intmain(){
floatlength,width;
cout<<"请输入矩形的长度和宽度:"<<endl;
cin>>length>>width;
Rectanglerect(length,width);
cout<<"矩形的长度是"<<length<<endl;
cout<<"矩形的宽度是"<<width<<endl;
cout<<"矩形的面积是"<<rect.area()<<endl;
return0;
}
② JAVA 考试编程题如何编写一个矩形类
用这个肯定可以
public class Rectangle {
private float width;//长
private float height;//宽
public Rectangle(float width, float height){
this.width = width;
this.height = height;
}
//计算周长= (长+宽)*2
public float calcCircle(){
return (width + height) * 2;
}
//计算面积 长*宽
public float calcArea(){
return width * height;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
}
③ c++程序设计 设计一个矩形类(rect),
#include<iostream>
using namespace std;
class rect
{
public:
rect(int width, int length) : width(width),length(length){}
int Area()
{
return length * width;
}
int Perimeter()
{
return 2 * (length + width);
}
friend int add(rect &r1, rect &r2);
private:
int length;
int width;
};
int add(rect &r1,rect &r2)
{
return r1.Area() + r2.Area();
}
int main()
{
rect rect1(2,4), rect2(4,6);
cout<<"rect1的面积是"<<rect1.Area()<<endl;
cout<<"rect1的周长是"<<rect1.Perimeter()<<endl;
cout<<"rect2的面积是"<<rect2.Area()<<endl;
cout<<"rect2的周长是"<<rect2.Perimeter()<<endl;
cout<<"rect1 和rect2的面积之和为"<<add(rect1,rect2)<<endl;
return 0;
}
运行结果
rect1的面积是8
rect1的周长是12
rect2的面积是24
rect2的周长是20
rect1 和rect2的面积之和为32
Press any key to continue
④ C++编程:编写一个矩形rectange类。。。
程序比较长,分开发给你,按照顺序粘贴到一起就行了
类定义:
#include
#include
#include
#define
n
100
using
namespace
std;
int
num_of_students=0;
class
student
{
private:
string
name;
string
no;
string
sex;
int
age;
float
score;
int
squad;
public:
void
get_info();
void
show_info();
void
show_ord_info();
string
&get_name()
{return
name;}
string
&get_no()
{return
no;}
float
get_score()
{return
score;}
int
get_squad()
{return
squad;}
}
st[n],t;
void
student::get_info()
{
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"年龄:";
cin>>age;
cout<<"学号:";
cin>>no;
cout<<"班级:";
cin>>squad;
cout<<"成绩:";
cin>>score;
}
void
student::show_info()
{
cout<<"---------------"<
评论
0
0
加载更多
⑤ C++编写程序,建立矩形圆形类。
CTest1 #include <iostream>#include <cmath>using namespace std;int main(){ while (true) { cout << "1.圆形" << endl; cout << "2.长方形" << endl; cout << "3.直角三角形" << endl; cout << "4.退出" << endl; int choice; cin >> choice; system("cls"); if (choice == 4) break; switch (choice) { case 1:{ double r = 0; cout << "请输入圆形的半径:"; cin >> r; cout << "圆形的面积:" << 3.14 * r * r << endl << "周长:" << 3.14 * 2 * r; }break; case 2:{ double l = 0.0, w = 0.0; cout << "请输入长方形的长和宽"<<endl; cout << "长:"; cin >> l; cout << "宽:"; cin >> w; cout << "长方形的面积:" << l * w << endl << "周长:" << 2 * (l + w); }break; case 3:{ double b = 0.0, h = 0.0; cout << "请输入直角三角形的长和宽" << endl; cout << "底:"; cin >> b; cout << "高:"; cin >> h; cout << "长方形的面积:" << 0.5 * b * h << endl << "周长:" << (b + h + sqrt(b*b + h*h)); }break; default:break; } getchar(); getchar(); system("cls"); } //getchar();getchar(); system("pause"); return 0;} 执行结果: CTest2 #include <iostream>#include <cmath>using namespace std;class Circle{public: Circle(double r) : radius(r){} double area(){ return 3.14*radius*radius; } double girth(){ return 3.14 * 2 * radius; }private: double radius;};class Rect{public: Rect(double l, double w) : length(l), width(w){} double area(){ return length * width;} double girth(){ return 2 * (length + width); }private: double length; double width;};class Tri{public: Tri(double b, double h) : bottom(b), height(h){} double area(){ return 0.5*bottom*height; } double girth(){ return (bottom + height + sqrt(bottom*bottom + height*height)); }private: double bottom; double height;};int main(){ while (true) { cout << "1.圆形" << endl; cout << "2.长方形" << endl; cout << "3.直角三角形" << endl; cout << "4.退出" << endl; int choice; cin >> choice; system("cls"); if (choice == 4) break; switch (choice) { case 1:{ double r = 0; cout << "请输入圆形的半径:"; cin >> r; Circle circle(r); cout << "圆形的面积:" << circle.area() << endl << "周长:" << circle.girth(); }break; case 2:{ double l = 0.0, w = 0.0; cout << "请输入长方形的长和宽"<<endl; cout << "长:"; cin >> l; cout << "宽:"; cin >> w; Rect rect(l, w); cout << "长方形的面积:" << rect.area() << endl << "周长:" << rect.girth(); }break; case 3:{ double b = 0.0, h = 0.0; cout << "请输入直角三角形的长和宽" << endl; cout << "底:"; cin >> b; cout << "高:"; cin >> h; Tri tri(b, h); cout << "长方形的面积:" << tri.area() << endl << "周长:" << tri.girth(); }break; default:break; } getchar(); getchar(); system("cls"); } //getchar();getchar(); system("pause"); return 0;} 执行结果:
⑥ 用Java编程实现矩形类,其中包括计算矩形周长和面积的方法
英语不好,就拿拼音吧。
public class Juxing {
private double a;
private double b;
// 计算周长
public double zhouchang() {
return 2 * (a + b);
}
// 计算面积
public double mianji() {
return a * b;
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
}
⑦ 编程实现矩形类,其中包括计算矩形周长和面积的方法,并测试方法的正确性,用JAVA编写
/**
* 矩形类
*/
public class Rectangle {
private double length; //长
private double width; //宽
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public Rectangle() {
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
/**
* 面积计算方法
* @param length 长
* @param width 宽
* @return 面积
*/
public double area(){
return length*width;
}
/**
* 周长计算方法
* @param length 长
* @param width 宽
* @return 周长
*/
public double girth(){
return 2*(length+width);
}
public static void main(String[] args) {
//定义矩形,长为4.5,宽为3
Rectangle rectangle=new Rectangle(4.5,3);
System.out.println("矩形面积是:"+rectangle.area());
System.out.println("矩形周长是:"+rectangle.girth());
}
}