首页 > 其他分享 >实验一

实验一

时间:2022-10-04 17:46:37浏览次数:38  
标签:const int double void width 实验 Rectangle

实验 2

#include<iostream>
using namespace std;
class point {public:
    point(int x0= 0,int  y0=0);
    point(const point&p);
    ~point()=default;
    int get_x() const { return x; } 
int get_y() const { return y; } 
void show() const;
private:
int x, y;
}
;
point::point(int x0, int y0): x{x0}, y{y0} {
cout << "constructor called." << endl;
}


point::point(const point& p): x{p.x}, y{p.y} {
cout << "copy constructor called." << endl;}
void point::show() const {
cout << "(" << x << ", "
<< y << ")" << endl;
}
int main() {
point p1(100, 13); 
p1.show();
point p2 = p1; 
p2.show();
point p3{p2}; 
p3.show();
cout << p3.get_x() << endl;}

实验3

#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
class Clock {
public:
    Clock(int h = 0, int m = 0, int s = 0);
    Clock(const Clock& t);
    ~Clock() = default;
    void set_time(int h, int m = 0, int s = 0);
    void show_time()const;
private:
    int hour, min, sec;
};
Clock::Clock(int h, int m, int s) :hour{ h }, min{ m }, sec{ s }
{
    cout << "constructor called" << endl;

}
Clock::Clock(const Clock& t) :hour{ t.hour }, min{ t.min }, sec{ t.sec } {
    cout << "copy constructor called" << endl;
}
void Clock::set_time(int h, int m, int s) {
    hour = h;
    min = m; sec = s;
}

void Clock::show_time() const {
    using std::setw;
    using std::setfill;
    cout << setfill('0') << setw(2) << hour << ":"
        << setw(2) << min << ":"
        << setw(2) << sec << endl;
}


Clock reset() {
    return Clock(0, 0, 0);
}
int main()
{
    Clock c1(1, 50, 5);
    c1.show_time();
    c1 = reset();
    c1.show_time();
    Clock c2(c1);
    c2.set_time(2,10,2);
    c2.show_time();
}

 

实验4

#include<iostream>
 2 using namespace std;
 3 
 4 class X {
 5 public:
 6     X();
 7     ~X();
 8     X(int m);
 9     X(const X& obj);
10     X(X&& obj)noexcept;
11     void show()const;
12 private:
13     int data;
14 };
15 
16 X::X() :data(42) { cout << "default constructor called.\n"; }
17 X::~X() { cout << "destructor called.\n"; }
18 X::X(int m) : data(m) { cout << "constructor called.\n"; }
19 X::X(const X& obj) : data(obj.data) { cout << "copy constructor called.\n"; }
20 X::X(X&&obj)noexcept:data(obj.data) { cout << "move constructor called.\n"; }
21 void X::show()const { cout << data << endl; }
22 
23 int main() {
24     X x1;
25     x1.show();
26 
27     X x2{ 2049 };
28     x2.show();
29 
30     X x3(x1);
31     x3.show();
32 
33     X x4(move(x2));
34     x4.show();
35 }

实验5

#include <iostream>
#include <iomanip>
class Rectangle {
public:
    Rectangle(double l=2.0 ,double w=1.0 );
    void output(const Rectangle& rect);
    Rectangle(const Rectangle& obj);
    void area(double l, double w);
    void circumference(double l, double w);
    void resize(int k,int j=2);
    double length, width, are, cir;


};
Rectangle::Rectangle(double l, double w) :length{ l }, width{ w } {

    area(l, w);
    circumference(l,w);


}
void Rectangle::area(double l,double w){

    are = l * w;

}
void Rectangle::circumference(double l, double w) {
     cir = 2 * (l + w);
}
Rectangle::Rectangle(const Rectangle& obj) : length{ obj.length }, width{ obj.width },are{obj.are},cir{obj.cir}
{


}
void Rectangle::resize(int k,int j) {
    length = length * k;
    width = width * j;
    area(length, width);
        circumference(length, width);

}





void output(const Rectangle& rect) {
    using namespace std;
    cout << "矩形信息: \n";
    cout << fixed << setprecision(2) << setw(5);
    cout << endl;
    cout  << "长:  "  << rect.length << endl;
    cout  << "宽:  "   << rect.width << endl;
    cout  << "面积:"   << rect.are << endl;
    cout  << "周长:"  << rect.cir << endl;
}

int main() {
    Rectangle rect1;
    output(rect1);
    Rectangle rect2(10, 5);
    output(rect2);
    
    Rectangle rect3(rect1);
    
    
    rect3.resize(2);
    
    output(rect3);
    
    rect3.resize(5, 2);
    output(rect3);
}

 

标签:const,int,double,void,width,实验,Rectangle
From: https://www.cnblogs.com/qazmlp10/p/16754111.html

相关文章

  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制......
  • 实验4:开源控制器实践——OpenDaylight
    目录一、基础要求1、利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器2、通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10......
  • 实验4:开源控制器实践——OpenDaylight
    目录实验4:开源控制器实践——OpenDaylight基本要求进阶要求个人总结实验4:开源控制器实践——OpenDaylight基本要求Mininet拓扑生成并连接控制器的结果Mininet中ping......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight基本实验1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;Mininet拓扑生成并连接控制器的结果Mininet中ping测试......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境......
  • 实验二
    一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Python代码运行OVS命令,控制网络拓扑中的O......
  • Python第五章实验报告
    一.实验项目名称:《零基础学Python》第五章实战、实例以及两道作业题二.实验目的和要求:了解和掌握操作字符串的方法和正则表达式的应用三.实验环境:IDLE(Python3.964-bit)......
  • 实验4:开源控制器实践——OpenDaylight
    一.基本要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器2.通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s......
  • 实验1
    #include<iostream>#include<iomanip>usingstd::cout;usingstd::endl;classClock{public:Clock(inth=0,intm=0,ints=0);Clock(constCl......