目录
C++概述
Ubuntu的终端下,安装时输入的命令:sudo atp-get install g++
检查是否安装成功:g++ -v
C++后缀:*.cpp
C++命名空间
命名空间中声明:namespace 空间名
{
//可以在此声明函数、变量、结构体。。。。
}
#ifndef _TEST_H
#define _TEST_H
namespace name
{
extern int val;//声明全局变量
void func();//声明的函数
}
#endif
1. vim test.cpp
#include"test.h"
#include<stdio.h>
int name::val = 0;
void name::func()
{
printf("hello world\n");
}
2. vim main.cpp
//第一种声明函数
#include"test.h"
int main(int argc,char **argv)
{
name::func();
return 0;
}
//第二种声明函数
/*#include"test.h"
int main(int argc,char **argv)
{
//name::func();第一种声明函数
using name::func;//第二种声明函数
func();
return 0;
}*/
//第三种声明函数
/*#include"test.h"
using name::func;
int main(int argc,char **argv)
{
func();
return 0;
}
*/
3. g++ main.cpp test.cpp -g -Wall
C++输出输入
输入:cin
注意:通过cin使用右移运算符“>>”进行输入时,默认“空白”为输入结束。“空白”指tab,空格和回车。
输出:cout
输出单个对象:cout<<要输出的对象;
输出多个对象:cout<<要输出的对象<<要输出的对象;
换行输出的形式:cout<<要输出的对象<<endl;//此处endl表示换行
头文件:#include<iostream>
using namespace std;
#include<iostream>
using namespace std;
int main(int arg ,int *argv)
{
int num = 0;
char str[20] = {0};
cont<<"input a num"<<endl;
cin>>num;
cont<<"input a str"<<endl;
cin>>str;
return 0;
}