C++struct继承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;
}

C++ 里面结构体是可以继承的,你可以自己从上面的代码中复制下来,class 改为 struct ,一样能用。struct和class区别可以理解为缺省可见性不同、没有虚表(没有多态)等

结构体可以继承,C++里面的类就是根据结构体演变过来的,可以这么说:“结构体就是类”。如果你又什么疑问的话,你可以直接查看C++的头文件,在你的 IDE 中找到头文件,比如“stl_list.h”这个,看看就知道了!

#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;
}

猜你喜欢

转载自blog.csdn.net/Interesting1024/article/details/109109314