1.helloworld程序
#include <iostream> using namespace std; int main() { cout<<"hello world"<<endl; return 0; }
2.命名空间 namespace
本质:程序员开辟的一块内存空间
作用:避免名字冲突
使用:
定义名字空间
namespace 名字空间名
{
//各种成员
}
名字空间用法
名字空间用法
1.使用作用域运算符
命名空间名::要访问的成员
std::cout
2.名字空间指令(掌握)
using namespace 名字空间名称;
3.名字空间声明
using 名字空间名称::要访问的成员;
4.无名名字空间(掌握)
不属于任何名字空间的标识符,将被编译器放到无名名字空间中
::要访问的成员
-------------------------------------------------------------------------------------------------------------------------------
练习:
#include<iostream> using namespace std; namespace ns { int a; void show() { std::cout << "ns::show()" << "\n"; } void print() { cout << "hello c++" << endl; } } using ns::show; int a; int main() { int a = 10; ns::a = 100; cout << ns::a << "\n"; cout << a << "\n"; show(); ns::print(); ::a = 200; cout << "gloab a = " << ::a << endl; return 0; }
3.标准输入输出(如果未包含std命名空间,使用时要加std::)
<< 输出运算符
>> 输入运算符
cout 输出流对象
cin 输入流对象
:: 作用域运算符“的”
std (standard)标准命名空间
endl 换行 '\n'
4.结构、联合、枚举
struct , union, enum
1.当使用结构体、联合、枚举这些类型来定义变量时,可以省略关键字
struct Student
{
int age;
void study();
};
2.在C++中,结构体允许有函数,把结构中的变量称为成员变量,结构中的函数称为成员函数
-------------------------------------------------结构struct-----------------------------------------------------------------------------
#include<iostream> #include<cstring> using namespace std; struct Student { int id; char name[20]; int age; void study() { cout << "我叫" << name << ",今年"<< age << ",正在学习C++" << endl; } }; int main() { Student s; strcpy(s.name,"zhangfei"); s.age = 20; s.study(); return 0; }
-------------------------------------------------联合union-----------------------------------------------------------------------------
练习:编写一个程序证明你的机器使用的是大端模式还是小端模式
大端模式:低位地址存高位字节
小端模式:低位地址存低位字节
#include<iostream> #include<cstdio> using namespace std; typedef union { int i; char p; }aaa; //方法一 int test1() { aaa a; a.i = 0x12345678; printf("a.p = %x\n",a.p); return (a.p==0x78); } //方法二 int test2() { int b = 0x12345678; char *p = (char *)&b; printf("p[0]= %x,p[1] = %x,p[2] = %x,p[3] = %x\n",p[0],p[1],p[2],p[3]); return (p[0]==0x78); } int main() { if(test1()) printf("小端模式!\n"); else printf("大端模式!\n"); if(test2()) printf("小端模式!\n"); else printf("大端模式!\n"); }
-------------------------------------------------枚举enum------------------------------------------------------------------------------
#include<iostream> using namespace std; enum Color{Red,Green,Blue}; int main() { Color c; c = Red; cout << "c = " << c << endl;//? c = Color(100); cout << "c = " << c << endl;//? return 0; }
5.字符串类型string
string,由C++标准库提供,专门用于表示字符串
string str1 = " hello ";
string str2 = "c++";
获得string类型字符串长度: size()、 length()
获得string类型字符串内容:c_str()、data()
string str3 = str1 + str2;
//str3 = helloc++
string类型 有成员变量:char *
字节大小:sizeof(string) == 32;(不同的机器可能不同)
注意:char*可以直接赋值给string类型的变量,由编译器隐式转换, 反过来必须手动转换。
-------------------------------------------------------------------------------------------------------------------------------
#include<iostream> using namespace std; int main() { string str1; str1 = "hello"; string str2 = "c++"; cout << "str1 = " << str1 << endl; cout << "str2 = " << str2 << endl; string str3; str3 = str1 + str2; cout << "str3 = " << str3 << endl; if(str1 == str2) { cout << "str1 == str2" << endl; } else { cout << "str1 != str2" << endl; } cout << sizeof(str1) << endl; cout << sizeof(str2) << endl; cout << sizeof(str3) << endl; cout << str1.size() << endl; cout << str2.size() << endl; cout << str3.size() << endl; // const char *s = str3.c_str(); const char *s = str3.data(); cout << "s = " << s << endl; cout << str3[0] << endl; cout << str3.at(1) << endl; str3.clear(); cout << str3.empty() << endl; return 0; }
6.布尔类型 bool
bool类型是C++的基本类型之一
bool类型有两种值:真true(1) 假false(0)
sizeof(bool) == 1
-------------------------------------------------------------------------------------------------------------------------------
#include<iostream> using namespace std; int main() { bool ok = 1+1;//"hello" NULL 10000 cout << ok << endl; cout << sizeof(bool) << endl; return 0; }
7.引用
引用即别名,就是给一个定义好的变量起另外一个名字
格式:
类型 &引用名 = 已定义的变量;
例如:
int a = 10;
int &ra = a;//ra和a就是指同一个变量,具有相同的存储地址,即ra就是a,a就是ra
注意:
1.引用在定义时必须初始化
2.初始化完成,引用就和它引用的变量绑定起来,不能再改变
作用:C++提出引用的概念,最主要的作用是用于函数参数
优点:
节省空间、提高效率、提高代码可读性
-------------------------------------------------------------------------------------------------------------------------------
#include<iostream> using namespace std; int main() { int a = 10; int &ra = a; cout << "ra = " << a << endl; ra++; cout << "a = " << a << endl; cout << "&ra = " << &ra << endl; cout << "&a = " << &a << endl; return 0; }
------------------------------引用的典型案例,交换两个变量的值-------------------------------------------------------------------------
#include<iostream> using namespace std; void swap(int a,int b)//int a = a,int b = b; { int t; t = a; a = b; b = t; } void swap2(int *a,int *b)//int *a = &a,int *b = &b; { int t; t = *a; *a = *b; *b = t; } void swap3(int &a,int &b)//int &a = a;int &b = b { int t; t = a; a = b; b = t; } int main() { int a = 10; int b = 20; swap3(a,b); cout << "a = " << a << endl; cout << "b = " << b << endl; return 0; }
8.函数重载
在C++,函数重载,指使用同一个函数名称,实现相似的函数功能
区分: 相同的作用域,函数名相同,参数列表不同
函数重载的实现原理:
由C++编译器换名机制实现
-------------------------------------------------------------------------------------------------------------------------------
#include<iostream> using namespace std; int add(int a,int b) { return a+b; } double add(double a,double b) { return a+b; } int main() { cout << add(1,1) << endl; cout << add(1.1,2.2) << endl; return 0; }
9.默认参数
在C++中允许实参与形参的个数不一致
当有实参时,就使用传过来的实参,如果不提供实参,就使用默认值
1.当给某个形参设置了默认值,那么 该形参右边所有的参数必须要有默认值
2.当函数的声明和实现分开时,默认值必须设置在声明中
3.当函数重载与默认参数一起使用时,要避免二义性问题
-------------------------------------------------------------------------------------------------------------------------------
#include<iostream> using namespace std; int add(int a=0,int b=0); int add(int a,int b,int c) { return a+b+c; } int main() { cout << add(10,20) << endl; cout << add(10) << endl; cout << add() << endl; return 0; } int add(int a,int b) { return a+b; }