6.2 Arrays as arguments to functions
An array element is an argument, as with a single variable.
Array name parameters, shape, real parameters should be the array names (essentially addresses), the type is the same, the first address of the array is transmitted.
Changes to the shape parameter group directly affect the real parameter group.
//6-2 using the array name as a function parameter//The main function Initializes a two-dimensional array that represents a matrix, a matrix, and outputs each element//The sub-function is then called to calculate the sum of the elements of each row, respectively, and directly in the first element of each row, returning the sum of the rows elements after the main function is returned. #include <iostream>using namespacestd;voidRowsum (inta[][4],intnrow) { for(inti =0; i < Nrow; i++){ for(intj =1; J <4; J + +) a[i][0] +=A[i][j]; }}intMain () {inttable[3][4] = {{1,2,3,4},{2,3,4,5},{3,4,5,6}}; for(inti =0; I <3; i++){ for(intj =0; J <4; J + +)//Output array elementscout << Table[i][j] <<" "; cout<<Endl; } rowsum (table,3); for(inti =0; I <3; i++) cout<<"Sum of Row"<< I <<" is"<< table[i][0] <<Endl; return 0;}
6.3 Object Array
Object Array Initialization:
When each element object in the array is created, the system invokes the class constructor to initialize the object.
To assign a value through an initialization list:
Example: Point A[2]={point (3,4)};
If no explicit initial values are specified for an array element, the element is initialized with a default value (called the default constructor).
When the initial value of each element object is required to be different values, you need to declare a constructor with a formal parameter.
//6-3 Object Array application examples//Point.h#ifndef _point_h#define_point_hclassPoint {//definition of Class Public://external InterfacePoint (); Point (intXinty); ~Point (); voidMoveintNEWX,intnewy); intGetX ()Const{returnx;} intGetY ()Const{returny;} Static voidShowcount ();//static function membersPrivate://Private data members intx, y;};#endif //_point_h//Point.cpp#include <iostream>#include"Point.h"using namespacestd; Point::P oint (): X (0), Y (0) {cout<<"Default Constructor called."<<Endl;} Point::P oint (intXinty): x (x), Y (y) {cout<<"Constructor called."<<Endl;} Point::~Point () {cout<<"destructor called."<<Endl;}voidPoint::move (intNEWX,intnewy) {cout<<"moving the point to ("<< newx <<", "<< Newy <<")"<<Endl; X=newx; Y=Newy;}//main.cpp#include"Point.h"#include<iostream>using namespacestd;intMain () {cout<<"Entering main ..."<<Endl; Point a[2]; for(inti =0; I <2; i++) A[i].move (i+Ten, i + -); cout<<"Exiting main ..."<<Endl; return 0;}
6.4 Range-based for loop
//Old MethodintMain () {intarray[3] = {1,2,3}; int*p; for(p = array; p < array +sizeof(array)/sizeof(int); ++P) {//Notice the difference between +p and +*p.*p + =2; Std::cout<< *p <<Std::endl; } return 0;}//Range-based for loopintMain () {intarray[3] = {1,2,3}; for(int&E:array) {e+=2; Std::cout<< e <<Std::endl; } return 0;}
Part6 array, pointer, and string 6.2 array as a function of parameter 6.3 Object array 6.4 range-based for loop