默认参数
int test1(int a=5,int b=6) {
return a + b;
}
int test2(int a = 3, int b = 4);
int test2(int a,int b) {
return a + b;
}
占位参数
void test3(int a,int ) {
cout << "test3"<<endl;
}
重载
void test4() {
cout << "无参" << endl;
}
void test4(int a) {
cout << "含int a" << endl;
}
void test4(double a) {
cout << "含double a" << endl;
}
void test4(int& a,int b) {
cout << "不含 const的引用类型 " << endl;
}
void test4(const int& a,int b) {
cout << "含 const 的引用类型" << endl;
}
void test4(int a, double b=3.56) {
}
重写
int main() {
test1(4);
test1();
test1(1, 2);
test2();
test2(5);
test2(2, 2);
test4(3);
test4();
test4(3.67);
test4(10, 4);
int temp = 10;
test4(temp, 3);
}
全部示例
#include<iostream>
using namespace std;
int test1(int a=5,int b=6) {
return a + b;
}
int test2(int a = 3, int b = 4);
int test2(int a,int b) {
return a + b;
}
void test3(int a,int ) {
cout << "test3"<<endl;
}
void test4() {
cout << "无参" << endl;
}
void test4(int a) {
cout << "含int a" << endl;
}
void test4(double a) {
cout << "含double a" << endl;
}
void test4(int& a,int b) {
cout << "不含 const的引用类型 " << endl;
}
void test4(const int& a,int b) {
cout << "含 const 的引用类型" << endl;
}
void test4(int a, double b=3.56) {
}
int main() {
test1(4);
test1();
test1(1, 2);
test2();
test2(5);
test2(2, 2);
test4(3);
test4();
test4(3.67);
test4(10, 4);
int temp = 10;
test4(temp, 3);
}