首页 > 编程语言 >c++从入门到精通——C++对于C语言的增强和拓展

c++从入门到精通——C++对于C语言的增强和拓展

时间:2022-11-01 18:36:15浏览次数:44  
标签:return int c++ C语言 C++ test const main


全局变量检测增强

  • int a ;
  • int a = 10; C下可以,C++重定义
    C语言之下,全局变量定义,不会出错。
#include<stdio.h>
int a;
int a = 10;
int main() {
printf("hello world!");
return 0;
}

c++从入门到精通——C++对于C语言的增强和拓展_#include


c++全局变量定义出错

#include<stdio.h>
int a;
int a = 10;
int main() {
printf("hello world!");
return 0;
}

c++从入门到精通——C++对于C语言的增强和拓展_c#_02

函数检测增强

  • 函数的返回值,
  • 形参类型
  • 函数调用参数个数
    C语言中对函数的返回值, 形参类型, 函数调用参数个数
#include<stdio.h>

getRectS(w,h) {
return w * h;
}
void test01()
{
printf("%d\n", getRectS(10, 10, 10));
}
int main() {
test01();
return 0;
}

c++从入门到精通——C++对于C语言的增强和拓展_c语言_03


C++

#include<iostream>

int getRectS(int w,int h) {
return w * h;
}
void test01()
{
printf("%d\n", getRectS(10, 10));
}
int main() {
test01();
return 0;
}

c++从入门到精通——C++对于C语言的增强和拓展_#include_04

类型转换检测增强

c语言

#include<stdio.h>
void test()
{
char * p = malloc(64);
}
int main() {
test();
return 0;
}

c++从入门到精通——C++对于C语言的增强和拓展_c语言_05

c++中满足以下规则 char * p = (char *)malloc(64) C++下必须等号左右一致类型

#include<iostream>
void test()
{
char * p = (char *)malloc(64);
}
int main() {
test();
return 0;
}

struct 增强

  • C++可以在结构体中放函数
  • 创建结构体变量 可以简化关键字struct
    c++中结构体可以增加函数
struct Person
{
int age;
//void func(); C语言下 结构体不可以有函数
};

c语言中定义:

struct Person p; //创建结构体变量时候,必须加关键字struct

c++ 中结构体

Person p;
#include<iostream>
using namespace std;
struct Person {
int age = 1;
void say() {
cout << "hello world!" << endl;
}

};
int main() {
Person p;
Person *pp = &p;
p.say();
pp->say();
return 0;
}

c++从入门到精通——C++对于C语言的增强和拓展_#include_06

bool数据类型扩展

C++才有bool类型6.5.2 代表真 — 1 true 假 ---- 0 false
sizeof = 1

bool类型,无论设设置多大,都设置为1

#include<iostream>
using namespace std;

int main() {
bool flag = true;
cout << "flag=" << flag << endl;
flag = 100;
cout << "flag=" << flag << endl;
flag = false;
cout << "flag=" << flag << endl;

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

c++从入门到精通——C++对于C语言的增强和拓展_c++_07

三目运算符增强

  • C语言下返回的是值
  • C++语言下返回的是变量
#include<iostream>
using namespace std;
void test()
{
//?:
int a = 10;
int b = 20;

printf("ret = %d\n", a > b ? a : b);

*(a > b ? &a : &b) = 100; //C语言下 返回的是值 20 = 100

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

}
int main() {
test();
return 0;
}

c++从入门到精通——C++对于C语言的增强和拓展_#include_08

const增强

C语言下

  • 全局const 直接修改 失败 间接修改 语法通过,运行失败
  • 局部 const 直接修改 失败 间接修改 成功

C++语言下

  • 全局 const 和C结论一样
#include<iostream>
using namespace std;
//const增强
//全局const
const int m_A = 100; // 受到常量区保护,运行修改失败
void test()
{
m_A = 200;
int * p = &m_A;
*p = 200;
}
int main() {
test();
return 0;
}
  • 局部 const 直接修改失败 间接修改 失败
#include<iostream>
using namespace std;

//const增强
//全局const
const int m_A = 100; // 受到常量区保护,运行修改失败
void test()
{
//局部const
const int m_B = 100; //分配到栈上
m_B = 200;
//int * p = &m_B;
//*p = 200;

//printf("%d\n", m_B);

//int arr[m_B]; 在C语言下 const是伪常量,不可以初始化数组
}
int main() {


test();
return 0;
}
  • C++ const可以称为常量

const 链接属性

  • C语言下const修饰的全局变量默认是外部链接属性
  • C++下const修饰的全局变量默认是内部链接属性,可以加extern 提高作用域
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(){

extern const int g_a;

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

system("pause");
return EXIT_SUCCESS;
}
extern const int g_b = 1000;//默认是内部链接属性 可以加关键字 extern 提高作用域

c++版本

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main(){

extern const int g_b;

cout << "g_b = " << g_b << endl;;

system("pause");
return EXIT_SUCCESS;
}


标签:return,int,c++,C语言,C++,test,const,main
From: https://blog.51cto.com/u_13859040/5814682

相关文章