Differences between structs and classes?

2019-04-17 15:00发布

Do structures support inheritance? I think it's stupid question, but I have not much idea about it.

What is the meaning of writing code like this:

struct A {
   void f() { cout << "Class A" << endl; }
};

struct B: A {
   void f() { cout << "Class B" << endl; }
};

In structures also private section will come, don't they give encapsulation? What is the major difference between structures and classes?

标签: c++ string class
3条回答
干净又极端
2楼-- · 2019-04-17 15:33

Structures are classes with default visibility public. Everything else is equal.

查看更多
ら.Afraid
3楼-- · 2019-04-17 15:43

Yes structures support all features that classes do. The differences are:

  • structure inheritance is public by default
  • structure members are public by default
查看更多
神经病院院长
4楼-- · 2019-04-17 15:56

In C++ only difference between a structure and a class is that for structure the method/member variable visibility is public by default and for class it is private by default. Other than that there is no difference.

查看更多
登录 后发表回答