Classes in C++


Introduction and Summary

Classes, fundamental elements of C++, are collections variables and functions operate on those variables. Together these variables and functions coherently describe the qualities of an object. The variables in a class definition are called data members, and a class's functions are called member functions. A class's variables and functions can have one of 3 basic accessibility types. Public members are visible directly through an object, whereas private members are completely hidden, only accessible within the class's member functions. Protected members behave like private members, except that subclasses have direct access to them. One exception to member visibility is that of friend functions, which are allowed to operate on an object's private data members even without belonging to the object's class.

Inheritance, the creation of subclasses from superclasses, is another key feature of C++. Subclasses inherit all the data members and member functions of their superclasses. In addition, they can have extra members to create a more specific kind of object. Some complications arising with inheritance can be solved by using composition, that is, by including objects in a class as data members.

Take a Study Break

SparkLife

Star Trek gets SEXY

Chris Pine and Zoe Saldana heat up the red carpet!

SparkLife

Are you afraid of relationships?

Auntie SparkNotes can help!

SparkLife

Wanna get JLaw's gorgeous glow?

Click here for simple, sexy makeup tricks!

SparkLife

Sexy starlet style

See every single look from the Met Gala!

SparkLife

Who'd be on your zombie-apocalypse crew?

We already dib'sed Genghis Khan.

Geek out!

The MindHut

Geeky Actors: Then and Now

Travel back in time!

The MindHut

Villains We Want These Actresses to Play

From super cute to super bad!

The MindHut

10 Movies Better Than Their Books

What do you think?

The MindHut

How To Look Like J-Law...

When you don't look like J-Law.

The MindHut

12 Scientific Inaccuracies in Into Darkness

What did Star Trek get wrong?