首页 > 其他分享 >第一次实验

第一次实验

时间:2022-09-29 23:12:31浏览次数:52  
标签:const int double 第一次 width length 实验 Rectangle

实验任务二

 1 #include<iostream>
 2 using namespace std;
 3 class Point
 4 {
 5 public:
 6     Point(int x0 = 0, int y0 = 0);
 7     Point(const Point& p);
 8     ~Point() = default;
 9     int getX() const { return x; }
10     int getY() const { return y; }
11     void show() const;
12 private:
13     int x, y;
14 };
15 //构造函数(带有默认函数值)
16 Point::Point(int x0, int y0) :x{ x0 }, y{ y0 }
17 {
18     cout << "constructor called." << endl;
19 }
20 //复制构造函数
21 Point::Point(const Point& p) :x{ p.x }, y{ p.y }
22 {
23     cout << "copy constructor called" << endl;
24 }
25 void Point::show() const
26 {
27     cout << "(" << x << "." << y << ")" << endl;
28 }
29 int main()
30 {
31     Point p1(3,9);
32     p1.show();
33 
34     Point p2 = p1;
35     p2.show();
36 
37     Point p3{ p2 };
38     p3.show();
39     cout << p3.getX() << endl;
40 
41 }

 

 

实验任务三

 

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 class Clock
 5 {
 6 public:
 7     Clock(int h = 0, int m = 0, int s = 0);
 8     Clock(const Clock& t);
 9     ~Clock() = default;
10 
11     void setTime(int h, int m = 0, int s = 0);
12     void showTime() const;
13 
14 private:
15     int hour, minute, second;
16 };
17 Clock::Clock(int hour, int minute, int second)
18 {
19     this->hour = hour;
20     this->minute = minute;
21     this->second = second;
22     cout << "constructor called" << endl;
23 }
24 Clock::Clock(const Clock& t)
25 {
26     this->hour = t.hour;
27     this->minute = t.minute;
28     this->second = t.second;
29     cout << "copy constructor called" << endl;
30 }
31 void Clock::setTime(int hour, int minute, int second)
32 {
33     this->hour = hour;
34     this->minute = minute;
35     this->second = second;
36 }
37 
38 void Clock::showTime() const
39 {
40     cout << setfill('0') << setw(2) << hour
41         << ":" << setw(2) << minute
42         << ":" << setw(2) << second << endl;
43 }
44 Clock reset()
45 {
46     return Clock(0, 0, 0);
47 }
48 int main()
49 {
50     Clock c1(13, 14, 0);
51     c1.showTime();
52 
53     c1 = reset();
54     c1.showTime();
55 
56     Clock c2(c1);
57     c2.setTime(13);
58     c2.showTime();
59 
60 }

 

 

实验任务四

 

#include<iostream>
using namespace std;
class X
{
public:
    X();
    ~X();
    X(int data);
    X(const X& obj);
    X(X&& obj) noexcept;
    void show()const;

private:
    int data;


};
X::X()
{
    data = 42;
    cout << "default constructor called." << endl;
}
X::~X()
{
    cout << "destructor called." << endl;
}
X::X(int data)
{
    this->data = data;
    cout << "constructor called." << endl;
}
X::X(const X& obj)
{
    this->data = obj.data;
    cout << "copy constructor called." << endl;
}
X::X(X&& obj) noexcept
{
    this->data = obj.data;
    cout << "move constructor called." << endl;
}
void X::show() const
{
    cout << data << endl;
}
int main()
{
    X x1; //默认构造函数被编译器自动调用
    x1.show();
    X x2{ 2049 };
    x2.show(); // 构造函数被编译器自动调用
    X x3{ x1 }; // 复制构造函数被编译器自动调用
    x3.show();
    X x4{ move(x2) }; // 移动构造函数被编译器调用
    x4.show();
}

实验分析

* line48中的x1被定义时,调用默认构造函数
* line50中的x2被定义时,调用带参数的构造函数
* line52中的x3被定义时,调用复制构造函数
* line54中的x4被定义时,调用移动构造函数
* 代码结束时,析构函数被调用x1,x2,x3,x4被逐一清理

实验任务五

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 class Rectangle
 5 {
 6 public:
 7     Rectangle();
 8     Rectangle(double l,double w);
 9     double len() const;
10     double wide() const;
11     Rectangle(const Rectangle& p);
12     double area()const;
13     double circumference()const;
14     void resize(double times);
15     void resize(double l_times, double w_times);
16 private:
17     double length, width;
18 };
19 Rectangle::Rectangle()
20 {
21     length = 2.0;
22     width = 1.0;
23 }
24 Rectangle::Rectangle(double l, double w)
25 {
26     length = l;
27     width = w;
28 }
29 Rectangle::Rectangle(const Rectangle& p)
30 {
31     length = p.length;
32     width = p.width;
33 }
34 double Rectangle::len()const
35 {
36     return length;
37 }
38 double Rectangle::wide() const
39 {
40     return width;
41 }
42 double Rectangle::area() const
43 {
44     return length * width;
45 }
46 double Rectangle::circumference()const
47 {
48     return 2 * (length + width);
49 }
50 void Rectangle::resize(double times)
51 {
52     length = length * times;
53     width = width * times;
54 }
55 void Rectangle::resize(double l_times, double w_times)
56 {
57     length = length * l_times;
58     width = width * w_times;
59 }
60 void output(const Rectangle& rect)
61 {
62 
63     cout << "矩形信息: \n"; cout << fixed << setprecision(2)
64         <<left<<setw(10)<<"长:"<< rect.len() << endl
65         << left << setw(10) << "宽:"<<rect.wide() << endl
66         << left << setw(10) << "面积:"<<rect.area() << endl
67         << left << setw(10) << "周长:"<<rect.circumference() << endl<<endl;
68 }
69 int main()
70 {
71 
72     Rectangle rect1; // 默认构造函数被调用
73     output(rect1);
74     Rectangle rect2(10, 5); // 带有两个参数的构造函数被调用
75     output(rect2);
76     Rectangle rect3(rect1); // 复制构造函数被调用
77     rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍
78     output(rect3); rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍
79     output(rect3);
80 }

 

标签:const,int,double,第一次,width,length,实验,Rectangle
From: https://www.cnblogs.com/pdywsf/p/16743457.html

相关文章

  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Pytho......
  • 第一次实验
    实验任务二#include<iostream>usingnamespacestd;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Point()=default;......
  • 实验2:Open vSwitch虚拟交换机实践
    一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Python代码运行OVS命令,控制网络拓扑中的......
  • 实验2:Open vSwitch虚拟交换机实践
    1.ovs-vsctl基础操作实践:创建OVS交换机,以ovs-xxxxxxxxx命名,其中xxxxxxxxx为本人学号。在创建的交换机上增加端口p0和p1,设置p0的端口号为100,p1的端口号为101,类型均为interna......
  • 实验1 类和对象(1)
    #include<iostream>#include<string>#include<vector>intmain(){usingnamespacestd;strings1;strings2{"cplusplus"};strings3{s2}......
  • 实验1 类和对象(1)
    实验任务2#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Point()=default......
  • Python第五章实验报告
    一、实验题目Python第五章实例和实战作业二、实验目的和要求1.熟悉Pycharm的运行环境2.学习并掌握Python的字符串及正则表达式三、主要仪器设备联想小新air15硬件:AM......
  • 实验一 类和对象(1)
    任务2://Point类//相较于教材,在构造函数的写法上,采用了业界更通用的初始化列表方式#include<iostream>usingstd::cout;usingstd::endl;//定义Point类classPoi......
  • 实验1
    #include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Poin......
  • 实验一
    #include<iostream>usingstd::cout;usingstd::endl;usingnamespacestd;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);......