Lecture 9: Compiling Preprocessing Commands

Compile preprocessing commands

1.#include contains instructions
 Embedding a source file at that point in the current source file.
# include < filename >  
Search in a standard way. Files are located in the include subdirectory of the C++ system directory
 # include "filename"
First, search in the current directory, and if not, search in a standard way.
2.#define macro definition instruction
 Defining symbolic constants has in many cases been replaced by const definition statements.
Defining macros with parameters has been replaced by inline functions.
#undef
 Delete macros defined by # define so that they no longer work.
3. Conditional compilation instructions #if and #endif
(1)
# if constant expression
 // Compiling Constant Expressions When Nonzero
     Program text  
#endif
......
Conditional Compilation Instruction #else
    (2) 
  # if constant expression
     // Compiling Constant Expressions When Nonzero
       Procedure body 1
#else
  // Compiling when Constant Expressions are Zero
       Procedure body 2
#endif
 Conditional Compilation Instruction # elif
(3)
# if constant expression 1
    Program text 1//compiled when "Constant Expressions 1" is non-zero
 # elif constant expression 2
    Program text 2//compiled when "Constant Expressions 2" is non-zero
#else
    Program body 3// compiled in other cases
#endif
 Conditional compilation instructions
(4)
# ifdef identifier
    Program segment 1
#else
    Program segment 2
#endif
 If the "identifier" is defined by # defined and not deleted by undef, then compile segment 1, otherwise compile segment 2.
Conditional compilation instructions
(5)
# ifndef identifier
   Program segment 1
#else
   Program segment 2
#endif
 If the identifier is not defined, then compile Segment 1, otherwise compile Segment 2.


Multi-file structure

A source program can be divided into multiple source files:
Class declaration file (. h file)
Class implementation file (. cpp file)
Class usage file (main(). cpp file)
Use engineering to compose documents.

Header files without conditional compilation
//main.cpp
#include "file1.h"
#include "file2.h"
int main()
{
    ...
}

//file1.h
#include "head.h"
    ...
//file2.h
#include "head.h"
    ...

//head.h
    ...
class Point
{
    ...
}
    ...

Header files compiled using conditions
//head.h
#ifndef  HEAD_H
  #define  HEAD_H
    ...
  class Point
  {
      ...
  }
      ...
#endif

III. Object Array
1. The Concept of Object Array
Arrays are collections of similar data.
Arrays can be composed not only of simple variables, but also of user-defined class objects, that is, each array element is the same kind of object.
For example, there are 30 students in a class, and each student's information includes his or her student number, name and gender. First, the student class is established, and then an object is created for each student, which needs 30 object names. A better approach is to define an array of objects of a "student class", each element of which is an object of a "student class".
For example: Stud St [30];
2. Constructor of object array
When building an array of objects, the constructor is also called.
(2) If the constructor has only one parameter, it can provide arguments in brackets after the equals sign when defining an array.
For example: Stud St [3]={60, 70, 80};
The number of arguments cannot exceed the number of array elements, because the compiler system passes only one argument to the constructor of each object element.
For example: Stud St [3]={60, 70, 80, 90}; // Illegal
(3) If the constructor has more than one parameter, it cannot be used to provide all the arguments directly when defining an array. For example:
Stud:: Stud (int = 100, int = 18, int = 60) // constructor
Stud st[3]={111,34,31};
// This kind of usage relationship is not clear and ambiguous.
When the constructor has many parameters, the initialization of the array object can be realized by the following methods.
For example: Stud St [3]={
Stud(11,32,74), // / calls the constructor of the first element, providing it with three arguments.
Stud(12,34,27), // / calls the constructor of the second element, providing it with three arguments.
Stud(14,45,63)// calls the constructor of the third element to provide it with three arguments.
};

#include <iostream> // / / case
#include <string>
using namespace std;
class stud  //Definition class
{private:
    int num;    string name;
    char sex;
 public:
    stud( int n, string nam, char s )
    {num = n;  name=nam;
      sex = s;
      cout <<"Constructor called."
              <<endl;
    }
   void display ( );
};


void stud::display ( )
{cout<<"num:"<<num<<endl;
 cout<<"name:"<<name<<endl;
 cout<<"sex:";
 if (sex==0) { cout<<"male"<<endl; }
else { cout<<"female"<<endl;}
}
int main( )
{stud st[3] = {
   stud(1001,"Zhang San",1),
   stud(1002,"Li Si",0),
   stud(1003,"Wang Wu",0)
 };    //  Initialize arrays with constructors with specified parameters
 cout <<"First student:" ; st[0].display( );
 cout <<"The second student:" ; st[1].display( );
 cout <<"The third student:" ; st[2].display( );
return 0;}

Example: Application of Object Array (One Dimension), Constructor and Destructor

#include <iostream> //  Case (Supplement)
#include <string>
using namespace std;
class stud  //Definition class
{private:
    int num;    string name;
    char sex;
 public:
    stud( int n, string nam, char s )
    {num = n;  name=nam;
      sex = s;
      cout <<num<<"  Constructor called."
              <<endl;    }
  ~stud()  {cout<<num<<"  pass"<<endl;}
   void display (   );
};
void stud::display ( )
{cout<<"num:"<<num<<endl;
 cout<<"name:"<<name<<endl;
 cout<<"sex:";
 if (sex==0) { cout<<"male"<<endl; }
else { cout<<"female"<<endl;}
}
int  main( )
{stud st[3] = {
   stud(1001,"Zhang San",1),
   stud(1002,"Li Si",0),
   stud(1003,"Wang Wu",0)
 };    //  Initialize arrays with constructors with specified parameters
 stud ss(888,"aaa",1);
 ss.display();
 cout <<"First student:" ; st[0].display( );
 cout <<"The second student:" ; st[1].display( );
 cout <<"The third student:" ; st[2].display( );
return  0;
}

Example: Application of Object Array (2-D), Constructor and Destructor

#include <iostream> //  Case (Supplement)
#include <string>
using namespace std;
class stud  //Definition class
{private:
    int num;    string name;
    char sex;
 public:
    stud( int n, string nam, char s )
    {num = n;  name=nam;
      sex = s;
      cout <<num<<"  Constructor called."
              <<endl;    }
  ~stud()  {cout<<num<<"  pass"<<endl;}
   void display (   );
};
void stud::display ( )
{cout<<"num:"<<num<<endl;
 cout<<"name:"<<name<<endl;
 cout<<"sex:";
 if (sex==0) { cout<<"male"<<endl; }
else { cout<<"female"<<endl;}
}
int  main( )
{stud st[3][2] = {
    {stud(1001,"Zhang San",1),
        stud(1002,"Li Si",0)},
    {stud(1003,"Wang Wu",0),
    stud(1111,"Three",1)},
    {stud(2222,"Four",0),
    stud(3333,"Five",0)}
 };    //  Initialize arrays with constructors with specified parameters
 stud ss(888,"aaa",1);
 ss.display();
 cout <<"First student:" ; st[0][0].display( );
 cout <<"The second student:" ; st[1][0].display( );
 cout <<"The third student:" ; st[2][0].display( );
return  0;}



3.4 Object Array
Example: 3.6-1. Application of CPP object array and constructor 2

#include <iostream>// Example 3.6-1
using namespace std;
class Box   // Class definition
 {public:
   Box(int h=10,int w=12,int len=15)
   { height=h; width=w; length=len; }
   int volume();
  private:
     int height, width, length;
    }; 
 int Box::volume()
  { return (height*width*length);  }
int main()
 {  Box a[3]={
       Box(10,12,15),
       Box(15,18,20),
       Box(16,20,26) };
//  Initialize arrays with constructors with specified parameters
  cout<<"volume of a[0] is "<<a[0].volume()<<endl;
  cout<<"volume of a[0] is "<<a[1].volume()<<endl;
  cout<<"volume of a[0] is "<<a[2].volume()<<endl;
  return 0;
 }



4 Object Pointer

1. Pointer to object
When an object of a class is created, the system allocates a certain amount of storage space for each object to store members. The starting address of the object space is the pointer to the object. A pointer can be defined to store the pointer of an object.
Define object pointer format: class name * object pointer name;
For example: time * pt, t;
Pt=& t1; // pointer to object
Method of accessing objects and object members by using object pointers:
(1) the object of *pt//pt
(2) (* pt). Hour or: Pt - > hour // Pt refers to the data member of the object
(3) (* pt).put () or: Pt - > put ()//pt refers to the member function of the object

#include <iostream.h>
class Time  //Class definition
{public:
   int hour, minute, sec;
   void put ( )
   { hour = 12;
      minute = 0;
      sec = 0;
   }
};

void main ( )
{Time *pt, t1;
 pt = &t1;    // Pointer to object
 pt->put( );
 cout<<pt->hour<<":"<<pt->minute
         <<":"<<pt->sec<<endl;
 cout<<(*pt).hour<<":"<<(*pt).minute
         <<":"<<(*pt).sec<<endl;
}


2. Pointer to Object Data Members
The method of defining a pointer variable to an object data member is the same as that of defining a pointer variable to an ordinary variable.
Definition format: data type name * pointer variable name;
For example: int * pl; // Defines pointer variables to integer data
Pl=&t1.hour; // hour must be a public data member
// Assign the hour address of the data member T1 to the pointer pl to point to t1.hour
cout << *pl <

#Include <iostream.h>// Example
class Time  // Class definition
{public:
   int hour, minute, sec;
   Time ( int h, int m, int s )
   { hour = h;
     minute = m;
     sec = s;
   }
   void get_Time( )
   { cout << hour<<":"
              << minute<<":"
              << sec<<endl;
   }
};
int main ( )
{Time t1( 10,13,56 );  
 int *p1 = &t1.hour; 
// Define a pointer to the shaping data, pointing to t1.hour
 cout << *p1 <<endl;
 t1.get_Time( );   // Calling member functions of t1
 Time  *p2 = &t1;
 // Define the pointer variable p2 to the Time class object and point to t1
 p2->get_Time( ); //Call the member function of the object specified in p2
 void (Time::*p3)( );
 // Define the pointer variable p3 pointing to the Time class common member function
 p3 = &Time::get_Time; 
 // Make p3 point to the Time class common member function get_Time()
 (t1.*p3) ( ); //Call the member function t1.get_Time() referred to in p3
 return 0;
}




```c++

3,this Pointer
        //Data members in each object occupy storage space separately. If n objects are defined for the same class, n groups of space of the same size store data members of n objects. However, many similar objects share member function codes in memory to save memory space. Then, how can we find our own data members when member functions of different objects refer to data members?
        C++Each member function contains a special pointer whose name is fixed and called“this". It is a pointer to the object whose value is the starting address of the object where the member function is currently called.
        //For example, when a member function calls data member a.volume, the compilation system assigns the starting address of object a to it. this Pointer, so when a member function refers to a data member, it follows this Direction Finding Object a Data members.




<div class="se-preview-section-delimiter"></div>
For example, the following return statements for operations involving data members:
    return length*width*height ;
In fact, C++ is treated as:
    return ( this ->length)*(this->width)*(this->height);
It can also be written in the following form:
    return ((*this).length*(*this).width*(*this).height);
But it can't be written as follows:
    Return (* this. length) * (* this. width) * (* this. height); // error
 Because the priority of the "." operator is higher than that of the pointer operator "*", (* this.length) is equivalent to * (this.length), and this.length is illegal, compilation error (should be this - > length), * this represents the object that this refers to.

include

Posted by robinstott on Sun, 19 May 2019 03:00:10 -0700