C++ basics

C++

菜鸟教程

Object oriented programming (OOP):
C++ is completed designed for Object oriented programming: Building Object with Functions

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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值