走入C++程序世界-------第一个C++程序
来源:互联网 发布:表单令牌的作用 php 编辑:程序博客网 时间:2024/05/18 01:11
下面就让我们走进C++的编程世界,目前介绍的都是在LINUX下的C++程序。废话少说,看下面经典的“hello world!"代码。
#include <iostream> /*io库*/#define N 1/*有些编译器允许main()的返回类型VOID,但是这种写法已不再是合法的C++程序 *请不要养成这种习惯。应该让main()返回int,像下面返回0就可以了。 * */int main(){#if 0 std::cout << "hello world\n";/*标准的hello world!程序*/ /* \n 和 std::endl 都是换行符的意思,但是使用后者会更好 * 因为在有些系统下可能不能识别\n. * endl = end line 的意思*/ std::cout << "my name is jsh "<<std::endl; std::cout << "cumputer 2+3=" << 5 <<std::endl;#endif /*下面是2中方式使用命名空间的方法*/#if N using std::cout; using std::endl;#else using namespace std;#endif cout << "my name is jsh "<<endl; cout << "cumputer 2+3=" << 5 <<endl; return 0;}
使用变量和常量
程序需要一种方式来存储器使用或创建的数据,以便在后面的程序执行期间能够使用它们。这里不再细讲直接看代码吧。
#include <iostream>int main(){ using namespace std; unsigned int Width = 10; unsigned int Length = 20; unsigned int Area = Width * Length; cout << "Width:" << Width <<endl; cout << "Length:" << Length <<endl; cout << "Area:" << Area <<endl;}
字符串处理及string类
#include <iostream>int main(){ using namespace std; char buffer[80] = {'\0'}; cout << "Enter the string:" << endl; //下面2种方法获取数据#if 0 /*没有边界检查,超过可能出现意想不到的后果,不安全*/ cin >> buffer;#else /*最多可以获取79个字符,有边界检查,更安全*/ cin.get(buffer,79);#endif cout << "Here is the Buffer: " << buffer <<endl; return 0;}
#include <string>#include <iostream>int main(){ using namespace std; /*声明并定义一个string对象*/ string str1 ("This is a C++ string! "); cout << "str1 = " << str1 << endl; string str2; str2 = str1; cout << "str2 = " << str2 << endl; /*直接赋值*/ str2 = "Hello string!"; cout << "赋值后的 str2 = " << str2 << endl; string str3; str3 = str1 + str2; cout << "the result of str1 + str2 is = " << str3 << endl; return 0;}
Cin 用法:
/*File: base.cpp *Author: sin *Date: 2014-4-15 *Mail: 413977143@qq.com */#include <iostream>/* input/output bin*/#include <string>using namespace std;int _main(){cout << "请输入您的姓名和年龄:" << endl;string name;int age;cin >> name >> age;cout << name << "您好,您出生于" << 2014 - age << "年。" << endl;return 0; // 可有可无。}
输出如下:
namespace 命名空间用法:
/*File: base.cpp *Author: sin *Date: 2014-4-15 *Mail: 413977143@qq.com */#include <iostream>/* input/output bin*/#include <string>using namespace std;namespace czq{string name = "陈宗权";int age = 40;}namespace furong{char name[] = "芙蓉姐姐";double salary = 800;}using namespace czq;using namespace furong;char name[20] = "阿辉";//全局变量 nameint main1(){cout << "我是:" << czq::name << ",今年" << age << endl;cout << "她是:" << furong::name << ",今年" << salary << endl;string name = "123123";cout <<"内部变量 name :" << name << endl;cout <<"全局变量 ::name :" << ::name << endl;return 0;}
输出如下:
- 走入C++程序世界-------第一个C++程序
- 第一个C程序
- 第一个C程序
- 第一个C程序
- 第一个C程序
- 第一个C程序
- 第一个c程序
- 第一个c程序
- 第一个c程序
- 第一个c程序
- 第一个C程序
- 第一个c程序
- 第一个 Linux c 程序
- 第一个unix c程序
- 第一个Objective-C 程序
- 第一个objective-c程序
- 第一个C程序(1)
- 第一个C语言程序
- CuSparse 第一章
- 人们怀念青春,因为青春代表着年轻
- 嵌入式C开发基础题(2)
- static在C和C++中的用法和区别
- POJ 1036 Gangsters
- 走入C++程序世界-------第一个C++程序
- 数组的循环移位
- 分享一个thinkphp邮件发送类
- 2014迅雷校园招聘笔试题答案
- Python的浅拷贝和深拷贝
- 算法的优雅(十):本能に従顺忠実 翻弄も重々承知
- Unix/Linux C++应用开发-多进程应用开发
- java和js中遍历数组和对象的总结
- python在多玩图片上下载妹子图的实现代码