const object, NULL, and array of objects created in nullptr,c++

Source: Internet
Author: User



1. classes defined as const

#include <iostream>class area{public:int x;int y;mutable int z;  class member without const constraint area (): X (Ten), Y (+), Z (2) {}void printxy () const  //cannot access local variables in class {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 object cannot refer to non-const member function//cannot change internal variable, mutable exception const area * Const P = New Area;p->go ();//p->add ();//p = new area;//pointer can change const area area1;area1.printxy ();//area1.add (1); Area1.go () //area1.x = 10;//This sentence indicates that when the class is defined as const, the value defined as mutable z can be modified area1.z + = 4;area1.printxy (); Std::cin.get ();}

Operation Result:

2.NULL and the nullptr

#include <iostream>void go (int num) {std::cout << "gonum" << Std::endl;} void Go (void *p) {std::cout << "GOP" << Std::endl;} The type of NULL in C + + is int, 0void main () {//c++ is strongly typed, strict type check void *p = Nullptr;//c++ Nullgo (p);   The result is a GOP  //Processing Go (NULL) based on the type;  The Go (int num)  result is called Gonumgo (nullptr);  The result is gopstd::cin.get ();}

3.QT in Button and the Line_edit operation-related

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 ();//Get Text QString  str2=ui->lineedit_2->text (); int  db1= Str1.toint (); int  db2=str2.toint ();//convert int  db3=db1+db2; QString Str3;str3.setnum (DB3);//functions can be overloaded, many types of ui->lineedit_4->settext (STR3);// The setting text converts qstring into a string: Str3.tostdstring (). C_str;

4.new Delete and the malloc , Free related

#include <iostream> #include <stdlib.h>class myclassa{public:myclassa () {std::cout << "create\n";} ~myclassa () {std::cout << "delete\n";} Protected:private:};void Main () {//new Delete automatically calls the construct destructor Myclassa *p = new Myclassa;delete p;//only allocates memory, frees memory, Does not operate on memory Myclassa *P1 = (Myclassa *) malloc (sizeof (Myclassa)); free (p1); Std::cin.get ();}

Case 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 << "construct Oh" << Std::endl;} MyClass () {}~myclass () {std::cout << "destroy Oh" << std::endl;//}public:void printxy ();p rotected:private:};void MyClass::p Rintxy () {std::cout << x << "" << y << Std::endl;} MyClass Class1 (10, 11);//global variable precedence main function MyClass Class2 (one, one); 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));//constructor initialization p->printxy (); MyClass Class1 (1); m Yclass * * pp = &p;//level two pointer stores the address of the first pointer (*PP)-&GT;PRINTXY ();//Class-A pointer (**pp). Printxy ();//0 Class pointer Std::cin.get ();} void main122 () {//myclass *p = (MyClass *) malloc (sizeof (MyClass));//free (P); MyClass *p = new MyClass;d elete P;std::cin.get ();} 
5. Creating an array of objects

#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 ();//heavy-duty subscript    return A.exec ();}

6. Creating an array of objects

#include "mainwindow.h" #include <QApplication> #include <qdebug>class morewindows{public:mainwindow *p[5            ];//pointer array, each element is a pointer 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;//Level two pointer morewindowsss () {//p= (MainWindow *) ma    Lloc (sizeof (MainWindow *));            } void init (int num) {p= new MainWindow * [5];//new type does not need to be bracketed 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)//deep copy written by yourself {Qdebug () << "she        n ";        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];//Two-dimensional array, each element is a pointer MainWindow **pa;//level two pointer MainWindow * (*P) [4];//Pointer to a two-dimensional pointer array        Morewindowssss () {pa=new MainWindow *[20];//one-dimensional array 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);    Morewindowsss more2;//Copy Construction more2=more1;//Assignment//morewindowsss more2 (More1);    More2.move (3,4); return a.exec ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.