C++
Object oriented programming (OOP):
C++ is completed designed for Object oriented programming: Building Object with Functions
- Encapsulation: Building Object, could be private, public or protected
- Inherit.
- Polymorphism
Annotation
// for one line
/* .
.. */ for multiple lines
Data Type
auto: adapted func
question: what will happen if the data exceed the max limitation, and what should we do?
Globle & Local Variable
Transfer Character
const: indicate a constant, unchangable, written with capital letter.
const int LENGTH = 10;
signed & unsigned
c++中的unsigned和signed直接使用的后果.
unsigned nagetive variable could cause mistake
static & extern
extern和static使用.
Save global variable in memory, not in stack
static: avaliable in this file
extern: avaliable in all files
Operator
logic
compiling order
Loop
std::string str = “hello, world”;
for(auto ch : str) {
std::cout << ch << std::endl;
}
Function
exp:
#include <iostream>
using namespace std;
int bigger(int a, int b = 1)
{
return max(a, b);
}
int main()
{
int x = 3;
int y;
cout << bigger(x, y) << endl;
return 0;
}
Lambda:
C++中的Lambda表达式详解.
math
#include <cmath>
Random
real random
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
{
int i,j;
// 设置种子
srand( (unsigned)time( NULL ) );
/* 生成 10 个随机数 */
for( i = 0; i < 10; i++ )
{
// 生成实际的随机数
j= rand();
cout <<"随机数: " << j << endl;
}
return 0;
}
#include <iostream>
#include<stdio.h>
#include<time.h>
#define random(x)(rand()%x)
using namespace std;
int main()
{
srand((int)time(0));//部署随机种子
for (int i = 0; i < 10; i++){
cout << random(100) << endl;
//输出0-100的随机数
};
return 0;
}
Array
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
balance[4] = 50.0;
//multi-dim array
int a[3][4] = {
{0, 1, 2, 3} , /* 初始化索引号为 0 的行 */
{4, 5, 6, 7} , /* 初始化索引号为 1 的行 */
{8, 9, 10, 11} /* 初始化索引号为 2 的行 */
};
how to return array in Functions?
Pointer
#include <iostream>
using namespace std;
int main ()
{
int var = 20; // 实际变量的声明
int *ip; // 指针变量的声明
ip = &var; // 在指针变量中存储 var 的地址
cout << "Value of var variable: ";
cout << var << endl;
// 输出在指针变量中存储的地址
cout << "Address stored in ip variable: ";
cout << ip << endl;
// 访问指针中地址的值
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
The necessity of existence?
Structure
#include <iostream>
#include <cstring>
using namespace std;
void printBook( struct Books book );
// 声明一个结构体类型 Books
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( )
{
Books Book1; // 定义结构体类型 Books 的变量 Book1
Books Book2; // 定义结构体类型 Books 的变量 Book2
// Book1 详述
strcpy( Book1.title, "C++ 教程");
strcpy( Book1.author, "Runoob");
strcpy( Book1.subject, "编程语言");
Book1.book_id = 12345;
// Book2 详述
strcpy( Book2.title, "CSS 教程");
strcpy( Book2.author, "Runoob");
strcpy( Book2.subject, "前端技术");
Book2.book_id = 12346;
// 输出 Book1 信息
printBook( Book1 );
// 输出 Book2 信息
printBook( Book2 );
return 0;
}
void printBook( struct Books book )
{
cout << "书标题 : " << book.title <<endl;
cout << "书作者 : " << book.author <<endl;
cout << "书类目 : " << book.subject <<endl;
cout << "书 ID : " << book.book_id <<endl;
}