北京理工大学CPP上机实验0323

本次上机实验包括三个部分:A. 设计Circle类,实现计算圆面积的功能;B. 设计Point和Rectangle类,Rectangle从Point派生,包含构造函数与拷贝构造函数;C. 编写程序接收日期,计算其在当年的位置。要求实现类的构造函数、拷贝构造函数,并进行相应操作。
摘要由CSDN通过智能技术生成

20220323上机练习内容
Frm: https://wenku.baidu.com/view/15a5bf60b8f67c1cfad6b8d2.html

A.定义一个 Circle 类,有私有数据成员 radius(半径);
公有成员函数
getArea(),用来计算圆的面积;
实现能初始化半径的构造函数,以及拷贝构造函数。
要在主函数中定义两个对象,并且输出两个对象的面积。
其中第二个由第一个对象初始化,圆周率取为3.14。
Define a Circle class with private data member radius; public member function getArea() used to calculate the area of circle ; Implement the constructor and copy constructor that can initialize the radius. To define two objects in the main function, and output the area of the two objects. The second one is initialized by the first object, and the PI is taken as 3.14.

B.设计一个名为 Point 的点类和一个名为 Rectangle 的矩形类。
点类的属性是整型的x和y坐标。
矩形类是从点类派生的,点坐标为矩形的左下角的点坐标,并增加两个整型属性,
分别是长(X 方向)和高(Y 方向)。
同时还有获取(并计算)右上角点的成员函数 getRightUpPoint()。
要求设计实现这两个类,并且矩形类还要实现带参数的构造函数,
以及拷贝构造函数。
从文件读取数据构造一个矩形对象 R1,
使用拷贝构造函数构造矩形对象变量名为 R2,
进而调用 R2 的getRightUpPoint()得到右上角坐标,然后输出该坐标值。
写出完整的C++测试派生类构造函数、拷贝构造函数的程序代码。
Design a Point class and a Rectangle class. The property of the point class are the X and Y coordinates of the integer. The Rectangle class is derived from the Point class. The Point coordinate is the point coordinate of the lower left corner of the rectangle, and two integer property are added, which are length (x direction) and height (Y direction). At the same time, there is the member function getRightUpPoint() to obtain (and calculate) the upper right corner. It is required to design and implement these two classes, and the Rectangular class should also implement the constructor with parameters and the copy constructor. Read the data from the file to construct a rectangular object R1, use the copy constructor to construct a Rectangular object variable named R2, then call getRightUpPoint() of R2 to get the coordinates of the upper right corner, and then output the coordinate value. Write the complete program code of C + + test derived class constructor and copy constructor.

Frm: https://cloud.tencent.com/developer/article/1157340
C.求日期是该年的第几天与第几周。输入日期(年、月、日),输出它是该年的第几天。

Exercise A

#include <iostream>
using namespace std;

#define PI 3.14

class Circle
{
   
private:
    double radius;
public:
    Circle(double r);
    Circle(Circle &p);
    ~Circle();
    double getArea();
};

//能初始化半径的构造函数
Circle::Circle(double r):radius(r)
{
   
}

//拷贝构造函数
Circle::Circle(Circle &p)
{
   
    radius = p.radius;
}


Circle::~Circle()
{
   
}

double Circle::getArea()
{
   
    return PI*radius*radius;
}

int main(){
   

    Circle a(1.2);
    Circle b(a);

    double sa,sb;

    sa=a.getArea();
    sb=b.getArea();

    cout<<"A: "<<sa<<endl;
    cout<<"B: "<<sb<<endl;

    return 0;
}


Exercise B

//Point.h
#ifndef _POINT_H_
#define _POINT_H_

class Point
{
   
private:
    int x;
    int y;
public:
    Point();
    Point
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值