const对象,NULL和nullptr,C++中创建对象数组

简介:  1.定义成了const之后的类 #include <iostream>class area{public: int x; int y; mutable int z; //不受const约束的类成员 area() :x(10), y(10), z(2) { } void printxy()const //不可以访问类中局部变量


1.定义成了const之后的类

#include <iostream>
class area
{
public:
	int x;
	int y;
	mutable int z;  //不受const约束的类成员
	area() :x(10), y(10), z(2)
	{
		
	}
	void printxy()const  //不可以访问类中局部变量
	{
		z = z + 1;
		std::cout << x << "  " << y  << "  " <<  z  << "\n";
	}
	void add(int a)
	{
		x += a;
		y -= a;
	}
	void go()const
	{
	}
protected:
private:
};

void main()
{
	//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;
	//这一句说明当把类定义成const之后,定义成了mutable的值z可以修改
	area1.z += 4;
	area1.printxy();
	std::cin.get();
}

运行结果:

2.NULLnullptr

#include <iostream>

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

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

//C++中的NULL的类型是int ,0
void main()
{
	//C++是强类型,严格的类型检查
	void *p = nullptr;//C++的null
	go(p);   //结果为gop  //根据类型来处理
	go(NULL);  //会调用go(int num)  结果为gonum
	go(nullptr);  //结果为gop
	std::cin.get();
}

3.QTbuttonLine_edit操作相关的

QString  str1=ui->lineEdit->text();
QString  str2=ui->lineEdit_2->text();
QString str3=str1+str2;
ui->lineEdit_3->setText(str3);

QString  str1=ui->lineEdit->text();//获取文本
QString  str2=ui->lineEdit_2->text();
int  db1=str1.toInt();
int  db2=str2.toInt();//转换
int  db3=db1+db2;
QString str3;
str3.setNum(db3);//函数可以重载,很多个类型
ui->lineEdit_4->setText(str3);//设置文本

将QString转换成为字符串的是:str3.toStdString().c_str;

4.new deletemallocfree相关

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

案例2

</pre><pre class="cpp" name="code">#include<iostream>

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();
}
5.创建对象数组

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

class morewindow
{
public:
    Dialog *p[5];
    morewindow()
    {
        for(int i=0;i<5;i++)
         {
             p[i]=new Dialog;
             p[i]->show();
             p[i]->move(i*100,i*100);
         }
    }
    ~morewindow()
    {
        for(int i = 0;i<5;i++)
        {
            delete p[i];
        }
    }
    Dialog * operator [](int i)
    {
        return p[i];
    }
};


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    morewindow more1;
    more1[3]->hide();//重载下标

    return a.exec();
}

6.创建对象数组

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

 
目录
相关文章
|
22天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
31 0
|
2天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
|
2天前
|
存储 编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
|
3天前
|
C++
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
|
3天前
|
存储 编译器 C++
【C++成长记】C++入门 | 类和对象(中) |拷贝构造函数、赋值运算符重载、const成员函数、 取地址及const取地址操作符重载
【C++成长记】C++入门 | 类和对象(中) |拷贝构造函数、赋值运算符重载、const成员函数、 取地址及const取地址操作符重载
|
9天前
|
编译器 C++
|
9天前
|
存储 编译器 C语言
C++类与对象
C++类与对象
2 0
|
22天前
|
编译器 C语言 C++
【c++】类和对象(三)构造函数和析构函数
朋友们大家好,本篇文章我们带来类和对象重要的部分,构造函数和析构函数
|
22天前
|
存储 编译器 C语言
【c++】类和对象(二)this指针
朋友们大家好,本节内容来到类和对象第二篇,本篇文章会带领大家了解this指针
【c++】类和对象(二)this指针
|
22天前
|
存储 编译器 C语言
【c++】类和对象(一)
朋友们,大家好,本篇内容我们来对类和对象进行初步的认识

热门文章

最新文章