10月11日作业

作业1:

实现对象树模型

#include <iostream>
#include<list>

using namespace std;

class Obj{
public:
    list<Obj*> child;
    Obj(Obj *parent=nullptr){
        if(parent!=nullptr){
            parent->child.push_back(this);
        }
    }
    virtual ~Obj(){
        cout<<"aaa"<<endl;
        for(auto it=child.begin();it!=child.end();it++){
            delete *it;
        }
    }
};

class A:public Obj{
public:
    A(Obj *parent=nullptr){
        cout<<"A::构造"<<endl;
        if(parent!=nullptr){
            parent->child.push_back(this);
        }
    }
    ~A(){

       cout<<"A::析构"<<endl;
    }
};

class B:public Obj{
public:
    B(Obj *parent=nullptr){
        cout<<"B::构造"<<endl;
        if(parent!=nullptr){
            parent->child.push_back(this);

        }
    }
    ~B(){
       cout<<"B::析构"<<endl;

    }
};
int main()
{
    A a;
    B *b=new B(&a);
    return 0;
}

作业2:

实现信号和槽的代码

源文件:

#include "mywid.h"


MyWid::MyWid(QWidget *parent)
    : QWidget(parent)
{
    this->resize(800,800);
    btn1=new QPushButton("未知",this);
    btn1->resize(200,100);
    btn2=new QPushButton("???",this);
    btn2->resize(btn1->size());
    btn2->move(btn1->width(),0);
    btn3=new QPushButton("显示",this);
    btn3->resize(btn1->size());
    btn3->move(0,btn1->height());


    connect(btn1,&QPushButton::clicked,
            this,&MyWid::this_close);
    connect(btn3,&QPushButton::clicked,
            this,&MyWid::btn1_set);
    connect(btn3,&QPushButton::clicked,
            this,&MyWid::btn2_set);
}

MyWid::~MyWid()
{

}

void MyWid::this_close()
{
    this->close();
}

void MyWid::btn1_set()
{
    btn1->setText("close");
}

void MyWid::btn2_set()
{
    btn2->setText("hello world");
}



头文件:

#ifndef MYWID_H
#define MYWID_H

#include <QWidget>
#include<QtDebug>
#include<QPushButton>//按钮类头文件
#include <QApplication>
class MyWid : public QWidget
{
    Q_OBJECT

public:
    MyWid(QWidget *parent = nullptr);
    ~MyWid();
public slots:
    void this_close();
    void btn1_set();
    void btn2_set();
private:
    QPushButton *btn1,*btn2,*btn3;

};
#endif // MYWID_H

测试文件:

#include "mywid.h"



int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWid w;
    w.show();
    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值