const对象,NULL和nullptr,C++中创建对象数组
生活随笔
收集整理的這篇文章主要介紹了
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 //不可以訪問類中局部變量{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.NULL和nullptr
#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++的nullgo(p); //結果為gop //根據類型來處理go(NULL); //會調用go(int num) 結果為gonumgo(nullptr); //結果為gopstd::cin.get(); }
3.QT中button和Line_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 delete和malloc,free相關
#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(); }?
總結
以上是生活随笔為你收集整理的const对象,NULL和nullptr,C++中创建对象数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 摆地摊卖奶茶怎么起步?需要准备什么?
- 下一篇: 操作符(++,+,+=,小于号,(),-