书籍版本:2019年9月第一版;王刚 杨巨峰译;电子工业出版社
编译器 : win10 && VS2015
6.1
形参是定义在函数中的虚拟值,实参是形参的初始值。
6.2
a. 返回值是int类型,不能return一个string
b. 没有定义返回值类型
c. 两个参数名不能一样
d. 没有加{}
6.3
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int fact(int val)
{
int sum = 1;
for (int i = 1; i <= val; i++)
{
sum = sum * i;
}
return sum;
}
int main()
{
int s = fact(5);
cout << s << endl;
system("pause");
}
6.4
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int fact(int val)
{
int sum = 1;
for (int i = 1; i <= val; i++)
{
sum = sum * i;
}
return sum;
}
int main()
{
cout << "请输入一个数字:";
int a;
cin >> a;
int s = fact(a);
cout << a << "的阶乘值为:" << s << endl;
system("pause");
}
6.5
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int numfab(int val)
{
return fabs(val);
}
int main()
{
cout << "请输入一个数字:";
int a;
cin >> a;
int s = numfab(a);
cout << a << "的绝对值为:" << s << endl;
system("pause");
}
6.6
局部变量包括 形参和静态局部变量。
形参属于自动对象,在函数开始时为形参申请存储空间,在函数终止时形参被销毁。
局部静态变量,在程序第一次经过对象定义语句时初始化,直到程序终止时才被销毁。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int numfab(int val)//形参
{
static int count = 0;// 局部静态变量
count++;
int num = fabs(val);// 局部变量
return num;
}
int main()
{
cout << "请输入一个数字:";
int a;
cin >> a;
int s = numfab(a);
cout << a << "的绝对值为:" << s << endl;
system("pause");
}
6.7
#include "stdafx.h"
#include <iostream>
using namespace std;
int num()
{
static int count = 0;
if (count<1)
{
return count++;
}
else
return count;
}
int main()
{
for (int i = 0; i < 10; i++)
{
cout << num() << endl;
}
system("pause");
}
6.8
//这是Chapter6.h文件的内容
#pragma once
class CChapter6
{
public:
CChapter6();
~CChapter6();
private:
int f();
int f2(int i);
int calc(int v1, int v2);
double square(double x);
};
// 这是Chapter6.cpp文件中的内容
#include "stdafx.h"
#include "Chapter6.h"
#include <iostream>
using namespace std;
CChapter6::CChapter6()
{
}
CChapter6::~CChapter6()
{
}
int CChapter6::f()
{
string s;
return 0;
}
int CChapter6::f2(int i)
{
}
int CChapter6::calc(int v1, int v2)
{
}
double CChapter6::square(double x)
{
}
6.9
如上题
6.10
#include "stdafx.h"
#include <iostream>
using namespace std;
void ExChangeNum(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int main()
{
int a = 111, b = 999;
cout << "交换之前: a: " << a << ", b: " << b << endl;
ExChangeNum(&a, &b);
cout << "交换之后: a: " << a << ", b: " << b << endl;
system("pause");
}
6.11
#include "stdafx.h"
#include <iostream>
using namespace std;
void ExChangeNum(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void Reset(int& a, int& b)
{
int tmp = a;
a = b;
b = tmp;
}
int main()
{
int a = 111, b = 999;
cout << "交换之前: a: " << a << ", b: " << b << endl;
Reset(a, b);
cout << "交换之后: a: " << a << ", b: " << b << endl;
system("pause");
}
6.12
代码同上。个人觉得引用更好用。
6.13
void f(T)是传入一个T类型的变量
void f(&T)是传入一个T类型的引用
6.14
当形参的值不能改就不能传引用;当形参是一个很难拷贝的类型并且可能对其值修改时传引用;
6.15
s是常量引用是表明函数中不会改变其值;
s与occurs是引用是为了取到对应的对象;
s是普通引用可能会造成其值被改变;
occurs是常量引用则无法改变其值,就无法得到想要的结果。
6.16
只能接受非常量string,改参数为const string&
6.17
#include "stdafx.h"
#include <iostream>
using namespace std;
bool IsUpExist(const string& s)
{
for (int i = 0; i < s.size(); i++)
{
if (isupper(s[i]))
{
return true;
}
}
return false;
}
void StringToLower(string& s)
{
for (int i = 0; i < s.size(); i++)
{
if ( (s[i]) )
{
s[i] = tolower(s[i]);
}
}
}
int main()
{
string a = "aaaaaHaaaa";
string b = "AHFBDIALFH";
cout << "修改之前a: " << a << endl;
cout << "修改之前b: " << b << endl;
bool hasUp = IsUpExist(a);
StringToLower(b);
cout << hasUp << endl;
cout << "修改之后b: " << b << endl;
system("pause");
}
6.18
a. bool Compare(matrix& m1, matrix& m2);
b. vectoe<int> change_val(int a, vector<int> iter);
6.19
a不合法,只能传一个参数
b合法,但第二个参数最好不要传常量
c合法
d不合法,最后一个参数不是整形
6.20
传入参数的值不能被改变时传入常量;
本该是常量设成了普通引用,有可能会改变其值与原目的不符,且无法传入常量引用作为参数
6.21