×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / 我找工作面试时的一个问题,Struct和Class有什么区别?我回答Struct 没有构造和析构函数,现在想想一定不对,谁知道,谢谢先。
    • struct is for ADT. there is no function inside a struct. class is for OOT, there are methods inside a class.
    • 面试就问这种问题?
      • 这道题看来是用来看看你是否知道(仅仅是知道,不是精通)C++
    • Actually, the only different between Struct & Class is that: by default all members in Struct are public while they are private in Class. Both of them can have member Variables and Functions(methods). Ji Ang's answer is not proper.
      • you are right!
      • Really? 我觉得Class 是为了OOP而将 Struct 改造过来 的,因此虽然 Struct里可以有 member function,但如果C编译器里就会出问题,而在C++ 编译器里就可以过去。所以关于 private 和 public 的问题我同意你的,但后面的解释我同意wjiang的。
        • Question is: STRUCT in C++ compared to CLASS in C++; not C.
          Hi, The question is to compare STRUCT & CLASS in C++, not C;
          It's meaningless to compare these two things in C 'cause there is no CLASS in C;
          I had the same experience being asked the same question and I failed. The reason why the employer ask this question is that he want to know if you are familiar with C++, not just using C++ compiler doing the C language stuff.
          So I believe if you answer like your way or Ji Ang's , I bet you will not get good result for the interview.

          I also want to ask a technical question from one of my interview, can anybody answer it:
          How to implement multiple inheritance in VB?

          thanks.
    • I understand Zack's answer is the most important point. If supplemented with your answer, it is good enough.
      Of course, there are difference in the syntax. For such an open question, you just need enumerate the main points. If you could give some examples, however, you will kill a lot of time. By the way, if you could say something about the advantage of class over struct, you may show people that you are really object-oriented.
      • Could anybody say something about the advantage of class over struct?
    • a standard answer
      : The default member and base class access specifiers are different.

      This is one of the commonly misunderstood aspects of C++. Believe it or not, many programmers think that a C++ struct is just like a C struct, while a C++ class has inheritance, access specifiers, member functions, overloaded operators, and so on. Some of them have even written books about C++. Actually, the C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specifier and private base class inheritance. Getting this question wrong does not necessarily disqualify you because you will be in plenty of good company. Getting it right is a definite plus.