Hello everyone, nice to see you again! Continuing from our last session, we have set up a warm and comfortable environment for C++ development. Today, I will guide you to understand the relationship between C and C++, reviving our long-dormant memories of C!
Differences Between C++ and C
First and foremost, the simplest difference is the names of the two programming languages: one is C, and the other is C plus plus!
Back to the point, let’s join the glorious progress together!
Source file differences
C source file: xxx.c
C++ source file: xxx.cpp
Header file differences
C header file: xxx.h
C++ header file: xxx.hpp
Differences in including header files
Standard C header file <stdio.h>
C++ standard header file <iostream>
Compiler differences
C compiler: gcc
C++ compiler: g++
Input and output function differences
C input function: scanf C output function: printf
C++ input: std::cin C++ output function: std::cout
Moreover: C is a procedural language, while C++ is an object-oriented language.
And my development is oriented towards Baidu programming.
Memory Allocation
Java’s automatic garbage collection mechanism – allocate space freely, and it will be automatically reclaimed.
Memory leaks/memory overflow
As the program continues to run, it may cause the memory used by the program to increase -> the program is forcibly killed.
C memory allocation
Allocate with malloc() and free with free()
C++ memory allocation
Allocate with new() and free with delete()
Namespaces
As projects grow larger, you may also reference functions or code written by others, even variables.
For example, a variable time -> system global time.
If you write code and also define time -> it may cause data conflicts.
Compilation will simply report an error! C does not allow duplicate global variables!
C++ introduced namespaces to solve naming conflicts.
Keyword namespace
Namespaces can be simply understood as adding an identifier in front of the variable!
#include <iostream>#include <stdio.h>using namespace std;namespace myspace{int A;int B;};namespace myspace_test{int A;int B;};int A;int B;namespace temp{int test;};using namespace temp;//明确的表示接下来的代码要用temp 命名空间int test;//这是嘛????int main(){ ::test = 20;printf("Global variable test==%d\r\n",::test); temp::test= 30;printf("Namespace test ==%d\r\n",temp::test); A = 20;//我在给哪个A赋值?->定义全局A B = 30;//我在给哪个B赋值?->定义全局B myspace_test::A = 22;//相当于我在给myspace_test命名空间的 A myspace_test::B = 32;//相当于我在给myspace_test命名空间的 B myspace::A = 21;//相当于我在给myspace命名空间的 A myspace::B = 31;//相当于我在给myspace命名空间的 B printf("A==%d\r\n",A);printf("B==%d\r\n",B);printf("myspace_test_A == %d\r\n",myspace_test::A);printf("myspace_test_B == %d\r\n",myspace_test::B);printf("myspace_A == %d\r\n",myspace::A);printf("myspace_B == %d\r\n",myspace::B);return 0;}
Boolean Type
bool has only two results -> true = 1 false = 0
Often used in C++ function return or logical judgment: Turn on the light, turn off the light……….
Common bug issue: Most open-source projects in C have a common point.
Unified return result: success returns 0, failure returns non-0 -> exactly the opposite of bool.
No big deal!
String Type
In C language, there is no string type -> char -> occupies a few, 1. C language has char str[] -> arrays are quite good! You can also temporarily create strings, but it’s actually not perfect, not as comfortable as the string type introduced in C++.
The string type introduced in C++ can also call internal functions of the string type, which can quickly help you complete some functions.
Family members can be reminded through vscode, for example:
string s3;
s3. After you type this ., vscode will automatically remind you of some functions.
You can quickly use to get work done: *Retrieve a single character,*String length…..
And so on
The feeling of knowledge coming out after going through your brain is so wonderful! That’s all for today’s sharing! Please don’t forget to give me a thumbs up, and family members who are interested in me can leave a message. Those with questions can also privately message us!