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>
using namespace std;
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++語言增強的地方
#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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/80739.html
標籤:C++
上一篇:C++ 建構式【新手必學】
下一篇:c++-const
