第五周实验指导--任务三--编写基于对象的程序,求5个长方柱的体积和表面积...

本文介绍了一个基于对象的C++程序,用于计算5个长方柱的体积和表面积。通过对象数组实现,用户可以输入第5个长方柱的尺寸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:编写基于对象的程序,求5个长方柱的体积和表面积。
* 作 者:雷恒鑫
* 完成日期:2012 年03 月19 日
* 版 本 号:V1.0

* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束

*/

#include <iostream> 

using namespace std; 

int n=0; 

class square 

{ 

public: 

    //square();

    square(int l=1,int w=2,int h=3);

    void cin_date(); 

    void volume( ); 

    void show_volume( ); 

    void areas( ); 

    void show_areas(); 

private: 

      

    int length; 

    int width; 

    int heigth; 

}; 

  

int main( ) 

{ 

    square a[5]={square (1,2,3),square (2,3,4),square (3,4,5),square ()}; 

    ++n; 

    //a[0].(1,2,3); 

    a[0].volume( ); 

    a[0].areas( ); 

    ++n; 

    //a[1].(2,3,4); 

    a[1].volume( ); 

    a[1].areas( ); 

    ++n; 

    //a[2].(2,3,5); 

    a[2].volume( ); 

    a[2].areas( ); 

    ++n; 

    a[3].volume( ); 

    a[3].areas( ); 

    ++n;

    a[4].cin_date() ; 

    a[4].volume( ); 

    a[4].areas( ); 

    system("PAUSE"); 

    return 0; 

} 

/*square::square()

{

     length=1; 

     width=2; 

     heigth=3; 

}*/

square::square(int l,int w,int h)

{

     length=1; 

     width=w; 

     heigth=h; 

}

  

void square::cin_date( ) 

{ 

      

    char c1,c2; 

    int length1,width1,heigth1; 

    cout<<"请输入"<<"第"<<n<<"个长方柱的长宽高(格式:长:宽:高 )"<<endl; 

    while(1) 

    { 

        cin>>length1>>c1>>width1>>c2>>heigth1; 

        length=length1; 

        width=width1; 

        heigth=heigth1; 

          

       if(c1!=':'||c2!=':')    

            cout<<"格式不正确,请重新输入"<<endl; 

        else 

            break; 

    } 

    return; 

} 

void square::volume( ) 

{ 

    double volume; 

    volume=length*width*heigth; 

    cout<<"第"<<n<<"个长方柱的体积为:"<<volume<<endl; 

    return; 

} 

  

  

void square::areas( ) 

{ 

    double areas; 

    areas=2*(length*width+length*heigth+width*heigth); 

    cout<<"第"<<n<<"个长方柱的表面积为:"<<areas<<endl; 

    return; 

}

运行结果:


经验积累:
1.我学会了使用对象数组。

3、按要编程:(1)定义一个Circle,该圆的数据成员包括:圆心点位置及圆的半径;方法成员有:设置圆心位置半径的方法,获取圆心位置半径的方法,无参的构造方法初始化圆心位置为(0,0),半径为1。另外定义一个构造方法可以收圆心位置与半径的参数。编写测试创建Circle对象,并且分别调用各种方法,对比这些方法的执行结果。(2)定义一个矩形MyRectangle,中有4个私有的整型变量,分别是矩形的左上角坐标(xUp,yUp)右下角坐标(xDown,yDown);中定义没有参数的构造方法有4个int参数的构造方法,用来初始化对象中还有以下方法:getw()—一计算矩形的宽度;getH()——计算矩形的高度;area()—计算矩形的面积;toString()—把矩形的宽、高面积等信息作为为宇符串返回。编写代码创建MyRectangle对象并输出矩形对象的信息。(3)设计一个方体MyCube,该包含第2题中的MyRectangle对象作为的成员变量,表示方体的底面;此外还包含一个整型变量d,表示方体的高。中定义构造方法初始化对象、定义体积表面积的方法。编写代码测试MyCube。(4)对于第2、3题的分别用的访问修饰符public与无修饰符定义,同时把它们声明在同一个包中、不同的包中,分析程序的编译情况,理解及其成员的访问控制符的作用。
06-08
(1) Circle的代码实现: ```java public class Circle { private double x; // 圆心x坐标 private double y; // 圆心y坐标 private double radius; // 圆的半径 public Circle() { x = 0; y = 0; radius = 1; } public Circle(double x, double y, double radius) { this.x = x; this.y = y; this.radius = radius; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public void setRadius(double radius) { this.radius = radius; } public double getX() { return x; } public double getY() { return y; } public double getRadius() { return radius; } public static void main(String[] args) { Circle c1 = new Circle(); System.out.println("圆心坐标为:" + c1.getX() + "," + c1.getY()); System.out.println("圆的半径为:" + c1.getRadius()); Circle c2 = new Circle(2, 3, 4); System.out.println("圆心坐标为:" + c2.getX() + "," + c2.getY()); System.out.println("圆的半径为:" + c2.getRadius()); } } ``` (2) MyRectangle的代码实现: ```java public class MyRectangle { private int xUp; // 左上角x坐标 private int yUp; // 左上角y坐标 private int xDown; // 右下角x坐标 private int yDown; // 右下角y坐标 public MyRectangle() { } public MyRectangle(int xUp, int yUp, int xDown, int yDown) { this.xUp = xUp; this.yUp = yUp; this.xDown = xDown; this.yDown = yDown; } public int getxUp() { return xUp; } public void setxUp(int xUp) { this.xUp = xUp; } public int getyUp() { return yUp; } public void setyUp(int yUp) { this.yUp = yUp; } public int getxDown() { return xDown; } public void setxDown(int xDown) { this.xDown = xDown; } public int getyDown() { return yDown; } public void setyDown(int yDown) { this.yDown = yDown; } public int getW() { return Math.abs(xDown - xUp); } public int getH() { return Math.abs(yDown - yUp); } public int area() { return getW() * getH(); } public String toString() { return "宽度:" + getW() + ",高度:" + getH() + ",面积:" + area(); } public static void main(String[] args) { MyRectangle rect = new MyRectangle(1, 2, 3, 4); System.out.println(rect.toString()); } } ``` (3) MyCube的代码实现: ```java public class MyCube { private MyRectangle bottom; // 底面矩形 private int height; // 高 public MyCube() { } public MyCube(MyRectangle bottom, int height) { this.bottom = bottom; this.height = height; } public void setBottom(MyRectangle bottom) { this.bottom = bottom; } public void setHeight(int height) { this.height = height; } public MyRectangle getBottom() { return bottom; } public int getHeight() { return height; } public int getVolume() { return bottom.area() * height; } public int getSurfaceArea() { return 2 * bottom.area() + 2 * bottom.getW() * height + 2 * bottom.getH() * height; } public static void main(String[] args) { MyRectangle bottom = new MyRectangle(1, 2, 3, 4); MyCube cube = new MyCube(bottom, 5); System.out.println("方体的底面为:" + cube.getBottom().toString()); System.out.println("方体的高为:" + cube.getHeight()); System.out.println("方体的体积为:" + cube.getVolume()); System.out.println("方体的表面积为:" + cube.getSurfaceArea()); } } ``` (4) 对于第2、3题的分别用的访问修饰符public与无修饰符定义,同时把它们声明在同一个包中、不同的包中的代码实现: Circle: ```java // 不同包中,访问权限为public package com.example.shape; public class Circle { // 省略代码 } // 同一个包中,访问权限为默认 package com.example.shape; class Circle { // 省略代码 } ``` MyRectangle: ```java // 不同包中,访问权限为public package com.example.shape; public class MyRectangle { // 省略代码 } // 同一个包中,访问权限为默认 package com.example.shape; class MyRectangle { // 省略代码 } ``` MyCube: ```java // 不同包中,访问权限为public package com.example.shape; public class MyCube { // 省略代码 } // 同一个包中,访问权限为默认 package com.example.shape; class MyCube { // 省略代码 } ``` 在同一个包中,的访问权限为默认时,可以被同一个包中的其他访问;在不同的包中,的访问权限为public时,可以被其他包中的访问。如果的访问权限为默认,则只能在同一个包中访问。如果将的访问权限改为private,则只能在该中访问,其他均无法访问。因此,及其成员的访问控制符的作用是控制及其成员的访问范围。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值