【C/C++学院】0820-Nullptr/const对象/类指针引用以及mallocfree与newde/类重载运算符/QT加法重载/类的重载赋值运算/自增在前在后差别/赋值重载深浅拷贝/重载下标

Nullptr

#include<iostream>

void go(int num)
{
	std::cout << "gonum" << std::endl;
}

void  go(void *p)
{
	std::cout << "gop" << std::endl;
}

void main()
{
	//void *p = nullptr;  
	void *p = NULL;//C++是强类型,严格的类型检查
	//go(p);//根据类型来处理,go(p)  gop
	go(NULL);//造成歧义,C++用Nullptr,gonum
	go(nullptr);//C++的空指针,类型

	std::cin.get();
}

const对象

#include<iostream>

class  area
{
public:
	int x;
	int y;
	mutable int  z;//不受const约束的类成员
	area() :x(10), y(10)
	{

	}
	void printxy() const
	{
		z = z + 1;
		std::cout << x << "  " << y << "\n";
	}
	void add(int a)
	{
		x += a;
		y -= a;
	}
	void go() const
	{

	}
protected:
private:
};

void main()
{
	{
		//const area *p1;
		//area const *p2;
		//const area * const p3;
		//area *const p4;
	}
	//const对象不可以引用非const成员函数
	//不可以改变内部变量,mutable例外
	const area * const p = new area;
	p->go();
	//p->add();
	//p = new area;//指针可以改变

	const area area1;
	area1.printxy();
	//area1.add(1);
	area1.go();
	//area1.x = 10;
	area1.z += 1;
	std::cin.get();
}

类指针引用以及mallocfreenewdelete差别

#include<iostream>
#include <stdlib.h>

class myclass
{
public:
	int x;
	int y;
public:
	myclass(int a, int b) :x(a), y(b)
	{
		std::cout << "构造哦" << std::endl;
	}
	myclass()
	{

	}
	~myclass()
	{
		std::cout << "销毁哦" << std::endl;//
	}

public:
    void printxy();
protected:
private:
};

void myclass::printxy()
{
	std::cout << x << "  " << y << std::endl;
}

myclass class1(10, 11);//全局变量优先main函数
myclass class2(11, 12);

void change1(myclass **pp)
{
	*pp = &class2;
}

void change2(myclass * &p)
{
	p = &class1;
}

void  main22()
{
	myclass *p=&class1;
	p->printxy();
	change1(&p);
	p->printxy();
	change2(p);
	p->printxy();

	std::cin.get();
}


void main11()
{
	//myclass *p = new myclass;
	myclass *p(new myclass(10,9));//构造函数初始化
	p->printxy();
	myclass  class1(20,1);
	myclass ** pp = &p;//二级指针存储一级指针的地址
	(*pp)->printxy();//一级类指针
	(**pp).printxy();//0级类指针
	
    std::cin.get();
}

void main122()
{
	//myclass *p = (myclass *)malloc(sizeof(myclass));
	//free(p);
	myclass *p = new myclass;
	delete p;

	std::cin.get();
}
#include <iostream>
#include<stdlib.h>

class myclassA
{
public:
	myclassA()
	{
		std::cout << "create\n";
	}
	~myclassA()
	{
		std::cout << "delete\n";
	}
protected:
private:
};

void main()
{
	//new delete自动调用构造析构
	myclassA *p = new myclassA;
	delete p;

	//只会分配内存,释放内存,不会对内存进行操作
	myclassA *p1 = (myclassA *)malloc(sizeof(myclassA));
    free(p1);

	std::cin.get();
}

类重载运算符

#include <iostream>
using namespace std;

class  mycomplex
{
public:
	//友元,需要操作类的内部
	//ostream 引用标准输入输出流,
	friend ostream & operator <<(ostream & out, mycomplex & Complex);
	friend istream & operator >>(istream & in, mycomplex & Complex);
	friend   mycomplex  operator +(mycomplex adddata1, mycomplex adddata2);
	friend   mycomplex  operator +(mycomplex adddata1, int x);
//友元函数可以处理不同的类型交错
	//成员函数能实现,友元函数都可以实现
	//友元函数
	int x;
	int y;//x,y坐标
	//没有构造无法使用this,初始化
	mycomplex()
	{
		this->x = 0;
		this->y = 0;
	}
	mycomplex(int x, int y) :x(x),y(y)
	{
		//this->x += 1;
		//this->y += 1;
	}
	~mycomplex()
	{

	}
	void show()
	{
		std::cout << x << "+" << y << "i" << std::endl;
	}
	void operator ++()
	{
		this->x++;
		this->y++;

	}
	void  operator --();
    
	int operator ()(int num)//重载函数调用运算符,变量名可以当作一个函数调用参数,返回结果
	{

		cout << num << endl;
		return num + num;
	}
	/*
	mycomplex  operator +(mycomplex adddata)
	{
		mycomplex temp;
		temp.x = this->x + adddata.x;
		temp.y = this->y + adddata.y;
		return temp;
	}
	*/	

protected:

private:

};
void  mycomplex::operator--()
{
	this->x--;
	this->y--;
}
//输入输出,cout,屏幕,fout文件
ostream & operator <<(ostream & out, mycomplex & Complex)
{
	out << Complex.x << "+" << Complex.y << "i" << std::endl;
	return out;
}
istream & operator >>(istream & in, mycomplex & Complex)
{
	cout << "请输入X,Y" << endl;
	in >> Complex.x >> Complex.y;
	return in;
}
mycomplex  operator +(mycomplex adddata1  ,mycomplex adddata2)
{
	mycomplex temp;
	temp.x = adddata1.x + adddata2.x;
	temp.y = adddata1.y + adddata2.y;
	return temp;
}
mycomplex  operator +(mycomplex adddata1, int x)
{
	mycomplex temp;
	temp.x = adddata1.x + x;
	temp.y = adddata1.y + x;
	return temp;
}

//++,--先自增,先自减

//

void main()
{
	mycomplex my1(7, 8), my2(9, 10);
	std::cout << my1 + my2 << std::endl;
	std::cout << my1 + 3 << std::endl;
	std::cin.get();

}

void main2()
{
	mycomplex my1;
	cin >> my1;
	cout << my1;
	//my1++;
	++my1;
	cout << my1;
	my1--;
	cout << my1;
	std::cout<<my1(1)<<std::endl;
	std::cout << my1(2) << std::endl;

	std::cin.get();
	std::cin.get();

}

void main1111()
{
	mycomplex my1(7, 8),my2(9,10);
	//my1.show();
	//my2.show();
	std::cout << my1;
	std::cout << my2;
	int num;
	std::cin >> num;
	//std::cin >> my1;

	std::cin.get();
}

类的重载赋值运算复合赋值运算关系运算元重载

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include<QLabel>

namespace Ui {
class Dialog;
}

class mylabel
{
public:

     QLabel *ql;
     int cx;
     int cy;
     mylabel()
     {
         ql=new QLabel("12345") ;
         cx=cy=300;
         ql->move(cx,cy);//移动位置
     }
     ~mylabel()
     {
        delete ql;
     }
    void  show()
    {
       ql->show();
    }

};

class Dialog : public QDialog
{
    Q_OBJECT

public:
    int x;//长 ,宽
    int y;
    int cx;//位置,x,y
    int cy;
public:

    explicit Dialog(QWidget *parent = 0);
    friend void  operator +=(Dialog & d  , mylabel &my  );

    ~Dialog();
    void operator ++()
    {
        this->cx++;
        this->cy++;
        this->move(cx,cy);
    }
   void setxy()
   {
       this->resize(x,y);
   }
   void settoxy(int a,int b)
   {
       this->x=a;
       this->y=b;
   }

   //二元运算符
   Dialog * operator +(Dialog const &adddata)
   {
         Dialog *p=new Dialog;
         p->x=this->x+adddata.x;
         p->y=this->y+adddata.y;
         return p;
   }
   Dialog *operator =(Dialog const &setdata)
   {
       this->x=setdata.x;
       this->y=setdata.y;
       this->setxy();
       return this;
   }
   //int a,int b, a+=b
   //Dialog a,int b,a+=b
   Dialog *operator +=(int length)
   {
       this->x+=length;
       this->y+=length;
       this->setxy();
       return this;
   }
   bool operator <(Dialog const &data)
   {
       return (this->x*this->y) < (data.x*data.y);
   }

private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    x=y=300;
    this->resize(x,y);
    cx=cy=0;
    this->move(cx,cy);
}

Dialog::~Dialog()
{
    delete ui;
}

void  operator +=(Dialog & d  , mylabel &my  )
{
      d.cx+=my.cx;
      d.cy+=my.cy;
      d.move(d.cx,d.cy);
}
#include "dialog.h"
#include <QApplication>
#include<Windows.h>
#include<QDebug>

void   add(Dialog &add1,Dialog &add2)
{
    Dialog temp;//栈上,用完了马上回收
    temp.x=add1.x+add2.x;
    temp.y=add1.x+add2.y ;
    temp.show();
    temp.setxy();
     Sleep(2000);
   //return 副本机制  return temp;

}
Dialog *  addX(Dialog &add1,Dialog &add2)
{
    Dialog *p=new Dialog;//堆上,只有自己回收才行
    p->x=add1.x+add2.x;
    p->y=add1.x+add2.y ;
    p->show();
    p->setxy();
    return p;
}

// >
// =
//+=
//友元重载+=,= 不同类的对象的位置
template<class T>
void showall(T* myt)
{
    myt->show();
}

//友元函数访问私有变量
//如果重载的时候,不用到私有变量,不需要友元
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);//框架支持

   // QLabel *ql=new QLabel("12345");
   // ql->show();
   // showall(ql);
     mylabel  label1;
     showall(&label1);


    Dialog w1;
    //w1.show();
    showall(&w1);
    w1+=label1;

    return a.exec();
}

int mainC(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w1;
    w1.show();
    w1.settoxy(400,700);
    w1.setxy();
    Dialog w2;
    w2.show();
    w2.settoxy(700,400);
    w2.setxy();
    w1=w2;
    w1+=230;
    qDebug()<<(w1<w2);
    qDebug()<<(w2<w1);

    return a.exec();
}

int mainB(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w1;
    w1.show();
    Dialog w2;
    w2.show();
   // addX(w1,w2);
    Dialog *p=w1+w2;
    p->setxy();
    p->show();

    return a.exec();
}

int mainA(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    w.move(0,0);
    for(int i=0;i<800;i++)
    {
        w++;
    }

    return a.exec();
}

自增在前在后差别

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include<QString>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    void setxy(int a,int b)
    {
        this->x=a;
        this->y=b;
    }
    Dialog  *operator ++()
    {
        this->x += 1;
        this->y += 1;
        return this;
    }
    Dialog  *operator ++(int data)
    {
       Dialog *ptemp=new Dialog;
       ptemp->x=this->x;
       ptemp->y=this->y;

       this->x+=1;
       this->y+=1;

       return ptemp;
    }

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void on_pushButton_3_clicked();

private:
    Ui::Dialog *ui;
    int x;
    int y;
};

#endif // DIALOG_H
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->x=10;
    this->y=100;

    QString str1,str2;
    str1.setNum(this->x);
    str2.setNum(this->y);
    ui->lineEdit->setText(str1);
    ui->lineEdit_2->setText(str2);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButton_clicked()
{
    QString str1,str2;
    str1.setNum(this->x);
    str2.setNum(this->y);
    ui->lineEdit->setText(str1);
    ui->lineEdit_2->setText(str2);
}

void Dialog::on_pushButton_2_clicked()
{
      ++(*this);
}

void Dialog::on_pushButton_3_clicked()
{
    Dialog *p=  (*this)++;
    p->show();
}
#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    //w.setxy(10,20);
    //++w;
   // Dialog *p=w++;
   // p->show();

    return a.exec();
}

赋值重载深浅拷贝

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->resize(100,100);
}

MainWindow::~MainWindow()
{
    delete ui;
}
#include "mainwindow.h"
#include <QApplication>
#include<QDebug>


class morewindows
{
public:
    MainWindow *p[5];//指针数组,每一个元素是指针
    morewindows()
    {
        for(int i=0;i<5;i++)
        {
            p[i]=new MainWindow;
            p[i]->show();
            p[i]->move(i*100,i*100);
        }
    }
    ~morewindows()
    {
        for(int i=0;i<5;i++)
        {
           delete p[i];

        }
    }
};

class morewindowss
{
public:
    MainWindow *p[5][4];

    morewindowss()
    {
        for(int i=0;i<5;i++ )
        {
            for(int j=0;j<4;j++)
            {
                p[i][j]=new MainWindow;
                p[i][j]->show();
                p[i][j]->move(i*130,j*130);
            }
        }
    }
    ~morewindowss()
    {
        for(int i=0;i<5;i++ )
        {
            for(int j=0;j<4;j++)
            {
              delete p[i][j];
            }

        }
    }
};
//int a[5]   int *p=a;   int *p= new int[5];
//int *a[5]   int **p=a  int **p=new  (int *)[5];
//int  *a[3][5]
//int * (*p)[5]

class morewindowsss
{
public:
    MainWindow  **p;//二级指针
    morewindowsss()
    {
       // p= (MainWindow **)malloc(sizeof(MainWindow *)*5);
    }
    void init(int num)
    {
        p= new MainWindow * [5];//new的时候类型不需要加上括号
        for(int i=0;i<5;i++)
        {
            p[i]=new MainWindow;
            p[i]->show();
            p[i]->move(num*100,i*100);
        }
    }

    void move(int x,int y)
    {
        for(int i=0;i<5;i++)
        {
            p[i]->move(x*100,y*100);
        }
    }
    morewindowsss  & operator = (morewindowsss const & more)//自己写的深拷贝
    {
        qDebug()<<"shen";
        this->p = new MainWindow * [5];

        for(int i=0;i<5;i++)
        {
            p[i]=new MainWindow;
            p[i]->show();
            p[i]->move(500,i*100);

        }
        return *this;
    }


    ~morewindowsss()
    {
        for(int i=0;i<5;i++)
        {
           delete p[i];
        }
        delete [] p;
       // free(p);
    }

};

class morewindowssss
{
public:
   // MainWindow *p[5][4];//二维数组,每一个元素都是指针
    MainWindow  **pA;//二级指针
    MainWindow *(*p)[4];//指向二维指针数组的指针
    morewindowssss()
    {
        pA=new  MainWindow  *[20];//一维数组
        p=(MainWindow *(*)[4])  pA;

        for(int i=0;i<5;i++ )
        {
            for(int j=0;j<4;j++)
            {
                p[i][j]=new MainWindow;
                p[i][j]->show();
                p[i][j]->move(i*130,j*130);
            }

        }
    }
    ~morewindowssss()
    {
        for(int i=0;i<5;i++ )
        {
            for(int j=0;j<4;j++)
            {
              delete p[i][j];
            }
        }

        delete [] pA;
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    morewindowsss   more1;
    more1.init(1);
   // more1.move(1,2);

   morewindowsss   more2;//拷贝构造

    more2=more1;//赋值

   // morewindowsss   more2(more1);
    more2.move(3,4);

    return a.exec();
}

重载下标

#include "mainwindow.h"
#include <QApplication>

class morewindow
{
public:
     MainWindow *p[5];
     morewindow()
     {
         for(int i=0;i<5;i++)
         {
             p[i]=new MainWindow;
             p[i]->show();
             p[i]->move(i*100,i*100);

         }
     }
     ~morewindow()
     {
         for(int i=0;i<5;i++)
         {
            delete p[i];
         }
     }
     MainWindow * operator [](int i)
     {
         return p[i];
     }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

     morewindow   more1;
     more1[3]->hide();//重载下标

    return a.exec();
}



















上一篇:Rsync同步日志服务的搭建


下一篇:easyui学习笔记12—tab标签页的添加和删除