当前位置:   article > 正文

C++struct继承struct_c++ struct可以继承

c++ 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/714699
推荐阅读
相关标签
  

闽ICP备14008679号