c++--语言本身
程序员文章站
2023-11-02 15:00:16
c++ + 面向对象概念(cout cin 类、对象 面向对象和面向过程求解问题) + 易犯错误模型(引入成员函数的必要性) + C语言和C++语言的关系 + namespace 定义(嵌套)、使用、标准命名空间std、iostream中没有引入std + 实用性增强(变量定义)、全局变量定义检查( ......
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(); }