C++struct inherits struct

#include<iostream>
using namespace std;
 
struct A
{
    
    
		int a;
		int b;
};
struct B : A
{
    
    
		int c;
};
 
int main()
{
    
    
		struct B stB;
		stB.a = 1;
		cout<<stB.a<<endl;
		return 0;
}

The structure in C++ can be inherited, you can copy it from the above code yourself, and change the class to struct, and it will work as well. The difference between struct and class can be understood as different default visibility, no virtual table (no polymorphism), etc.

Structures can be inherited. Classes in C++ evolved from structures. It can be said: "Structures are classes". If you have any questions, you can directly check the C++ header file, and find the header file in your IDE, such as "stl_list.h", just look at it!

#include<iostream>
using namespace std;
 
class A
{
    
    
		public:
		int a;
		int b;
};
 
class B :public A
{
    
    
		public:
		int c;
};
 
int main()
{
    
    
		class B stB;
		stB.a = 1;
		cout<<stB.a<<endl;
		return 0;
}

Guess you like

Origin blog.csdn.net/Interesting1024/article/details/109109314