本文主要是介绍2024.9.6 作业,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1> 手写unique_ptr指针指针
#include <iostream>using namespace std;template <typename T>
class my_unique_ptr
{
public:explicit my_unique_ptr(T *p = nullptr) noexcept // 构造函数{ptr = p;}~my_unique_ptr() noexcept //析构函数{delete ptr;}T& operator*() const // 重载*操作符{return *ptr;}T* operator->() const noexcept // 重载->操作符{return ptr;}my_unique_ptr(const my_unique_ptr &) = delete; // 禁用拷贝构造函数my_unique_ptr& operator=(const my_unique_ptr &) = delete; // 禁用拷贝赋值函数my_unique_ptr(my_unique_ptr &&other) noexcept // 移动构造函数,右值引用{ptr = other.ptr;}my_unique_ptr& operator=(my_unique_ptr &&other) noexcept // 移动赋值函数,右值引用{if(this != &other){delete ptr;this->ptr = other.ptr;other.ptr = nullptr;}return *this;}private:T *ptr; // 内置的指针。
};class Test
{
public:string name;Test() {cout<<name<<"::无参构造"<<endl;}Test(string n):name(n) {cout<<name<<"::有参构造"<<endl;}~Test() {cout<<name<<"::析构函数"<<endl;}
};int main()
{my_unique_ptr<Test> mup1(new Test("张三"));cout<<"mup1 name = "<<(*mup1).name<<endl;cout<<"mup1 name = "<<mup1->name<<endl;Test *p1 = new Test("李四");my_unique_ptr<Test> mup2(p1);cout<<"mup2 name = "<<(*mup2).name<<endl;mup2=move(mup1);cout<<"mup2 name = "<<mup2->name<<endl;return 0;
}
2> 手写登录界面,不允许拖拽,要求尽可能的美观
#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QtDebug>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QLabel *lab1 = new QLabel("账号",this);lab1->move(160,150);lab1->resize(50,50);QLabel *lab2 = new QLabel("密码",this);lab2->move(lab1->x(),lab1->y()+lab1->height()+30);lab2->resize(50,50);QLineEdit *edit1 = new QLineEdit(this);edit1->move(lab1->x()+lab1->width()+5,lab1->y());edit1->resize(350,40);edit1->setPlaceholderText("账号");QLineEdit *edit2 = new QLineEdit(this);edit2->move(lab2->x()+lab2->width()+5,lab2->y());edit2->resize(350,40);edit2->setPlaceholderText("密码");edit2->setEchoMode(QLineEdit::Password);QPushButton *btn1 = new QPushButton("登录",this);btn1->resize(70,50);btn1->move(lab2->x()+100,lab2->y()+100);QPushButton *btn2 = new QPushButton("取消",this);btn2->resize(70,50);btn2->move(btn1->x()+btn1->width()+50,btn1->y());connect(btn2, SIGNAL(clicked()), this, SLOT(close()));QLabel *lab3 = new QLabel("登录成功",this);lab3->setStyleSheet("background-color:skyblue;");lab3->setAlignment(Qt::AlignCenter);lab3->move(200,150);lab3->resize(320,170);lab3->hide();QPushButton *btn3 = new QPushButton("确定",lab3);btn3->resize(60,40);btn3->move(120,120);connect(btn3, &QPushButton::clicked, [=](){lab3->hide();});QLabel *lab4 = new QLabel("登录失败",this);lab4->setStyleSheet("background-color:skyblue;");lab4->setAlignment(Qt::AlignCenter);lab4->move(200,150);lab4->resize(320,170);lab4->hide();QPushButton *btn4 = new QPushButton("确定",lab4);btn4->resize(60,40);btn4->move(120,120);connect(btn4, &QPushButton::clicked, [=](){lab4->hide();});connect(btn1, &QPushButton::clicked, [=](){if(edit1->text()==edit2->text()){qDebug()<<"登录成功";lab3->show();}else{qDebug()<<"登录失败";lab4->show();}});
}Widget::~Widget()
{delete ui;
}
思维导图
这篇关于2024.9.6 作业的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!