下面就让我们走进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;
}
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 <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] = "阿辉";//全局变量 name
int main1()
{
cout << "我是:" << czq::name << ",今年" << age << endl;
cout << "她是:" << furong::name << ",今年" << salary << endl;
string name = "123123";
cout <<"内部变量 name :" << name << endl;
cout <<"全局变量 ::name :" << ::name << endl;
return 0;
}
输出如下: