目录:
1、maximum function with a function prototype.
2、Shifted, scaled integers produced by 1 + rand() % 6.
3、Demonstrating srand (随机种子)
4、Random Number (优点)
5、Inline Functions
6、References and Reference Parameters
7、Using default arguments
8、Unary scope resolution operator. 引用全局变量
9、function overloading, 参数类型不同
10、Templates 模板
1、maximum function with a function prototype.
#include <iostream>
#include <iomanip>
using namespace std;
int maximum(int x, int y, int z);
int maximum(int x, int y, int z) {
int maximumValue{x};
if (y > maximumValue) {
maximumValue = y;
}
if (z > maximumValue) {
maximumValue = z;
}
return maximumValue;
}
int main() {
cout << "Enter three integer values:" ;
int int1, int2, int3;
cin >> int1 >> int2 >> int3;
cout << "The maximum integer value is: " << maximum(int1, int2, int3) << endl;
}
Enter three integer values:86 67 75
The maximum integer value is: 86
2、Shifted, scaled integers produced by 1 + rand() % 6.
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main() {
for(unsigned int i{1}; i <= 20; ++i) {
cout << setw(10) << (1 + rand() % 6);
if (i % 5 == 0){
cout << endl;
}
}
}
//2 2 6 3 5
//3 1 3 6 2
//1 6 1 3 4
//6 2 2 5 5
3、Demonstrating srand (随机种子)
//The program produces a different sequence of random numbers each time it executes, provided that the user enters a different seed. We used the same seed in the first and third sample outputs, so the same series of 10 numbers is displayed in each of those outputs
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main() {
unsigned int seed{0};
cout << "please enter the seed: ";
cin >> seed;
for (unsigned i{1}; i <= 10; ++i) {
cout << setw(10) << (1 + rand() % 6);
if (i % 5 == 0) {
cout << endl;
}
}
}
//please enter the seed: 10
//2 2 6 3 5
//3 1 3 6 2
//please enter the seed: 2
//2 2 6 3 5
//3 1 3 6 2
//please enter the seed: 10
//2 2 6 3 5
//3 1 3 6 2
4、Random Number (优点)
// C++11 provides a more secure library of random-number capabilities that can produce nondeterministic random numbers—a set of random numbers that can’t be predicted.
// use the default random-number generation enginedefault_random_engine—and a uniform_int_distribution,
#include <iostream>
#include <iomanip>
#include <random>
#include <ctime>
using namespace std;
int main() {
// default random-number generation engine to produce uniformly distributed pseudorandom int value
default_random_engine engine{static_cast<unsigned int>(time(0))};
uniform_int_distribution<unsigned int> randomInt{1, 10};
for (unsigned int counter{1}; counter <= 10; ++counter) {
cout << setw(10) << randomInt(engine);
if (counter % 5 ==0 ) {
cout << endl;
}
}
}
//3 5 8 5 2
//6 2 3 6 3
5、Inline Functions
// Placing the qualifier inline before a function’s return type in the function definition advises the compiler to generate a copy of the function’s body code in every place where the function is called (when appropriate) to avoid a function call.
// inline function that calculates the volume of a cube.
#include <iostream>
using namespace std;
inline double cube(const double side) {
return side * side * side;
}
int main() {
double sideValue;
cout << "Enter the side length of your cube: ";
cin >> sideValue;
cout << "Volumne of cube with side " << sideValue << " is " << cube(sideValue) << endl;
}
Enter the side length of your cube: 3.5
Volumne of cube with side 3.5 is 42.875
6、References and Reference Parameters
#include <iostream>
using namespace std;
int main() {
int squareByValue(int);
void squareByReference(int&);
int num1;
cout << "input by_value num: ";
cin >> num1;
cout << "by value res = " << squareByValue(num1) << endl;
int num2;
cout << "input by_reference num: ";
cin >> num2;
squareByReference(num2);
cout << "after reference res = " << num2;
}
inline int squareByValue(int number) {
return number *= number;
}
inline void squareByReference(int& numberRef) {
numberRef *= numberRef;
}
//input by_value num: 3
//by value res = 9
//input by_reference num: 5
//after reference res = 25
7、Using default arguments
#include <iostream>
using namespace std;
unsigned int boxVolume(unsigned int length = 1, unsigned int width = 1, unsigned int height = 1);
unsigned int boxVolume(unsigned int length, unsigned int width, unsigned int height) {
return length * width * height;
}
int main() {
// no arguments--use default values for all dimensions cout << "The default box volume is: " << boxVolume();
// specify length; default width and height
cout << "\n\nThe volume of a box with length 10,\n" << "width 1 and height 1 is: " << boxVolume(10) ;
// specify length and width; default height
cout << "\n\nThe volume of a box with length 10,\n" << "width 5 and height 1 is: " << boxVolume(10, 5);
// specify all arguments
cout << "\n\nThe volume of a box with length 10,\n"
<< "width 5 and height 2 is: " << boxVolume(10, 5, 2) << endl;
}
//The volume of a box with length 10,
//width 1 and height 1 is: 10
//
//The volume of a box with length 10,
//width 5 and height 1 is: 50
//
//The volume of a box with length 10,
//width 5 and height 2 is: 100
8、Unary scope resolution operator. 引用全局变量
#include <iostream>
using namespace std;
int number{7};
int main() {
double number{10.5};
cout << "local double value of number = " << number << "\nGobal int value of number = " << ::number << endl;
}
//local double value of number = 10.5
//Gobal int value of number = 7
9、function overloading, 参数类型不同
#include <iostream>
using namespace std;
int square(int x) {
cout << "square of integer " << x << " is ";
return x * x;
}
double square(double y) {
cout << "square of double " << y << " is ";
return y * y;
}
int main() {
cout << square(7);
cout << endl;
cout << square(7.5);
cout << endl;
}
//square of integer 7 is 49
//square of double 7.5 is 56.25
10、Templates 模板
#include <iostream>
#include "maximum.h"
using namespace std;
int main() {
cout << "Input three integer values: ";
int int1, int2, int3;
cin >> int1 >> int2 >> int3;
cout << "The maximum integer value is :" << maximum(int1, int2, int3);
cout << "\n\nInput three double values: ";
double double1, double2, double3;
cin >> double1 >> double2 >> double3;
cout << "The maximum double value is: " << maximum(double1, double2, double3);
cout << "\n\nInput three characters: ";
char char1, char2, char3;
cin >> char1 >> char2 >> char3;
cout << "The maximum character value is: " << maximum(char1, char2, char3) << endl;
}
//Input three integer values: 1 2 3
//The maximum integer value is :3
//
//Input three double values: 1.2 3.2 1.1
//The maximum double value is: 3.2
//
//Input three characters: A C B
//The maximum character value is: C