Create an object array in const object, NULL and nullptr, and C ++

Source: Internet
Author: User

Create an object array in const object, NULL and nullptr, and C ++
Zookeeper

1. defined as a class after const

# Include
 
  
Class area {public: int x; int y; mutable int z; // class member area (): x (10), y (10) not restricted by const ), z (2) {} void printxy () const // The local variable {z = z + 1; std :: cout <x <"" <y <"" <z <"\ n";} void add (int a) {x + =; y-= a;} void go () const {} protected: private :}; void main () {// The const object cannot reference non-const member functions. // internal variables cannot be changed. mutable exception const area * const p = new area; p-> go (); // p-> add (); // p = new area; // the pointer can change const area area1; area1.printxy (); // area1.add (1); area1.go (); // area1.x = 10; // This statement indicates that after the class is defined as const, the mutable value z can be changed to area1.z + = 4; area1.printxy (); std :: cin. get ();}
 

Running result:

2. NULL and nullptr

# Include
 
  
Void go (int num) {std: cout <"gonum" <std: endl;} void go (void * p) {std :: cout <"gop" <std: endl ;}// the NULL type in C ++ is int, and 0 void main () {// C ++ is strongly typed, strict type check void * p = nullptr; // nullgo (p) of C ++; // The result is gop // process go (NULL) based on the type ); // go (int num) is called and the result is gonumgo (nullptr); // The result is gopstd: cin. get ();}
 

3. Related to the button and Line_edit operations in QT

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 (); // obtain the 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 reloaded. Many types of ui-> lineEdit_4-> setText (str3 ); // set the text to convert QString to str3.toStdString (). c_str;

4. related to new delete, malloc, and free

# Include
 
  
# Include
  
   
Class myclassA {public: myclassA () {std: cout <"create \ n ";}~ MyclassA () {std: cout <"delete \ n" ;}protected: private :}; void main () {// new delete automatic call to construct the Destructor myclassA * p = new myclassA; delete p; // only memory is allocated and the memory is released, myclassA * p1 = (myclassA *) malloc (sizeof (myclassA); free (p1); std: cin. get ();}
  
 

Case 2

 
# Include
 
  
Class myclass {public: int x; int y; public: myclass (int a, int B): x (a), y (B) {std :: cout <"constructor" <std: endl;} myclass (){}~ Myclass () {std: cout <"Destroy" <std: endl; //} public: void printxy (); protected: private:}; void myclass:: printxy () {std: cout <x <"" <y <std: endl;} myclass class1 (10, 11 ); // The global variables take precedence over the main function 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 )); // The constructor initializes p-> printxy (); myclass class1 (20, 1); myclass ** pp = & p; // The second-level pointer stores the address of the first-level pointer (* pp)-> printxy (); // The first-level class pointer (** pp ). printxy (); // Class 0 pointer std: cin. get ();} void main122 () {// myclass * p = (myclass *) malloc (sizeof (myclass); // free (p ); myclass * p = new myclass; delete p; std: cin. get ();}
 
5. Create an object Array

# Include "dialog. h" # include
 
  
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 (); // reload the subscript return a.exe c ();}
 

6. Create an object Array

# Include "mainwindow. h" # include
  
   
# Include
   
    
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; // second-level pointer morewindowsss () {// p = (MainWindow **) malloc (sizeof (MainWindow *) * 5);} void init (int num) {p = new MainWindow * [5]; // when it is new, no parentheses need to be added 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) // write your own deep copy {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]; // two-dimensional array, every element is a pointer MainWindow ** pA; // second-level 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 (1, 2); morewindowsss more2; // copy to construct more2 = more1; // assign a value // morewindowsss more2 (more1 ); more2.move (3, 4); return a.exe c ();}
   
  

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.