The differences between C++ structure struct and class class

  • 2020-06-01 10:23:56
  • OfStack

Because I was busy with the thesis proposal and submission before, I didn't have time to update this series of articles. I read the last article about the scenery in the fog, and I hope I can continue to write this series of articles. Frankly speaking, C++ has many features, and this is not a teaching guide. I will choose some questions worth discussing in the process of learning C++ to talk with you. Ok, let's start with some appetizers and talk about the struct and class keywords.

1.struct keyword:

The C++ language, as a superset of the C language, is compatible with all the grammar rules of the C language. C language is the first programming language I have learned, and I am naturally familiar with its grammar rules. In C language, struct keyword can be used to combine basic data types and realize many kinds of high-level data structures such as figure and tree.

Let's briefly review the usage of struct in C:


struct Node { //  Defines the 1 A tree node 
 int val;
 struct Node* left;
 struct Node* right;
};

intmain() {
 struct Node root = {2,NULL,NULL}; // You can use {a,b,c} The method of direct assignment 
 cout << root.val << endl;

 return 0;
}

We used a simple piece of code to define the data structure of the tree in C. Obviously, using struct Node as a type definition is 10 points long, so we can bring out typedef.


typedef struct {
 int val;
 struct Node* left;
 struct Node* right;
}Node ;

intmain() {
 Node root = {2,NULL,NULL}; // You can use it directly Node The type is defined 
 cout << root.val << endl;

 return 0;
}

Well, that's easy to use. When I started learning C++, I naively assumed that this was how struct was used. But let's not forget that C++ is an object-oriented language, and the designers of C++ made use of the struct keyword. So I was studying the C++ exception handling section, and I saw this code.


struct MyException : public exception {
 const char * what () constthrow () {
 return "C++ Exception";
 }
};

The structure struct defines is used as a class, can be inherited, and includes functions. That's right, in C++, the struct keyword is no longer the same as the C keyword, but almost the same as the class keyword. The only difference is:

The default access for classes, properties, and functions implemented via the struct keyword is public
The default access for classes, properties, and functions implemented through the class keyword is private
So if the code above needs to change the keyword from struct to class, the change is as simple as explicitly adding access to public:


class MyException : public exception{
 public : 
 const char * what () constthrow () {
 return "C++ Exception";
 }
};

2. Thoughts of non-C ++ language programmers:

The difference between struct and class mentioned above is another good interview question, but? ! I don't like this design, obviously, no matter what the case, explicit access is a good coding habit, using the rules of the language to do access control, is not a smart way to do it.

Golang

There are only one way to define a class:


type TreeNode struct {
 Val int
 Left *TreeNode
 Right *TreeNode
 }

There are no additional pits for case-sensitive access control based on the initials.

Python

There are only one way to define a class:


class TreeNode:
def__init__(self,x):
 self.val = x
 self.left = self.right = None

Python has no access control

Java

JAVA has many classes, enumeration classes, anonymous classes, and interface keywords. But there is no way to change the way access control properties are accessed by defining class keywords.

Scala

Similar to JAVA, static class functionality is replaced by the companion object object. Overall, access control is more flexible and more stringent.

3. Summary:

The father of JAVA once said: I left out operator overloading quite a personal choice because I have seen too many people abuse it at C + +.

So Java does not yet support operator overloading. (the grammar sugar is written 1 hour, after the abuse will bring a lot of problems of teamwork)

Therefore, today's article expresses my personal opinion that there are too many complicated features in C++, and many times we may use some bad features because of the convenience of 1. struct should be like literal content 1, a simple structure to use, but if we want to use a class, we should still pick up the class keyword. As for access rights, you specify them explicitly in four words.


Related articles: