文档库 最新最全的文档下载
当前位置:文档库 › Chapter7_Array_II

Chapter7_Array_II

C++Programming

Chapter 7 Array

Part 2

Learning line of this course ●

Learning line ● Basic syntax of C++ language

● ● Chapter 2

More about variable and function ● Basic conception of Class and Object ● Chapter 3

● More syntax about C++ language

● ● ● ● ● Basic data type and expression

Chapter 4-5: control statements Chapter 6: function

Chapter 7: array

Chapter 8: pointer

● More conception of Class and Object ● Chapter 9-13

Lecture10 ● Part 2: Using Array

● ●

● ● 7 7.5 5 Passing Arrays to Functions

7.6 Case Study: Class GradeBook Using an Array to Store Grades

7.7 Searching Arrays with Linear Search

7.8 Sorting Arrays with Insertion Sort

● Part 3: Multidimensional Arrays

● ● 7 7.9 9 Multidimensional Arrays

7.10 Case Study: Class GradeBook Using a Two-Dimensional Array

● Part 4: Vector

● 7.11 Introduction to C++ Standard Library Class Template vector

7.5 Passing Arrays to Functions

● What to pass? What is the actual argument ?

● If you want to pass an array element 传递数据元素

● Individual array elements are passed by value exactly as

simple variables are

● If you want to pass the entire array 传递整个数组

● To pass an array argument to a function, specify the name of

the array without any brackets 语法是int array_name[]

int a[10]={0};

……

? How can the function void modify_array(int a[])

know it ?

{

for(int i=0;i<10;i++)

{a[i]+=1;}

}

7.5 Passing Arrays to Functions

● Pass the entire array to function

● Case 1 全局变量const int arraySize=10;

void modify_array(int[]);

int main(void)

{

int a[arraySize]={0};

modify_array(a);

}

void modify_array(int x[])

{

for(int i=0;i

}

7.5 Passing Arrays to Functions

● Pass the entire array to function

● Case 2 函数形参void modify_array(int[],int);

int main(void)

{

int a[10]={0};

modify_array(a,10);

}

void modify_array(int x[],int arraySize) {

for(int i=0;i

}

The function can get the array size by formal argument.

7.5 Passing Arrays to Functions ● When passing the entire array to function, it is in manner of passing by reference 将整个数组传递 给函数的方案,本质上是引用的传递 ● ● ● C++ passes arrays to functions by reference. The value of the name of the array is the address in the computer’s memory of the first element of the array. 数组名实际上代表了数组的地址,即数组的引用 the called functions can modify the element values in the callers’ original arrays. 被调用函数可以修改原始数 组的值

如果禁止被调函数修改数组元素的值,可以在形参上加

上const 关键字

● void copyArray(const int []);

Lecture10 ● Part 2: Using Array

● ●

● ● 7 7.5 5 Passing Arrays to Functions

7.6 Case Study: Class GradeBook Using an Array to Store Grades

7.7 Searching Arrays with Linear Search

7.8 Sorting Arrays with Insertion Sort

● Part 3: Multidimensional Arrays

● ● 7 7.9 9 Multidimensional Arrays

7.10 Case Study: Class GradeBook Using a Two-Dimensional Array

● Part 4: Vector

● 7.11 Introduction to C++ Standard Library Class Template vector

7.6Case Study:Class GradeBook Using an

Array y to Store Grades

Analyze the requirement

User can set the course name

Get the course name

Open the gradebook

Display the course name

Private data:

NEW

Course name

Input the grades with N students

Students’grades Output the sum, barchart,

average, max, min ……

Using an Array to Store Grades ● ● Design the class

Requirement of new variable to store the grades of 10 student ● int grades[10]

Design the new functions

● Get the grades in constructor

● Get the statistics

● int getMinimum()

● int getMaximum()

● double getAverage()

● Display and output the information

● void processGrades()

● void outputBarchart()

● void outputGrades()

header file

数组的大小被声明为一个公共的静态

常量数据成员

类的对象被创建后,所有的对象

将共享同一个类的静态数据成员

constructor,getAverage

数组名形参被声明为常量,编译

器将不允许在函数内部对数组元

素进行修改,这里仅进行了复制

操作

getMinimum,getMaximum

寻找最小值

寻找最大值

outputBarChart

静态数据成员student在使用上

与其他数据成员基本一致,除了

值不允许被修改以外

main

公共静态数据成员可以被类以外的代码访

问,需要加上作用域解析符(::)

results:

关于GradeBook::student

● public的作用域

● 表明该数据成员是可以直接访问的,无需通过getxxx()函数● static的存储类别

● ● ● ● 表示该数据成员是静态的

静态的数据成员属于该类,在程序运行时自动创建

即使还没有创建该类的对象,该静态数据成员也已经存在了静态数据成员可以被该类的所有对象访问和修改

● const的访问限定

● ● 表示该数据成员是常量,即一旦创建就不允许修改其数值在创建时也可用表达式,例如 const int a = 5 * 3;

● 关于student

● ● GradeBook类的公共的静态的常量数据成员

外界可以直接访问,但需要指定名称域GradeBook::student

Lecture10 ● Part 2: Using Array

● ●

● ● 7 7.5 5 Passing Arrays to Functions

7.6 Case Study: Class GradeBook Using an Array to Store Grades

7.7 Searching Arrays with Linear Search

7.8 Sorting Arrays with Insertion Sort

● Part 3: Multidimensional Arrays

● ● 7 7.9 9 Multidimensional Arrays

7.10 Case Study: Class GradeBook Using a Two-Dimensional Array

● Part 4: Vector

● 7.11 Introduction to C++ Standard Library Class Template vector

相关文档