C++学习 <二> 基础语法(1)

1、cin、cout

int main() 
{
	cout << "Hello World!" << endl;
	int age;
	cin >> age;
	cout << "age is" << age << endl;
	getchar(); // 等待键盘输入,一直卡在这里(如果敲回车,就会读取键盘输入)
	getchar(); // 2个getchar
	return 0;
}

2、函数重载

  • C语言不支持函数重载
    C++支持

重载:
1.函数的名字相同,但是参数的类型不同,或者参数的个数不同,参数顺序不同
2.参数相同,返回不同,不能构成函数重载

// 函数重载:
int sum(int v1, int v2)
{
	return v1 + v2;
}

int sum(int v1, int v2, int v3)
{
	return v1 + v2 + v3;
}
// 函数重载:
void func(int v1, double v2) {
	cout << "方法1" << endl;
}

void func(double v1, int v2 ) {
	cout << "方法2" << endl;
}
/*
* 以下不能构成函数重载
int func()
{

}

double func()
{

}
*/
/*
调用函数时,实参的隐式类型转换可能会产生二义性
*/


void func(long v1)
{

}

void func(int v1)
{

}

void func(double v1)
{

}

// 调用不会报错
int main()
{
	func(10);
	func(10.1);
	func(10L);
}
void func(long v1)
{

}

//void func(int v1)
//{
//
//}

void func(double v1)
{

}

// 这样调用报错
int main()
{
	func(10); // 报错:因为10 可以隐式转换为long 或 double类型,都是符合的,编译器不知道到底是调用哪个
}

3、函数重载的本质

  • name mangling 或 name decoration技术

C++编译器默认会对符号名(比如函数名)进行改编、修饰

例如:

// func_long
void func(long v1)
{

}

// func_int
void func(int v1)
{

}

// func_double
void func(double v1)
{

}

4、默认参数

1.默认参数只能从右边到左边

int sum1(int v1 = 5, int v2 = 6)
{
	return v1 + v2;
}

int sum3(int v1, int v2 = 6)
{
	return v1 + v2;
}
int main()
{
	cout << sum3(8) << endl;
	cout << sum1() << endl;
	cout << sum1(10) << endl;
	cout << sum1(10, 20) << endl;
}

2.函数声明里有默认参数值,实现的时候就不能有默认参数值

int sum2(int v1 = 5, int v2 = 6);
int main()
{

}

int sum2(int v1, int v2)
{
	return v1 + v2;
}

3.默认参数可以是全局变量、函数名

int age = 10;
void printAge(int v1 = age) // 全局变量
{
	cout << "age = " << v1 << endl;
}

void test(int a)
{
	cout << "test-int" << a << endl;
}

void func(int v1, void(*p)(int) = test)  // 函数名
{
	p(v1);
}



int main()
{
	printAge();
	func(20);
}

5、extern “C”

  • extern “C” 会按照C语言来编译,而不是C++来编译
#include <iostream>
using namespace std;

extern "C" void func()
{
	cout << "func" << endl;
}
extern "C" void func(int v)  // 这里会报错,因为C语言不支持重载
{

}

int main()
{

	getchar();
	return 0;
}

  • 直接导入头文件

C语言的头文件


#ifndef test_h
#define test_h

#include <stdio.h>

#endif /* test_h */

int sum(int v1, int v2);
int sum1(int v1, int v2);
int sum2(int v1, int v2);

C语言的实现

#include "test.h"

int sum(int v1, int v2) {
    return v1 + v2;
}

int sum1(int v1, int v2) {
    return v1 + v2 + 1;
}
int sum2(int v1, int v2) {
    return v1 + v2 + 2;
}

C++中使用

#include <iostream>
using namespace std;


extern "C" {
    #include "test.h"
}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    cout <<  sum(10,20) << endl;
    cout <<  sum1(10,20) << endl;
    cout <<  sum2(10,20) << endl;
    return 0;
}
  • C、C++都可以用
#ifdef __cplusplus
extern "C" {
#endif
    int sum(int v1, int v2);
    int sum1(int v1, int v2);
    int sum2(int v1, int v2);
#ifdef __cplusplus
}
#endif

6、防止重复定义

  • 1、如下,通用方式,不受编译器限制
    #ifndef test_h
    #define test_h

#include <stdio.h>

#endif /* test_h */

  • 2、#pragma once 受编译器限制
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值