实验1

任务二

代码:

#include<iostream>
    using std::cout;
    using std::endl;
    
    class Point{
    public:
        Point(int x0=0,int y0=0);
        Point(const Point&p);
        ~Point()=defualt;

int get_x() const{ return x;}
int get_y() const {return y;}
    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(4, 5); 
p1.show();
Point p2 = p1; 
p2.show();
Point p3{p2}; 
cout << p3.get_x() << endl;}

更改数据后

运行结果

 

任务三

代码:

#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, minute, second;
};

Clock::Clock(int h, int m, int s): hour{h}, minute{m}, second{s} {
cout << "constructor called" << endl;
}
Clock::Clock(const Clock& t): hour{t.hour}, minute{t.minute},
second{t.second} {
cout << "copy constructor called" << endl;
}
void Clock::set_time(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
void Clock::show_time() const {
using std::setw;
using std::setfill;
cout << setfill('0') << setw(2) << hour << ":"
<< setw(2) << minute << ":"
<< setw(2) << second << endl;
}

Clock reset() {
return Clock(0, 0, 0);
}
int main()
{
Clock c1(12, 0, 5); 
c1.show_time();
c1 = reset(); 
c1.show_time();
Clock c2(c1);
c2.set_time(6);
c2.show_time();
}

 

更改数据后 运行结果

 

任务4

 

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

 

更改数据后 运行结果

第33行代码运行时调用的带默认参数的构造函数

第35行代码运行时调用的不带默认参数的构造函数

第37行代码运行时复制构造函数被调用

第39行移动构造函数被调用

程序结束之后析构函数被调用,x1,x2,x3,x4被销毁

任务5

#include <iostream>
#include <iomanip>
using namespace std;
class Rectangle {
private: double length, width;
public:
    Rectangle() { length = 2.0, width = 1.0; }

    Rectangle(double l, double w) { length = l, width = w; }
    Rectangle(const Rectangle& rect) {
        length = rect.length;
        width = rect.width;
    };
    ~Rectangle() {};
    double len() const { return length; }
    double wide() const { return width; }
    double area()const { return length * width; }
    double circumference()const { return 2 * (length + width); }
    void resize(double times) { length *= times; width *= times; }
    void resize(double l_times, double w_times) { length *= l_times; width *= w_times; }

};

void output(const Rectangle &rect) {

        cout << "矩形信息: \n";
        cout << fixed << setprecision(2)  << "" << ":  "<<std::right<< rect.len()<< endl;
        cout << fixed << setprecision(2)<< "" << ":  " << std::right  << rect.wide() << endl;
        cout << fixed << setprecision(2) << "面积" << ":  " << std::right << rect.area() << endl;
        cout << fixed << setprecision(2) << "周长" << ":  " << std::right <<rect.circumference() << 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);
}

 

posted @ 2022-09-28 19:56  akumanpower  阅读(18)  评论(0)    收藏  举报