作业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();
}