gogoWebsite

Use new, struct keywords and Class keywords in c++

Updated to 2 days ago
Use new in c++
int *a = new int[5];
 class A {...} //Declare a class A
 A *obj = new A(); //Create an object using new
 delete []a;
 delete obj;

1. New can directly allocate memory space on objects.

2. An object can be created, the constructor will be called and the memory space of the object is in the heap. The memory space of the object is in the stack when new is created.

In C++, struct keyword and Class keyword

There are only two differences between classes and structures in C++, and there is no difference except this.

(1) The default member access permissions in class are private, while in struct are public.

(2) Inheriting from class is private inheritance by default, while inheriting from struct is public inheritance by default.
That is to say

Note: struct in C++ is a new type definition declaration, so typedef can be omitted. When defining variables, struct can also be omitted, instead of using typedef to give a new name like C language, you need to use the struct structure name to define variables.
MacOS Xcode cannot be omitted.

Right now:

If typedef is not used in C language

struct A {...}; //Declare a structure A

 struct A a1; //Create a variable of type A

In c++

struct A {...} //Declare a struct A
  A a1; // Create variables of type A directly

 A *obj = new A(); //Create structure variables using new