c++第一节课

本文介绍了C++编程的基础操作,包括使用vim创建cpp文件并用g++编译,讲解了C++的基本语法,如头文件、命名空间、变量定义、布尔类型、指针比较以及浮点数的输出格式。此外,还提到了域运算符`::`的使用。

1.

使用vim创建后缀为cpp的文件,使用g++编译

vim hello.cpp

g++ hello.cpp -o hello

如果使用gcc编译的话:gcc hello.cpp -lstdc++ -o hello

2.

#include <iostream>                 //头文件

using namespace std;              //using 是申明,namespace是命名空间,作用是防止定义的变量函数重名

int main()

{

        cout<<"hello world !"<<endl;              //cout是输出,cin是输入,endl是换行,这写都是在std类里面

        return 0;

}

3.

c  语言中所有变量的定义在函数开头;

c ++ 中那边用到只要在用之前定义就行


bool布尔类型只有两种情况,0和1 就是false 和true


指针和0的比较

if(ptr == NULL)


浮点和0比较

if((f_num >-0.000001)&&(f_num < 0.000001))


float保留三位小数输出

float f_num = 98.5

printf("%.3f",f_num);


4.

::是域运算符

命名空间用namespace定义,格式如下:

namespace 命名空间名

{

      命名空间生命内容

}                                                            //最后不需要分号;




#include  <iostream>
using namespace std;    
namespace nsA            
{
    void print()
    {
        cout<<"nsA"<<endl;
    }
}
namespace nsB
{
    void print()
    {
        cout<<"nsB"<<endl;
    }
}

int main()
{
    nsA::print();                        //使用nsA命名空间内的print
    nsB::print();                       //使用nsB命名空间内的print
    return  0;
}







/*******************************************/
#include <string>
#include <iostream>
using std::cin;                                 //没有使用std,    使用std中的cin,string,cout
using std::string;
using std::cout;
int main()
{
 string temp;
 cin>>temp;
 cout<<temp;
 return 0;
}




/********************************************/
/*using的作用域*/
#include <iostream>
using namespace std;
namespace nsA{
 int var = 10;
}
namespace nsB {
 float var = 12.12;
}
int main()
{
 {                                                                   //这边的两组打过好不可以省略,大括号限制using的作用域
  using namespace nsA;
  cout<<"nsA var ="<<var<<endl;
 }
 {
  using namespace nsB;
  cout<<"nsB var ="<<var<<endl;
 }
 return 0;
}



/**********************************************/
/*重载*/
c++允许函数重载,就是允许函数名一样,但是函数的形参个数和形参类型不能一模一样
#include <iostream>
using namespace std;
int max(int ,int );
double max (double , double);
int max(int ,int , int );
int main()
{
 int a = 2,b = 4, c = 5, d;
 double m = 11.1, n = 13.3,j;
 d = max(a,b);
 cout<<a<<","<<b<<",max = "<<d<<endl;
 j = max(m,n);
 cout<<m<<","<<n<<",max = "<<j<<endl;
 d = max(a, b, c);
 cout<<a<<","<<b<<","<<c<<",max = "<<d<<endl;
 return 0;
}
int max(int x, int y)
{
 return x > y ? x:y;
}
double max(double x,double y)
{
 return x > y ? x:y;
}
int max(int x,int y,int z)
{
 int temp;
 temp = (x > y ? x:y);
 return temp > z ? temp : z;
}




/*********************************/
默认参数的设置
 默认值在函数声明中提供,但当有声明又有定义时,定义中不允许有默认值。
 如果函数只有定义,则默认值可以出现在函数定义中。
 函数调用时,实参与形参按从左到右的顺序进行匹配
函数没有声明时,在函数定义中指定形参的默认值
函数既有定义又有声明时,声明时指定后,定义后就不能再指定默认值
 
#include <iostream>
using namespace std;
void point(int x, int y = 0, int z = 0)                          //不能void point(int x =0,int y ,int z =0)不能void point(int x =0,int y,int z)默认值的设置必须是从右往左设置
{
 cout<<x<<","<<y<<","<<z<<endl;
}
int main()
{
 int x , y , z;
 cout<<"x y z"<<endl;                                              
 cin >>x>>y>>z;                                                           //输入2 4 6
 point(x);                                                                 //  输出2 0 0
 point(x,y);                                                             //输出 2 4 0
 point(x,y,z);                                                            //输出2 4 6
                                                                                //从左往右写入实参,,函数调用时,实参与形参按从左到右的顺序进行匹配

 return 0;
}



/*********************************/
内联函数
作用:可以省去函数调用时候出栈入站的时间花销,多用于简单的函数,在在编译的时候将函数体嵌入到每个内联函数, 对于函数体比较小,执行时间短但频繁使用的函数来说,这种开销比较大。
说明:内联函数不能用于循环语句和switch语句;内联函数在使用前需要定义声明
 函数的定义:
 inline  <类型标识><函数名>(形参列表)
 {
    函数体
 }
 
 
 


#include <iostream>
using namespace std;
inline int max(int x, int y);
int main()
{
 int a[10]={0};
 int i;
 cout<<"please input 10 numbers!"<<endl;
 for(i = 0;i < 10; i++)
 {  
  cin>>a[i];
 }
 int temp = a[0];
 for(i = 0;i < 10;i++)
 {
  temp = max(temp ,a[i]);
 }
 cout<<"in 10 numbers max is:"<<temp<<endl;
 return 0;
}
inline int max (int x ,int y)
{
 return x >= y? x:y;
}


5


/*c++*/
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
class student{
public:                                                                         //这里public不能少
 //char const *name;
 char  name[20];
 int age;
 float score;
 void say()
 {
 // printf("%s,%d,%.3f\n",name,age,score);
  cout<<name<<","<<age<<","<<score<<endl;           //连续输出
 }
};
int main()
{
 class student stu1;
 //stu1.name = "xiaoming";                          //如是指针就直接把字符串付给他就行
 strcpy(stu1.name,"xiaoming");       //just use strcpy(); 若是数组就需要使用strcpy
 stu1.age = 19;
 stu1.score = 98.5;
 stu1.say();                                       //可以直接调用类里面的say()函数
 return 0;
}




/*c*/
#include <stdio.h>
struct student{                                     //对比c++中的不同 c++中使用类class
 const char *name;                              //c++中可以在类里面有函数c语言中不可以
 int age;
 float score;
};
int main()
{
 struct student stu1;
 stu1.name = "xiaoming";
 stu1.age = 19;
 stu1.score = 98.5;
 printf("%s,%d,%.3f\n",stu1.name,stu1.age,stu1.score);
 return 0;
}




7
******************************************
new ,delete
1. malloc ,free
    int *p = (int *)malloc(sizeof(int)*10);
    free(p);
   
2. new,delete
2.1 单个类型
  int *p = new int;
  delete p
 
2.2 希望连续分配的数据
 int *p = new int[10];
 delete[] p;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值