1. 程式人生 > >c++基礎篇---HelloWorld

c++基礎篇---HelloWorld

end endif 面向對象 spa name printf -h oid pre

c++入門程序

c中的輸入和輸出,一般使用標準庫中printf,scanf去進行讀寫。

#if 0

#include<stdio.h>
int main()
{
    //輸出
    printf("hello world...\n");
    //輸入
    int a = 0;
    printf("請輸入一個整數:");
    scanf("%d", &a);
    return 0;
}

#endif

#if condition...#endif c/c++中的預編譯指令,只有當條件滿足時才去編譯。

作為對比:

c++中簡化了對輸入輸出的書寫,通過引入標準命名空間std的方式使用cin,cout進行標準的輸入輸出。

#include<iostream>
using namespace std;
//std{
//    cout...
//iostream有一個標準的命名空間std,定義了cin,cout等關鍵字
//}
int main(void)
{
    //輸出
    cout << "helloworld" << endl;
    //輸入
    int a = 0;
    cout << "請輸入一個整數:";
    cin >> a;
    cout << "a="<< a << endl;
    return
0; }

通過書寫可以看出c++完全繼承c的特性,但又多了面向對象的特征。

c++基礎篇---HelloWorld