Differences between structs and classes?

2019-04-17 14:58发布

问题:

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?

回答1:

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

  • structure inheritance is public by default
  • structure members are public by default


回答2:

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



回答3:

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.



标签: c++ string class