### The relationship between C and C++
- C++ can be mixed with C code, C can be written in C++, but not the other way around.
- C++ is an object-oriented programming language and C is procedural programming.
- C++ an enhancement to C. C++ has the concept of class, reference, heap memory allocation in addition to C language malloc, free, and new, delete keywords.
###C++ namespace
#### Basic Concepts
Namespaces, also called namespaces, are similar to packages (categories) in Java. When the project is large, it is used to distinguish between code written by different people and code from different libraries. Function names, variable names, class names, and so on can be repeated under different namespaces.
#### The most basic example:
#include <iostream> using namespace std; Void main(){cout << "I love you" << endl; system("pause"); }Copy the code
This is an example of a Hello Word. The things to note are:
- Use the STD namespace by using namespace. You can also use namespaces with STD ::, called access modifiers.
- Cout is the output function of the iostream header file. In C++, header file inclusion does not require the. H suffix. Where << is operator overload. Endl means line break.
#### Custom namespace
Customize a namespace using the namespace keyword:
#include <iostream>
using namespace std;
namespace name1{
int a = 1;
namespace name1_1{
int a = 3;
}
}
namespace name2{
int a = 2;
}
void main(){
cout << name1::a << endl;
cout << name2::a << endl;
cout << name1::name1_1::a << endl;
system("pause");
}
Copy the code
As you can see from the example above, namespaces can be nested.
Structs, structs and namespaces in C++
-
C++ is an enhancement of C. Structures in C++ can have access modifiers, which are shared by multiple variables or functions.
-
Structures in C++ are very similar to classes in Java. In C speech, only function Pointers can be used as member functions. In C++, they are functions directly.
-
Structs in C++ have a this pointer to themselves.
-
After using a namespace in C++, the struct keyword can be omitted when using a structure.
#include
using namespace std;
namespace name1{
struct Teacher{ private: char* name; public: int age; void say(){ cout << "Hello My age is :" << this->age << endl; }};Copy the code
}
void main(){
using name1::Teacher; Teacher t; t.age = 20; system("pause"); Copy the code
}
# # # in c + + class
#include <iostream>
using namespace std;
class AClass
{
public:
int getA(){
//return this->a;
return a;
}
void setA(int a){
this->a = a;
}
private:
int a;
};
void main(){
AClass c;
c.setA(4);
cout << c.getA() << endl;
system("pause");
}
Copy the code
Note:
- Like Java, there is a TIHS pointer to itself.
- Class definitions need to end with a semicolon.
- Here, the c in main is in stack memory, and we can use the new keyword to create the entity of the object in heap memory, but we need to
Boolean types in C++
C++ has a Boolean type bool, but C doesn’t.
#include <iostream>
using namespace std;
void main(){
bool b = true;
if (b){
cout << b << endl;
cout << sizeof(bool) << endl;
}
system("pause");
}
Copy the code
The values of bool are true, false, 1, and 0, and are one byte in size.
###### In C/C++, conditional expressions are true if they are greater than 0 and false if they are less than or equal to 0. In Java, you can only use true and false.
###C++ functions
#### function arguments can have default values, as in PHP.
void test1(int a = 10){
}
Copy the code
Once a parameter value has a default value, all subsequent parameters need to have one
void test2(int a, int b = 10, int c = 20){
}
Copy the code
Ambiguity is important when functions are overloaded.
#### Variable arguments to the function
The header file stdarg.h is required.
-
Start reading variable arguments with va_start, where parameter I is the last fixed argument.
-
Read by vA_arg. The type needs to be specified.
-
End the read by va_end.
#include #include <stdarg.h>
using namespace std;
void test(int i, …) {
va_list args_p; va_start(args_p, i); Int a = va_arg(args_p, int); char b = va_arg(args_p, char); int c = va_arg(args_p, int); va_end(args_p); cout << a << endl; cout << b << endl; cout << c << endl;Copy the code
}
void main(){ test(1, 10, ‘a’, 20); system(“pause”); }
It is also possible to loop, but only if the variables are greater than 0 and of the same type. Example:
void test(int i, ...) { va_list args_p; va_start(args_p, i); While (true){int a = va_arg(args_p, int); if (a <= 0){ break; } cout << a << endl; continue; } va_end(args_p); } void main(){ test(1, 10, 20, 30, -1); //-1 equals system("pause"); }Copy the code
###C++ class
Definitions are usually written in header files and implemented in source files. For example:
The header file:
#pragma once
class Teacher
{
public:
Teacher();
~Teacher();
};
Copy the code
The source file:
#include "Teacher.h"
Teacher::Teacher()
{
}
Teacher::~Teacher()
{
}
Copy the code
Use:
#include "Teacher.h"
void main(){
Teacher t;
system("pause");
}
Copy the code
Function templates in C++
Template functions, equivalent to generics in Java.
For example, to exchange two things, we can implement two functions:
void myswap(int& a,int& b){
int tmp = 0;
tmp = a;
a = b;
b = tmp;
}
void myswap(char& a, char& b){
char tmp = 0;
tmp = a;
a = b;
b = tmp;
}
Copy the code
However, we find that the two functions have the same business logic and different data types, so we can use the function template to reduce the amount of code in this case:
template <typename T> void myswap(T& a, T& b){ T tmp = 0; tmp = a; a = b; b = tmp; } void main(){int a = 10, b = 20; myswap<int>(a,b); cout << a << "," << b << endl; char x = 'v', y = 'w'; myswap(x, y); cout << x << "," << y << endl; system("pause"); }Copy the code
# # # template class
Custom template classes
template<class T>
class A{
public:
A(T a){
this->a = a;
}
protected:
T a;
};
Copy the code
Custom ordinary classes inherit from template classes
class B :public A<int>{
public:
B(int a)
:A<int>(a){
}
};
Copy the code
Custom template classes inherit from template classes
template<class T>
class C :public A<T>{
public:
C(int a)
:A<T>(a){
}
};
Copy the code
Instantiation of a template class
A<int> a(10);
Copy the code
If you feel that my words are helpful to you, welcome to pay attention to my public number:
My group welcomes everyone to come in and discuss all kinds of technical and non-technical topics. If you are interested, please add my wechat huannan88 and I will take you into our group.