C++基础知识(1)

C语言与C++的区别

CC++
拓展名.c.cpp
头文件<stdio.h>< iostream>
常用输入输出scanf,printfcin,cout
变量初始化int a = 0;int a = 0;,int a(0);
函数形参不能有默认值形参可以有默认值

注意:

  1. C++完全兼容C语言,C语言程序把后缀改成.cpp程序一样用。
  2. C++需要“使用了标准命名空间”。

C++函数

C++函数可以重载,名称一样,参数个数不一样,带有默认形参的函数中,其参数个数按照不带默认值的形参计数,只要个数不一样,就可以进行函数重载。

#include <iostream>
using namespace std;
void fun(int a, int b, int c, int d = 0)
{
    cout << "1" << endl;
}
void fun (int a, int b)
{
    cout << "2" << endl;
}
int main()
{
    fun(1, 2);
    cout << showbase << hex << 175 << endl;
    cout << uppercase << showbase << hex << 175 << endl;
    double a = 2356.8712;
    cout << scientific << a << endl;
    cout << fixed << a << endl;
    cout << showpos << a;
    return 0;
}

输出:

2
0xaf
0XAF
2.356871E+03
2356.871200
+2356.871200

C++输出

如果输出太长的时候,可以分开写:

cout << “afdsfa”
<<“asdfadf”;
或者
cout << “afdsfa
asdfasf”;

	cout << "afdsfa"
    <<"asdfadf"
    << endl;
    cout << "afdsfa\
    asdfasf" << endl;//输出结果包含一个tab间隙
    cout << "afdsfa\
asdfasf" << endl;

afdsfaasdfadf
afdsfa asdfasf
afdsfaasdfasf

bool类型

  • true
  • false
    只有数值0代表false,其他都是true。

引用

int a;
int &b = a;

引用格式:

类型名 & 引用名 = 目标变量;

引用名与目标变量共用一个存储空间,和指针很像。

#include <iostream>
using namespace std;
void fun(int b, int & cc)
{
	b = 3;
	cc = 3;
}

int main()
{
    int a = 1, b = 1;
    fun(a, b);
    cout << a << endl << b << endl;
    int &ab = a;
    cout << a << endl;
    ab = 5;
    cout << a << endl;
    return 0;
}

输出:

1
3
1
5

注意:两种方式可以在函数调用时可以直接访问对应的存储空间,一种指针,一种引用,引用的方式更简洁和方便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小猛笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值