c++.1

底版

#include <iostream>
using namespace std;

int main():
{
  cout  << "hello world" << end1;
  system("pause");
  return 0
  //单行注释
  /*多行注释*/
}

变量创建

int a = 0
cout << "a=" << a << end1;

常量创建

//第一种方式
#define day 7
cout << "一周总共有:" << day << "天" << end1;
//第二种方式
const int mouth = 12
cout << "一年总共有" << mouth << "月" << end1; 

关键字

如int

//sizeof 
cout << "short占用的内存空间为:" << sizeof (short) << end1; 

输入

//cin
int a = 0
cout << "请给整形a赋值:" << end1 ;
cin >> a ;
cout << "整形变量 a =" << a << end1 ;

标识符

不能有空格,区分大小写

数据类型

整型:short \ int \ long \ long long
实数:float \ double

float f1 = 3.14f; // 默认是double
float f2= 3e2;   // 300

字符:char
字符串:char / string

char ch = 'a' ;           //只能有一个字符
char ch[] = "abc" ;       // 多括号双引号
string ch = "abc" ;//需要头文件#include <string>
cout << (int) ch << end1; //强制转换为整形

转义字符:\n 回车,\t对齐

cout << "一年总共有" << mouth << "月\n" ;//相当于<< end1 

布尔:true 和 false

bool flag = true ;
cout << flag << end1; 

加减乘除

16/3 = 5 // 小数可以
16%3= 1 //小数不行

选择结构

//if后面没有标点
if (a > 6)
{
cout << ''第一种" << end1;
}
//if可嵌套
if (a > 6)
{
cout << ''第二种" << end1;
}
else
{
cout << "也是第二种" << end1;
}
//if
if (a > 6)
{
cout << ''第三种" << end1;
}
else if (a > 4)
{
cout << "也是第三种" << end1;
}
else if (a > 2)
{
cout << "还是第三种" << end1;
}
else
{
cout  << "更是第三种" << end1;
}
//?:是返回a,否返回b
a > b ? a : b
//switch
switch (a)
{
  case 10:
      cout << "这是第三种"  << end1;
      break;
  case 9:
      cout << "也是第三种"  << end1;
      break;
  default:
      cout << "还是第三种"  << end1;
      break;
}
cout << "也是第二种" << end1;
}

循环结构

//while
while (a > 6)
{
cout << ''第一种" << end1;
a++;
}
//do while
do
{
cout << ''第二种" << end1;
a++
} while (a < 6)
//for,执行顺序是int i = 0, i < 10,循环里面的,i++
for (int i = 0; i < 10; i++)
{
cout << ''第三种" << end1;
} 

跳转语句

break

在这里插入图片描述

continue

在这里插入图片描述

goto

在这里插入图片描述

//goto
goto FLAG
某某某
某某某
某某某
FLAG:
某某某
某某某

数组

定义

//第一种,5是指长度,定义数组必须有初始长度
int arr [5];
arr [0] = 10;
arr [1] = 20;
arr [2] = 30;
arr [3] = 40;
arr [4] = 50
//第二种。默认没设置的是0
int arr [5] = { 10,20,30,40,50 };
//第三种
int arr [] = { 10,20,30,40,50 };
//第2.一种,两行三列
int arr [2][3];
arr [0][0] = 10;
arr [0][1] = 20;
等等等
arr [2][3] = 60;
//第2.二种。默认没设置的是0
int arr [2][3] = 
{
 10,20,30
 40,50,60 
 };
//第2.三种,先行后列
int arr [2][3] = { 10,20,30,40,50,60 }
//第2.四种
int arr [][3] = { 10,20,30,40,50,60 }

函数

在这里插入图片描述

int add (int num1, int num2)
{
    int sum = num1 + num2;
    return sum;
}

int main ()
{
    int a = 10;
    int b = 20;
    int c =  add (a,b);
}

如果函数没有返回值,可用viod add ()

要么先定义函数,要么函数声明(声明就是函数第一行)

函数分文件

头文件.h函数声明
源文件.cpp函数定义
//在main中
#include "啥啥.h"
main
//在.h中
#include <iostream>
using namespace std;
函数声明
//在.cpp中
#include "啥啥.h"
函数定义

指针

//定义变量a
int a = 0;
//定义指针p,p是整形
int * p;
//让指针记录a的地址
p = &a;
//访问指针p的内容,有权限修改
num = *p;//访问
*p = 100;//修改
//空指针
int * p = NULL;
//const常量指针,可以改指针指向,不可以改指针内容
const int * p = &a;
p = &b;
//const指针常量,可以改指针内容,不可以改指针指向
int * const p = &a;
*p = 100;
//const都不行
const int * const p = &a;

指针与数组

在这里插入图片描述

指针与函数

地址传递的时候,输入的值会变。不用指针,函数没有输出的时候,输入的值没有变化。

结构体

                       //第一种
                      //struct
struct student
{
  string name;
  int age;
  int score;
};                     //结构体定义
struct student s1;    //student s1;也可以
s1.name = "11";
s1.age = 18;
s1.score = 100;        //创建结构体用户


                                          //第二种
                                          //struct
struct student
{
  string name;
  int age;
  int score;
};                                        //结构体定义
struct student s2 = {"12" , 18 , 100};   //创建结构体用户


                       //第三种
                       //struct
struct student
{
  string name;
  int age;
  int score;
} s3;                  //结构体定义
s3.name = "13";
s3.age = 18;
s3.score = 100;       //创建结构体用户

结构体数组

就是数组,组里每一个变量是结构体
需要先定义结构体
然后在main中定义结构体数组

struct studend arr [3] ;

结构体指针

利用指针访问结构体

struct studend * p = &s;
或者
studend * p = &s;
struct studend * p1 = &s;
或者
studend * p1 = &s;
各变量为 p->name

结构体做函数参数

//值传递
//声明
void add (studenr s);
//main函数
add (p);
//地址传递
vioid add (studenr * s)
//main函数
add (&p);

const

//声明
vioid add (const studenr * s)

内存

在这里插入图片描述

在堆区创建指针

//创建并赋值10
 int * p = new int (10)
//释放
 delect p;
//创建但没赋值
 int * arr = new int [10]
//释放
 delect [] arr;

引用

//意思是创立一个别名,二者具有同等效力,用&
int a = 10;
 int &b = a;
 //不用前面第一句,但是不能改
 const int & ref = 10
//引用做函数参数和返回值
int& test (int &a, int &b)
{
static int a = 0;
return a
}

类和对象

对象:封装、继承、多态

封装

1.将属性和行为写在一起,体现事物

//创建类
class circle
{
 //访问权限
 public:
 //属性
 int r;
//行为
double lenthyuan()
{
  return 2 * 3.14 * r;
} 
}

//使用类
int main ()
{
//通过创建的类,创建用户
circle c1;
//给用户赋值
c1.r = 10
//使用
ans = c1.lenthyuan()
}

2.把属性和行为放在不同权限下,加以控制

public公共权限类内访问类外访问可继承
protected保护权限类内访问类外不访问可继承
private私有权限类内访问类外不访问不可继承

class与struct十分相似,只是class默认private,struct默认public

初始化-构造函数 与 清理-析构函数

//创建类
class circle
{
 public:
//构造函数
circle(){}
//析构函数
~circle(){}
}

//自动调用构造函数circle()和析构函数~circle(){}
int main ()
{
circle c1;
}
构造函数
//创建类
class circle
{
 public:
//构造函数-拷贝函数,将传入的c1的属性拷贝到新的类身上
circle( const circle &c1)
{
  r = c1.r;
}
}

//自动调用构造函数circle()
int main ()
{
circle c1;//如果用拷贝构造,得先有一个类,比如有一个c1,那么新的c2用的是circle c2( c1 )
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值