MaskerQwQ

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

实验任务二:

#include<bits/stdc++.h>

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(1,5);
    p1.show();
    
    Point p2=p1;
    p2.show();
    
    Point p3{p2};
    p3.show();
    cout<<p3.get_x()<<endl;
  
  return 0; }

实验任务二运行结果截图: 

 实验任务三:

#include<bits/stdc++.h>
#include<iomanip>

using namespace std;

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<<"costructor called"<<endl;
}

Clock::Clock(const Clock& t):hour{t.hour},minute{t.minute},second{t.second}{
    cout<<"copy construvtor called"<<endl;
}

void Clock::set_time(int h,int m,int s){
    hour=h;
    minute=m;
    second=s;
}
void Clock::show_time()const{
    cout<<setfill('0')<<setw(2)<<hour<<":"<<setw(2)<<minute<<":"<<setw(2)<<second<<endl;
}

Clock reset(){
    return Clock(0,0,0);
}

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

实验任务三运行结果截图:

 实验任务四:

#include<bits/stdc++.h>

using namespace std;

class X{
    public:
        X();
        ~X();
        X(int m);
        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 m):data{m} {
    cout<<"constructor called."<<endl;
}

X::X(const X& obj): data{obj.data} {
    cout<<"copy constructor called."<<endl;
}

X::X(X&& obj) noexcept: 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();
    
    return 0;
}

实验任务四运行结果截图:

 

X x1;执行时,构造函数被调用,调用的是X();

X x2{2049};执行时,构造函数被调用,调用的是X(int m);

X x3{x1};执行时,构造函数被调用,调用的是X(const X& obj);

X x4{ move(x2) };执行时,构造函数被调用,调用的是X(X&& obj) noexcept;

程序执行到最后一行的 } 时,析构函数被调用,调用的顺序为x4,x3,x2,x1

 实验任务五:

#include<bits/stdc++.h>
#include<iomanip>
using namespace std;

class Rectangle{
    public:
        Rectangle();
        Rectangle(double l,double w);
        Rectangle(const Rectangle& obj);

        double len() const{
            return length;
            }
        double wide() const{
            return width;
            }
        double area() const{
            return width*length;
            }
        double circumference() const{
            return 2*(length+width);
            }
        void resize(double times);
        void resize(double l_times,double w_times);
    private:
        double length,width;
};

Rectangle::Rectangle(){
    length=2.0;
    width=1.0;
}

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

Rectangle::Rectangle(const Rectangle& obj) : length{obj.length},width{obj.width}{}

void Rectangle::resize(double times){
    length*=times;
    width*=times;
}

void Rectangle::resize(double l_times,double w_times){
    length*=l_times;
    width*=w_times;
}

void output(const Rectangle &rect){
    
    cout<<"矩形信息: "<<endl;
    cout<<fixed<<setprecision(2);
    cout<<"长:    "<<rect.len()<<endl;
    cout<<"宽:    "<<rect.wide()<<endl;
    cout<<"面积:  "<<rect.area()<<endl;
    cout<<"周长:  "<<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 on 2022-09-30 12:09  MaskerQwQ  阅读(11)  评论(0编辑  收藏  举报