C++语法学习_(1)

 C1_input_output.cpp--基本输入输出

"左出右入,靠近关键字的先入或先出"

C2_variables_fuction_definition 参数和函数定义

///带参独立函数定义与调用 ; for 循环结构

#include <iostream>
using namespace std;

//语法: 独立函数定义 与调用
//变量定义
// 函数的输入输出参数定义
//for循环

//定义一个独立的函数
int sum(int n){
int total=0;
for(int i=0;i<=n;i++){
total=i+total;
}
return total;
}

int main(){
int n;
cout<<"please input a positive number for 累加"<<endl;
cin>>n;
//调用函数
cout<<"total is"<<sum(n);
}

或:

#include <iostream>
using namespace std;

//语法: 独立函数定义 与调用
//变量定义
// 函数的输入输出参数定义
//for循环

//声明和定义:一个独立的函数  返回类型 函数名(参数类型 参数名){} 分号可以加可以不加
//或者只声明,在后面任意位置定义。也是可以的。可以在main之后,编译器会自动找到它。
//声明就是写一个空头
int sum(int n); //声明函数--函数原型

int main(){
int n;
cout<<"please input a positive number for 累加"<<endl;
cin>>n;
//调用函数
cout<<"total is "<<sum(n);
}

int sum(int n){  //这里定义函数
int total=0;
for(int i=0;i<=n;i++){
total=i+total;
}
return total;
}

 


C3_布尔类型 

同C语言,一个字节,输出0/1

#include <iostream>
using namespace std;

//define the funciton
bool compfun(int a, int b)
{
bool ret; // bool 类型,同c语言, 一个字节长的,输出结果0/1
ret=a>=b;
cout<<a<<" is bigger than "<<b<<" ? "<<ret<<endl;

return ret;
};


int main (){
int x,y;
cout<<"please input two value for compare"<<endl;
cin>>x>>y;
compfun(x,y);
return 0;
}

C4_const 型变量 ---只读变量

#include <iostream>
/*
常量(Constant)--const

C++中的 const 更像编译阶段的 #define
程序处理过程: 先预处理, 在编译,最后运行!const 变成了只读属性,不能修改

const 和 #define 的优缺点,#define 定义的常量仅仅是字符串的替换,不会进行类型检查,而 const 定义的常量是有类型的,编译器会进行类型检查,
相对来说比 #define 更安全,所以鼓励大家使用 const 代替 #define
*/
using namespace std;
int main(int argc, char const *argv[])
{
    int const  n=100;
    // const int * n; //违法
    //  int n=100;
    // (int*) q=5;
   //操作不被允许,只读变量 n=5; 
   int *p=(int*)&n; 
   //& 取地址, (int*)类型转换 ,把地址付给了p,下面的操作把,p地址的值改成了9,就是n变成了9
   *p=9;
    cout<<n<<endl;
    return 0;
}

C5_内存申请和释放  new  ,delete.

#include <iostream>
using namespace std;

/* 
内参的申请和释放
成对出现的,
c语言用法:
变量类型 * 变量指针= malloc(sizeof(类型)*变量个数) , free(变量指针)
int *p = malloc(sizeof(int),10)  //申请10个int型
free(p)//释放

C++用法:new  类型[个数], delete[个数]类型  //个数可选参数,
类型 *p= new 类型
delete p

 */
int main(int argc, char const *argv[])
{

    bool *p= new bool[2];
    //int *g=new int[3];    
    //double *h=new double[2];
    cout<< "Size of memory request "<<sizeof(h);
    delete[] p;
    return 0;
}

C6_1_define 宏定义

/* 
在多文件编程时,我建议将内联函数的定义直接放在头文件中,并且禁用内联函数的声明(声明是多此一举),
声明和实现要一起写完。不可分开。
宏替换只是单纯的字符替换,
 */
#include <iostream>

using namespace std;

#define SQR(y) y*y
#define SQR1(y) (y)*(y)
#define SQR2(y) ((y)*(y))

//using inline fucntion
inline int SQR_func(int y){
int ret;
ret=y*y;
return ret;
};

int main(int argc, char const *argv[])
{
    int p,y;
    cout<<"plsease input a number to get x^2"<<endl;
    cin>>p;
    y=SQR(p);
    cout<<y<<endl;
    y=SQR(p+1);
    cout<<"p+1*p+1 = "<<y<<" right is "<<SQR1(p+1)<<endl; //易错 p+1*p+1 ,需要在宏定义上加上括号!
    cout<<"100/X*X "<<100/SQR1(p+1)<< "  right is "<<100/SQR2(p+1)<<endl; 
    //100/5*5
    cout<<"\ninline fucction output is "<<100/SQR_func(p+1)<<endl;
    return 0;
}

C6_2_inline_function 内联函数 (可以替换掉宏定义)

内联的函数一般不能太复杂,(不包含循,)只是一些简单的函数。

指针操作!TBC

函数执行过程,main() 中途会跳转执行调用的函数,这个跳转过程有时空开销,大型程序是不明显的, 小段程序就很明显,函数声明+定义出 加上inline标签,这个时候,程序编译阶段,就会把调用函数,替换掉,后面执行时就快了。(类似于宏替换),比宏替换好,程序员友好,不易错,参数计算的宏,推荐用内联函数!

 

#include <iostream>
using namespace std;
//指针的操作!个人记忆:赋值--地址不变,改变值 *P操作 ;赋地址& 改变全部  

inline void swap(int *n, int* m)
{
int temp;
temp=*n;//值赋值
*n=*m;
*m=temp;
cout<<"n = "<<*n<<" m= "<<*m<<endl;
};

int main(int argc, char const *argv[])
{
    int p,g;
//call swap
cout<<"please input two integer for swap "<<endl;
cin>>p>>g;
swap(&p,&g); //&取指针

return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值