标准C++程序设计学习笔记(二)——第一个C++程序

本文介绍了C++中的名字空间概念,包括匿名空间、名字空间合并和嵌套,以及如何使用`using namespace`。同时,通过"Hello World"程序展示了C++的基本输出操作,强调了标准库`iostream`中的`std`名字空间和相关对象如`cout`、`endl`的使用。
摘要由CSDN通过智能技术生成

目录

第一个程序"Hello world"

名字空间  又称命名空间

匿名空间

名字空间合并

名字空间嵌套


第一个程序"Hello world"

#include <iostream>   //01hello.cpp

using namespace std;


int main(){

  cout << "Hello world!" << endl;

  return 0;

}

1.C++的编译器g++,如果使用gcc编译,需要带上 -std=c++0x,指定其使用标准C++的运行库

2.源文件扩展名:.cpp .cc .C .cxx .c++

  • 最好使用.cpp

#include <iostream>    //02hello.cc
using namespace std;
int  main(){
	cout << "Hello world!" << endl;
	return 0;	
}

3.头文件#include <iostream>     iostream—— IO 流 (in out stream,输入输出流)

#include <iostream>    //03hello.cpp   不使用名字空间
int  main(){
	std::cout << "Hello world!" << std::endl;
	std::cout << 1919 << std::endl;
	std::cout << 3.14 << std::endl;
	std::cout << 2012 << "," << 1314 << "," << std::endl;
	return 0;	
}
  • C++中标准库文件一般都没有.h

    • 当然也允许有.h

  • C++标准库中的内容放在一个叫做std名字空间中

    • using namespace std;

      • cout 对象 标准输出对象

      • cin 对象 标准输入对象

      • cerr 对象 标准错误输出对象

      • endl 对象 换行对象

#include <iostream>    //04info.cpp
using namespace std;


int main(){
	char name[40] = {};
	cout << "input name:";
	cin >> name;
	int age = 0;
	cout << "input age:";
	cin >> age;

	cout << "input salary:";
	f
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值