cf+补题:4小时
复习c++第8章,并敲了相关代码熟悉:2小时
///c++第八章
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"algorithm"
using namespace std;
///inline表示该函数为内联函数,参数为引用参数
///同时如果不想对函数参数的引用的值改变可将函数写为square1形式
///此为work1引用的有关函数
inline int square(int &x)
{
int &z = x;
return z = z * z;
}
inline int square1(const int &x)
{
/// int &z = x; ///此语句是错误的,同样在其中对x值进行改变也是错误的。
///编译器将禁止其对x的值进行改变。
int z = x;///此可行
return x * z;
}
///此为临时变量&的有关函数
inline void swapr1(int &a,int &b)///交换a,b值
{
int temp = a; a = b; b = a;
return ;
}
inline void swapr2(long long &a,long long &b)
{
long long temp = a; a = b; b = temp; return ;
}
inline int swapr3(const int &a,const int &b)
{
///鼓励使用const原因如下
///1,使用const可以避免无意中修改数据的编程错误。
///2,使用const使函数能够处理const和非const实参。否则将只能接受非const数据。
///3,使用const引用使函数能够正确生成并使用临时变量
return a + b;
}
///此为引用结构体的有关函数及其操作
struct Node{
char name[26];
char quote[26];
int used;
};
const Node & use(Node &node)
{
///此node为别名,为引用
///返回值为引用,此为:
///1,返回机制将返回值复制到临时存储区域中,随后调用程序访问该区域。
///然而,返回引用意味着调用程序将直接访问返回值,而不需要拷贝。
///通常,引用指向传递给函数的引用,因此调用函数实际上是直接访问自己的一个变量。
///因不进行拷贝,故速度会快。
///一定要注意不要返回当函数终止时不再存在的内存单元。
cout << node.name << " says:\n";
cout << node.quote << endl;
cout << endl;
node.used ++;
return node;
}
///函数交换模板
template <class Any>
void Swap(Any &a,Any &b)///也可写为 template <typename Any>
{
Any temp; temp = a; a = b; b = temp;
}
template <typename Any>
void Swap(Any &a,Any &b,Any &c)
{
Any temp; temp = c; c = b; b = a; a = temp;
}
void work1()///此函数为&时的操作
{
printf("这里为引用的相关操作:\n");
int x;
scanf("%d",&x);
int y = square(x);
int &z = x;///引用参数必须进行初始化赋值,同时只能赋值一次,一次引用确定后,就不可再次引用别的变量
///上代码实际为int * const z = &x;的伪装表示
printf("x = %d y = %d z = %d\n",x,y,z);
printf("x = %x y = %x z = %x\n",&x,&y,&z);///x与z的地址相同,y则差一个int单位,印证了他是一个栈
}
void work2()///临时匿名变量(引用)
{
printf("这里为临时你们匿名变量的相关操作:\n");
long long a = 3,b = 4;
///swapr1(a,b);此语句将会报错,因为swapr的函数的参数为int型,无法引用longlong型数据
int x = 5,y = 6;
///swapr2(x,y);同样参数为longlong型,无法引用int型数据
///引用类型并不会强制转换,原因:当类型不同时,
///编译器会进行隐式转换,创建一个和函数参数相同类型的临时匿名变量
///然后函数参数的引用,会指向的是那两个临时匿名变量
///从而导致最后函数参数指向的是临时匿名变量,达不到我们想要的效果
///此操作在早期c++较宽松时可行。
///三种临时匿名变量会出现的情况,同时下三种引用必须为const型
int s = swapr3(a,b);///此操作同是int引用longlong变量
///可行原因在于,函数参数为const型,表示无法更改值。
///那么在这种情况下,引用指向一个临时匿名变量是可以的
int s1 = swapr3(6,7);///此种调用,同样会出现临时匿名变量,因为6,7无地址,所以需要临时匿名变量
int s2 = swapr3(a + 1,b);///同样此也会出现临时匿名变量,因为a+1为表达式无地址
printf("%lld %lld\n",a,b);
printf("%d %d s = %d s1 = %d s2 = %d\n",x,y,s,s1);
}
void work3()///引用结构体相关操作
{
printf("这里为引用结构体的相关操作:\n");
cout << "此函数有三处不同之地" << endl;
cout << "1,使用了指向结构的引用,由use(node)展示"<< endl;
cout << "2,将引用作为返回值" << endl;
cout << "3,使用函数调用来访问结构成员,由use(node).used展示"<< endl;
Node node = { "Rick","I LOVE YOU",0
};
use(node);
cout << "Node : " << node.used << " use(s)\n";
Node node1;
node1 = use(node);
cout << "Node : " << node.used << " use(s)\n";
cout << "Node1 : " << node1.used << " use(s)\n";
cout << "use(node): " << use(node).used << " use(s)\n";
///use的有一个不同之处在于,将const用于引用返回类型。
///此const的作为本不意味着结构Node本身为const,而只意味着
///不能使用返回的引用来直接修改它指向的结构。如省掉此const,则可能可以出现如下语句
///use(node).used = 10;当然此并没有错。
///等价于use(node); node.used = 10;
///总之,省掉const后,便可以编写更简短但含义更模糊的代码。
return ;
}
void work4()///函数模板
{
int x,y;
x = 6; y = 19;
Swap(x,y);
int a = 1,b = 3,c = 5;
Swap(a,b,c);
printf("x = %d y = %d\n",x,y);
printf("a = %d b = %d c = %d\n",a,b,c);
}
int main()
{
work1();
work2();
work3();
work4();
}
java视频+作业:1小时
来源:https://www.cnblogs.com/yrz001030/p/12393533.html