3-4 长方形的周长和面积计算

Time Limit: 1000MS Memory limit: 65536K

标题叙述性说明

通过本题的练习能够掌握拷贝构造函数的定义和用法;
设计一个长方形类Rect。计算长方形的周长与面积。

类中有私有数据成员Length(长)、Width(宽)。由具有缺省參数值的构造函数对其初始化,函数原型为:Rect(double Length=0, double Width=0); 再为其定义拷贝构造函数,形參为对象的常引用。函数原型为:Rect(const Rect &); 编写主函数。创建Rect对象r1初始化为长、宽数据,利用r1初始化还有一个Rect对象r2。分别输出对象的长和宽、周长和面积。

 
 
要求: 创建对象 Rect r1(3.0,2.0),r2(r1);

输入

输入两个实数,中间用一个空格间隔;代表长方形的长和宽

输出

共同拥有6行;

分别输出r1的长和宽。r1的周长;r1的面积;r2的长和宽;r2的周长;r2的面积。注意单词与单词之间用一个空格间隔

演示样例输入

56 32

演示样例输出

the length and width of r1 is:56,32
the perimeter of r1 is:176
the area of r1 is:1792
the length and width of r2 is:56,32
the perimeter of r2 is:176
the area of r2 is:1792

提示

输入-7.0 -8.0

输出

the length and width of r1 is:0,0

the perimeter of r1 is:0

the area of r1 is:0

the length and width of r2 is:0,0

the perimeter of r2 is:0

the area of r2 is:0

来源

 黄晶晶

演示样例程序

#include <iostream>

using namespace std;

class Rect
{
private:
double l;
double w; public:
Rect (double x=0,double y=0);
Rect (const Rect &a); const void display1()
{
cout<<"the length and width of r1 is:"<<l<<","<<w<<endl;
cout<<"the perimeter of r1 is:"<<(l+w)*2<<endl;
cout<<"the area of r1 is:"<<l*w<<endl;
} const void display2()
{
cout<<"the length and width of r2 is:"<<l<<","<<w<<endl;
cout<<"the perimeter of r2 is:"<<(l+w)*2<<endl;
cout<<"the area of r2 is:"<<l*w<<endl;
}
}; Rect::Rect(double x,double y)
{
l=x;
w=y;
} Rect::Rect(const Rect & a)
{
l=a.l;
w=a.w;
} int main()
{
double x,y;
cin>>x>>y;
if(x<0|| y<0)
{
x=0;
y=0;
}
Rect rect(x,y);
Rect rect_copy=rect; rect.display1();
rect_copy.display2();
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

sdut 3-4 长方形的周长和面积计算的更多相关文章

  1. Java练习 SDUT-3339_计算长方形的周长和面积(类和对象)

    计算长方形的周长和面积(类和对象) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 设计一个长方形类Rect,计算长方形 ...

  2. HDU 1828 Picture(长方形的周长和)

    HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...

  3. 【DTOJ】1001:长方形周长和面积

    DTOJ 1001:长方形周长和面积  解题报告 2017.11.05 第一版  ——由翱翔的逗比w原创 题目信息: 题目描述 已知长方形的长和宽,求长方形的周长和面积? 输入 一行:空格隔开的两个整 ...

  4. C#编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出

    编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出. 代码: using System; using System.Collections.Generic; using Syst ...

  5. Java面向对象5(V~Z)

    计算各种图形的周长(接口与多态)(SDUT 3338) import java.util.Scanner; public class Main { public static void main(St ...

  6. Java面向对象练习

    1.定义长方形类,含: 属性:宽.高(整型): 方法:求周长.面积: 构造方法3个:(1)无参——宽.高默认值为1:(2)1个参数——宽.高均为参数值:(3)2个参数——宽.高各为参数值. 要求:进行 ...

  7. 1028作业:Java语法基础

    //1.输入长与宽计算长方形面积与周长.(用精度类型计算) package com.lovo.J133; import java.util.Scanner; public class Homework ...

  8. C#知识点有必要知会

    如果说你父类里面有一个成员比如int a;那么你子类里面也可以再定义一个int a,这个时候base.a和this.a表示的就不是一个变量了,如果说子类里面没有,那么base.a和this.a表示的都 ...

  9. Python ~~~ 面向对象的利器

    class Rectangle(): # 有没有括号都行 . def __init__(self,x,y): self.x=x self.y=y def getPeri(self): def getA ...

随机推荐

  1. Oracle Net Listener Parameters (listener.ora)(转)

    12/20 7 Oracle Net Listener Parameters (listener.ora) This chapter provides a complete listing of th ...

  2. 腾讯2014在广州站实习生offer经验(TEG-开发背景)

    研究在过去的一年是linux 什么系统编程和网络编程.比较熟悉的语言c/c++,python只写一些测试client.是后台开发类,比方前面笔面的网易CC(面完hr后挂).大概3月15号就在腾讯 jo ...

  3. Thread thread2 = new Thread()

    Thread thread2 = new Thread() { @Override public void run() { test.function(); } }; thread1.start(); ...

  4. Data source rejected establishment of connection, message from server: &quot;Too many connections&quot;

    错误叙述性说明: 測试一段时间没有不论什么问题.今天突然用户无法登录,报错如Data source rejected establishment of connection,  message fro ...

  5. ecshop广告调用方法

    在简单地概括ecshop广告调用该方法,已发表在博客上,在这里,我们总结了以下 :就是官方默认的方法.先加入广告位,然后加入模板的广告位区域,再在将两者相应上. 1.后台 > 广告管理 > ...

  6. jquery :操作iframe

    原文 jquery :操作iframe 1. 内容里有两个ifame <iframe id="leftiframe"...</iframe> <iframe ...

  7. poj3903 Stock Exchange(最长上升子序列)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3903">http://poj.org/problem?id=3903 Descrip ...

  8. sdut 在机器上面向对象编程练习11(运算符重载)

    在机器上面向对象编程练习11(运算符重载) Time Limit: 1000MS Memory limit: 65536K 标题叙述性说明 有两个矩阵a和b,均为2行3列,求两个矩阵之和.重载运算符& ...

  9. NSIS:判断程序是否运行并进行卸载

    原文NSIS:判断程序是否运行并进行卸载 今天在评论里看到网友说要一个这样的功能,就简单写了一个,本来想做360杀手来着,但遗憾的是我从来不用360的东西,所在电脑上也没有360相关的软件进行测试,所 ...

  10. Windows下一个MySQL有些错误的解决方法

    1.无论是什么提示.我们有一个直接看错误日志.由于它描述了最具体描述错误日志. 于MySQL安装文件夹中找到 my.ini简介 看日志保存路径 2. 我的错误是[ERROR] Fatal error: ...