The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. |
Home Classes and invariants What is a Class? | ||
What is a Class?
But there is another feature in C++ that also meets this definition; confusingly, it is called a class. In C++, a class is just a structure whose instance variables are private by default. For example, I could have written the Card definition: class Card{ int suit, rank; public: Card (); Card (int s, int r); int getRank () const { return rank; } int getSuit () const { return suit; } int setRank (int r) { rank = r; } int setSuit (int s) { suit = s; } }; I replaced the word struct with the word class and removed the private: label. This result of the two definitions is exactly the same. In fact, anything that can be written as a struct can also be written as a class, just by adding or removing labels. There is no real reason to choose one over the other, except that as a stylistic choice, most C++ programmers use class. Also, it is common to refer to all user-defined types in C++ as "classes," regardless of whether they are defined as a struct or a class.
|
||
Home Classes and invariants What is a Class? |