C++教程 第11页

C++ 函数模板-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 函数模板

学习C++ – C++函数模板 函数模板是一个通用的函数描述。 它定义了一个泛型类型的函数。 稍后可以替换特定类型,例如int或double。 通过将类型作为参数传递给模板,编译器将生成一个函数。 因为类型由参数表示,所以参考模板特征作为参数化类型。 函数模板使您能够根据某种任意类型定义函数。 例如,您可以设置如下的交换模板: template <typename AnyType> void Swap(AnyType &a, AnyType &b) { AnyType temp; temp = a; a = b; b = temp; } 第一行设置一个模板,你正在命名AnyType任意类型。 关键字模板和类型名称是强制性的,除了您可以使用关键字类而不是类型名称。 类型名称AnyType是您的选择,只要遵循通常的C ++命名规则即可。 许多程序员使用简单的名字,如T. 其余的代码描述了交换AnyType类型的两个值的算法。 例子 以下代码使用T代替AnyType作为类型参数。 #include <iostream> using namespace std; // function template prototype template <typename T> // or class T void Swap(T &a, T &b); int main(){ int i = 10; int j = 20; cout << "i, j = " << i << ", " << j << ".\n"; Swap(i,j); // generates void Swap(int &, int &) cout << "Now i, j = " << i << ", " << j << ".\n"; double x = 2.5; double y = 8.7; cout << "x,...

C++ 函数重载-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 函数重载

学习C++ – C++函数重载 函数多态性(也称为函数重载)允许我们使用不同数量的参数来调用相同的函数。 函数重载可以创建具有相同名称的多个函数。 多态性意味着具有许多形式。 函数重载的关键是函数的参数列表,也称为函数签名。 如果两个函数以相同的顺序使用相同数量和类型的参数,则它们具有相同的签名。变量名称并不重要。 只要函数具有不同的签名,C++就可以使用相同的名称定义两个函数。 参数的数量或参数的类型,或两者都有所不同。 例子 例如,您可以使用以下原型定义一组print()函数: void print(const char * str, int width); // #1 void print(double d, int width); // #2 void print(long l, int width); // #3 void print(int i, int width); // #4 void print(const char *str); // #5 当您使用print()函数时,编译器会将您的使用与具有相同签名的原型相匹配: print("a", 15); // use #1 print("a"); // use #5 print(2020.0, 11); // use #2 print(2020, 12); // use #4 print(2020L, 15); // use #3 例子 // Overloaded functions. #include <iostream> using namespace std; // function square for int values int square( int x ) { cout << "square of integer " << x << " is "; return x * x; } // end function square with int argument // function...

C++ 函数默认参数-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 函数默认参数

学习C++ – C++函数默认参数 默认参数是一个自动使用的值,如果从函数调用中省略相应的实际参数。 例如,如果在my_func(int n)n中的默认值为1,则调用my_func()的函数与my_func(1)相同。 要建立默认值,请使用函数原型。 编译器查看原型以查看函数使用多少个参数。 以下原型使用默认参数创建一个函数。 char * left(const char * str, int n = 1); 上面的函数返回一个char指针。 要使原始字符串不变,使用第一个参数的const限定符。 您希望n的默认值为1,因此您将该值赋值给n。 默认参数值是一个初始化值。 如果你只留下n,它的值为1,但是如果你传递一个参数,新的值将覆盖1。 当您使用具有参数列表的函数时,必须从右到左添加默认值。 int my_func(int n, int m = 4, int j = 5); // VALID int my_func(int n, int m = 6, int j); // INVALID int my_func(int k = 1, int m = 2, int n = 3); // VALID 例子 // Using default arguments. #include <iostream> using namespace std; // function prototype that specifies default arguments int boxVolume( int length = 1, int width = 1, int height = 1 ); int main() { // no arguments--use default values for all dimensions cout << "The default box volume is: " << boxVolume(); // specify length;...

C++ 函数指针-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 函数指针

学习C++ – C++函数指针 声明一个函数的指针 指向函数的指针必须指定指针指向什么类型的函数。 声明应该识别函数的返回类型和函数的参数列表。 声明应提供与函数原型相同的功能相同的信息。 假设我们有以下函数。 double my_func(int); // prototype 下面是一个适当指针类型的声明: double (*pf)(int); pf指向一个使用一个int参数并返回类型double的函数。 我们必须把括号围绕* pf提供适当的运算符优先级。 括号的优先级高于*运算符。 *pf(int)表示pf()是一个返回指针的函数。 (*pf)(int)表示pf是指向函数的指针。 在您正确声明pf后,您可以为其赋值匹配函数的地址: double my_func(int); double (*pf)(int); pf = my_func; // pf now points to the my_func() function my_func()必须匹配签名和返回类型的pf。 使用指针调用函数 (*pf)起到与函数名称相同的作用。 我们可以使用(*pf),就像它是一个函数名一样: double (int); double (*pf)(int); pf = my_func; // pf now points to the my_func() function double x = my_func(4); // call my_func() using the function name double y = (*pf)(5); // call my_func() using the pointer pf 例子 以下代码演示了在程序中使用函数指针。 #include <iostream> using namespace std; double my_func(int); void estimate(int lines, double (*pf)(int)); int main(){ int code = 40; estimate(code, my_func); return 0; } double my_func(int lns) { return 0.05 * lns; } void estimate(int lines, double (*pf)(int)) { cout <<...

C++ 函数参数-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 函数参数

学习C++ – C++函数参数 函数参数和值传递 C++通常通过值传递参数。 例如, double volume = cube(side); 这边是一个变量,在运行中,值为5。 cube()的函数头是这样的: double cube(double x) 调用此函数时,将创建一个新的类型double变量x,并将其初始化为5。 多个参数 一个函数可以有多个参数。 在函数调用中,你只需用逗号分隔参数: #include <iostream> using namespace std; void n_chars(char, int); int main() { int times; char ch; cout << "Enter a character: "; cin >> ch; while (ch != "q") // q to quit { cout << "Enter an integer: "; cin >> times; n_chars(ch, times); // function with two arguments cout << "\nEnter another character or press the q-key to quit: "; cin >> ch; } cout << "The value of times is " << times << ".\n"; return 0; } void n_chars(char c, int n) // displays c n times { while (n-- > 0) // continue until n reaches 0...

C++ 函数-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 函数

学习C++ – C++函数 以下代码显示如何创建函数。 #include <iostream> using namespace std; void my_function(int); // function prototype for my_function() int main() { my_function(3); // call the my_function() function cout << "Pick an integer: "; int count; cin >> count; my_function(count); // call it again cout << "Done!" << endl; return 0; } void my_function(int n) // define the my_function() function { using namespace std; cout << "Hi:" << n << " ." << endl; // void functions don"t need return statements } main()函数调用my_function()函数两次,一次参数为3,一次为变量参数。 上面的代码生成以下结果。 定义函数 没有返回值的函数称为类型void函数,并具有以下一般形式: void functionName(parameterList) { statement(s) return; // optional } parameterList设置传递给函数的参数的类型和数量。 可选的return语句标记函数的结尾。 通常,您使用void函数执行某种操作。 具有返回值的函数产生返回给调用者的值。 这样的函数被声明为具有与返回的值相同的类型。这是一般的形式: typeName functionName(parameterList) { statements return value; // value is type cast to type typeName } 函数原型 要使用C ++函数,您必须提供函数定义,提供函数原型和调用函数。 #include <iostream> using namespace std; void...

C++ 递归函数-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 递归函数

学习C++ – C++递归函数 C++函数可以调用自身。 这种行为称为递归。 例子 #include <iostream> using namespace std; void countdown(int n); int main(){ countdown(4); // call the recursive function return 0; } void countdown(int n){ cout << "Counting down ... " << n << endl; if (n > 0) countdown(n-1); // function calls itself cout << n << "\n"; } 上面的代码生成以下结果。 例2 演示递归函数阶乘。 #include <iostream> #include <iomanip> using namespace std; unsigned long factorial( unsigned long ); // function prototype int main() { // calculate the factorials of 0 through 10 for ( int counter = 0; counter <= 10; ++counter ) cout << setw( 2 ) << counter << "! = " << factorial( counter ) << endl; } // end main // recursive definition of function...

C++ while循环-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ while循环

学习C++ – C++ while循环 while循环是初始化和更新部分的for循环;它只是一个测试条件和主体: while (test-condition) body 如果表达式计算结果为true,程序将执行正文中的语句。 以下代码显示了如何使用while循环读取用户输入。 #include <iostream> int main() { using namespace std; char ch; int count = 0; // use basic input cout << "Enter characters; enter # to quit:\n"; cin >> ch; // get a character while (ch != "#") // test the character { cout << ch; // echo the character ++count; // count the character cin >> ch; // get the next character } cout << endl << count << " characters read\n"; return 0; } 上面的代码生成以下结果。 例子 以下循环遍历字符串中的每个字符,并显示字符及其ASCII代码。 #include <iostream> const int ArSize = 20; int main() { using namespace std; char name[ArSize]; cout << "Your first name, please: "; cin >> name; cout << "Here is your name, verticalized...

C++ for-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ for

学习C++ – C++ for 以下代码显示了如何使用for循环。 #include <iostream> int main(){ using namespace std; int i; // create a counter for (i = 0; i < 5; i++) cout << "C++ knows loops.\n"; cout << "C++ knows when to stop.\n"; return 0; } 上面的代码生成以下结果。 for循环部分 for循环提供了执行重复任务的逐步操作。 for循环的部分处理这些步骤: 初始设定值 执行测试以查看循环是否应该继续 执行循环操作 用于测试的更新值(s) 控制部分后的语句称为循环体,只要测试表达式保持为真,就执行该语句。 for (initialization; test-expression; update-expression) body 以下代码显示了如何在for循环中使用数字测试。 #include <iostream> using namespace std; int main() { cout << "Enter the starting countdown value: "; int limit; cin >> limit; int i; for (i = limit; i; i--) // quits when i is 0 cout << "i = " << i << "\n"; cout << "Done now that i = " << i << "\n"; return 0; } 上面的代码生成以下结果。 注意 该程序使用一个循环来计算连续阶乘的值。 然后它使用第二个循环显示结果。此外,程序还介绍了使用外部声明的值。 #include...