首页 > 其他分享 >实验一 类和对象

实验一 类和对象

时间:2022-09-30 12:23:59浏览次数:81  
标签:const width 对象 double times int 实验 Rectangle

实验任务二:

#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);
    
}

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

 

标签:const,width,对象,double,times,int,实验,Rectangle
From: https://www.cnblogs.com/MaskerQwQ/p/16738774.html

相关文章

  • List转为String数组 List对象.toArray(new String[0])
    List转为String数组List对象.toArray(newString[0])privateString[]getStringArray(){returnnewString[]{"one","two","three"};}@Testpublicvoidtes......
  • 实验1 类和对象(1)
    #include<iostream>#include<string>#include<vector>intmain(){usingnamespacestd;strings1;strings2{"cplusplus"};strings3{s2......
  • 2022-09-30 数据源对象,容器
    目录spring数据源对象管理DruidDataSourceComboPooledDataSource加载properties文件容器创建容器获取bean核心容器总结容器相关bean相关依赖注入相关spring数据源对象管......
  • json字符串与json对象之间的转换
    一、认识json1.什么是json?JSON(JavaScriptObjectNotation,JS对象标记)是一种轻量级的数据交换格式,用完全独立于编程语言的文本格式来存储和传输数据。(可以用......
  • 《MiniPRO H750开发指南》第三十八章 红外遥控实验
    第三十八章红外遥控实验​本章,我们将介绍STM32对红外遥控器的信号解码。STM32板子上标配的红外接收头和一个小巧的红外遥控器。我们将利用STM32的输入捕获功能,解码开发板标......
  • 提取数组中对象
    例如fileDate=[{deleteValue:0,downloadValue:0},{deleteValue:1,downloadValue:1},{deleteValue:2,downloadValue:0......
  • ...args剩余参数和 arguments对象的区别
    一、...args剩余参数(展开运算符)允许一个表达式在某处展开。展开运算法 在 多个参数(函数调用)、多个元素(用于数组和字面量)和多个变量(用于解构赋值) 地方使用。剩余参数语......
  • 实验1
    实验1#include<iostream>#include<string>#include<vector>intmain(){usingnamespacestd;strings1;strings2{"cplusplus"};strings3{s2......
  • 面向对象编程
    面向过程&面向对象面向过程思想步骤清晰简单,第一步做什么,第二步做什么......面对过程适合处理一些较为简单的问题面向对象思想物以类聚,分类的思维模式,思考问题......
  • 实验一:类和对象
     task21#include<iostream>2usingstd::cout;3usingstd::endl;45classPoint{6public:7Point(intx0=0,inty0=0);//构......