c++ 01

输入输出

#include<iostream>
using namespace std;

int main()
{
	int a = 100;
	char str[] = "abcde";

	cout << "Hello World" << endl;
	cout << a << "  " << str << endl; 
	cin >> a;
	cin >> str;
	cout << a <<"  "<<str<< endl;
	system("pause");
	return 0;
}

命名空间

#include<iostream>
using namespace std;
namespace AA
{
	int a = 10;
	int b = 20;
	int c = 30;
}
namespace BB
{
	int a = 100;
	int b = 200;
	int c = 300;
}
int main()
{
	//int a = 100;
	using namespace AA;
	cout << a << endl;//输出局部变量
	//cout和endl是放在std命名空间里的
	//用 命名空间名字:: 来表示使用该命名空间的内容
	//std::cout << a << std::endl;
	/*cout << ::a << endl;*///输出全局变量
	//单独::表示使用全局内容
	
	cout << b << endl;
	cout << c << endl;
	
	//命名空间作用 提高代码的复用性
	system("pause");
	return 0;
}

动态分配内存空间

#include<iostream>
using namespace std;

int main()
{

	//练习
	//字符变量的指针
	//长度为5的整型数组的指针
	//长度为10整形指针数组的指针
	//定义一个指针存arr首元素的地址
	//定义一个指针存arr1首元素的地址
	/*char* p;
	int arr[5];
	int(*p1)[5] = &arr;
	int* arr1[10];
	int* (*p2)[10] = &arr1;
	int* p3 = arr;
	int** p4 = arr1;*/

	int* p = new int;
	*p = 100;
	cout << *p << endl;
	int* p1 = new int(10);//将()里的指写入到申请的空间里
	cout << *p1 << endl;

	int *p2 = new int[5];//连续申请5个int

	//new返回首元素的地址

	for (int i = 0; i < 5; i++)
	{
		p2[i] = i + 1;
	}
	for (int i = 0; i < 5; i++)
	{
		cout << p2[i] << endl;
	}

	delete p;
	delete p1;
	delete []p2;//当释放一个连续申请的空间时 前面一般加[]
	p = NULL;
	p1 = NULL;
	p2 = NULL;

	
	system("pause");
	return 0;
}

默认参数

#include<iostream>
using namespace std;

void Show(int a = 100, char b = 'z', short c = 1);

//默认参数 要出现在函数声明中 
//默认参数 在参数列表中的顺序是从右向左的 即参数列表的左面可以没有 但右面不能
void Show1(int a, char b, short c = 1);

int main()
{

	Show(10, 'x', 30);
	Show();
	Show1(110, 'a');

	system("pause");
	return 0;
}
void Show(int a , char b , short c)
{
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;

}

函数重载

#include<iostream>
using namespace std;
//函数重载 函数名一样 参数列表不一样

int Area(int x, int y)
{
	return x * y;
}
int Area(int r)
{
	return 3 * r * r;
}
int Area(int a, int b, int h)
{
	return (a + b) * h / 2;
}
//函数重载 只能是参数列表不一样 返回值类型也得一样
//使用函数重载的时候 不要使用默认参数 会造成二义性

int main()
{
	cout << Area(3, 4) << endl;
	cout << Area(3) << endl;
	cout << Area(2, 4, 2) << endl;


	system("pause");
	return 0;
}

引用

#include<iostream>
using namespace std;

void Swap(int& ra, int& rb)
{
	int nTemp = ra;
	ra = rb;
	rb = nTemp;
}


int main()
{
	//int a = 100;
	//int& ra = a;
	引用  类型 + & + 名字
	//ra = 200;
	//cout << a << endl;
	引用的本质  给变量起一个别名
	引用没有自己的空间 操作的是原空间
	使用引用的时候必须进行初始化
	//int b = 10;
	//ra = b;
	//cout << a << endl;

	//int* p = &a;
	//int* &rp = p;
	int &*pp = &ra;   //不能给引用定义指针  但是可以给指针定义引用
	//将类对象作为参数时必须使用引用
	int a = 1;
	int b = 2;
	Swap(a, b);
	cout << a << endl;
	cout << b << endl;



	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值