c++--語言自己

c++

  • 面向對象概念(cout cin 類、對象 面向對象和麪向過程求解問題)
  • 易犯錯誤模型(引入成員函數的必要性)
  • C語言和C++語言的關係
  • namespace 定義(嵌套)、使用、標準命名空間std、iostream中沒有引入std
  • 實用性加強(變量定義)、全局變量定義檢查(嚴格)、變量類型檢查嚴格、全部變量和函數定義必須有類型
  • struct關鍵字(c中不是新類型),與class功能相同
  • 類型增強 bool 1個字節,但只有true和false
  • c++中三目運算符 返回變量自身 c中返回的是變量的值 (可作左值)

初學c++有點小激動

  • 內聯函數(請求、函數調用處插入函數體,壓棧,跳轉和返回的開銷,與帶參數的宏區別)
  • 默認參數(形參有一個默認值,有一個是默認參數,則右邊的均是默認參數才行)
  • 函數佔位參數 運算符重載後置++ int func(int a, int b, int ) 在函數體內部沒法使用佔位參數
  • C++對C的函數擴展
    • 內聯函數(請求、函數調用處插入函數體,壓棧,跳轉和返回的開銷,與帶參數的宏區別)
    • 默認參數(形參有一個默認值,有一個是默認參數,則右邊的均是默認參數才行)
    • 函數佔位參數 運算符重載後置++ int func(int a, int b, int ) 在函數體內部沒法使用佔位參數
    • 默認參數和佔位參數在一塊兒 int func(int a, int b, int =0)
    • 函數重載
      • 概念(函數名稱同樣 函數參數不同)
      • 函數返回值不是判斷標準
      • 調用規則(按照名稱、個數、類型)
      • 函數重載趕上函數默認參數,調用是二義性
      • 函數指針語法:定義函數類型 定義函數指針類型 定義函數指針變量
      • 函數重載和函數指針在一塊兒
  • 默認參數和佔位參數在一塊兒 int func(int a, int b, int =0)
  • 函數重載
    • 概念(函數名稱同樣 函數參數不同)
    • 函數返回值不是判斷標準
    • 調用規則(按照名稱、個數、類型)
    • 函數重載趕上函數默認參數,調用是二義性
    • 函數指針語法:定義函數類型 定義函數指針類型 定義函數指針變量
    • 函數重載和函數指針在一塊兒

命名空間

#include <iostream>ios

using namespace std;c++

iostream 提供一個叫命名空間的東西, 標準的命名空間是 std函數

#include <iostream>

//方式二:
#if 0
using std::cout; //聲明命名空間中的一個變量
using std::endl;
using std::cin;
#endif

//方式三
using namespace std;

int main(void)
{
    int a = 0;
#if 0
    //方式一:
    std::cout << "nihao shijie" << std::endl;
    std::cout << "nihuai shijie" << std::endl;
#endif

    cout << "nihuai shijie" << endl;

    cin >> a;


    return 0;
}

命名空間的定義

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;



//定義一個命名空間
namespace spaccA {
    int g_a = 10;
}

namespace spaceB {
    int a = 20;

    namespace spaceC {
        struct teacher
        {
            int id;
            char name[64];
        };
    }

    namespace spaceD {
        struct teacher
        {
            int id;
            char name[64];
        };
    }

    using namespace spaceC;

}



int main(void)
{
    //using spaccA::g_a;
    using namespace spaccA;
    int a = 20;
    cout << g_a << endl;


    //spaceB::spaceC::teacher t1;
    //using spaceB::spaceC::teacher;

    //teacher t1;

    //using namespace spaceB::spaceC;
    //spaceB::spaceC::teacher t1;

    using namespace spaceB;
    teacher t1;

    t1.id = 10;

    //spaceB::spaceD::teacher t2;
    //t2.id = 20;

    return 0;
}

c++語言加強的地方spa

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//c++語言對全局變量的定義檢測能力加強了
int g_val; //bss段
//int g_val = 20;

struct  student
{
    char name[64];
    int id;
};

void test1()
{
    //定義一個變量 能夠隨用歲定義
    int i = 0;

    for (int i = 0; i < 10; i++)
    {

    }

    int b = 20;
    cout << "b " << b << endl;
}

void test2()
{
    student s1;
    s1.id = 20;
}


int foo()
{
    return 10;
}

int g(int a)
{
    return 10;
}

//bool類型
void test3()
{
    //true 1  false 0  只能取這兩個值
    bool flag = true;

    flag = false;

    flag = true;
    cout << "flag(true)" << flag << endl;
    flag = false;
    cout << "flag(true)" << flag << endl;
    flag = -20;
    cout << "flag(true)" << flag << endl;

    cout << sizeof(flag) << endl;
}

void test4()
{
    int a = 10;
    int b = 20;
    int c = 0;
    c = (a < b) ? a : b;
    cout << c << endl;
    //! 三目運算符 能夠當左值。
    ((a < b) ? a : b) = 50;
    //a = 50;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    #define A 20
    // const int f = 20;
}

void test5()
{
    const int a = 10; //a 是真正的常量

    int *p = (int*)&a;

    *p = 20;//改變的是臨時開闢的temp變量

    cout << "a =" << a << endl;
    cout << "*p=" << *p << endl;

    int array[a] = { 0 };
    cout << A << endl;//20
    //cout << 10 << endl;
    // cout << f << endl;
}


enum season
{
    SPR = 0,
    SUM,
    AUT,
    WIN
};

void test6()
{
    enum season s = AUT;

    if (s == AUT) {
        cout<<"hello";
    }
}

int main(void)
{

    //test2();
    //test3();
    //g(10, 20, 30);
    test4();
    test5();
    test6();
    return 0;
}

c++語言加強的地方

#include <stdio.h>

int g_val; //bssśÎ
int g_val = 20;//data

struct student
{
    char name[64];
    int id;
};

foo()
{
    return 10;
}

int g(int a)
{
    return 10;
}

void test4()
{
    int a = 10;
    int b = 20;
    int c = 0;

    //×óÖľ ÓŇÖľ

    c = a < b ? a : b;

    printf("c = %d\n", c);

    *(a < b ? &a : &b )= 50;
    //10
    //10 = 50;
    printf("a = %d\n", a);
}

void test5()
{
    const int a = 10;
    //int array[a] = { 0 };

    int *p = &a;//一引用a的地址,並未創造新的空間,p指向a

    *p = 70;

    printf("a = %d\n", a);

}

enum season
{
    SPR = 0,
    SUM,
    AUT,
    WIN,
    WIN1,
    WIN2,
    WIN64,
};

void test6()
{
    enum  season s = 2;

    s = 64;

    if (s == 2) {
        //
    }
}

int main(void)
{
    int a = 10;
    int b = 20;

    struct student s1;
    s1.id = 20;

    test4();
    //,,,


    printf("%d\n", g_val);

    //g(10,20,30,40,50);
    printf("-----------------\n");
    test5();
}
相關文章
相關標籤/搜索