Chapter 7. Defining Your Own Data Types

WHAT YOU WILL LEARN IN THIS CHAPTER:

  • How structures are used

  • How classes are used

  • The basic components of a class and how you define class types

  • How to create, and use, objects of a class

  • How to control access to members of a class

  • How to create constructors

  • The default constructor

  • References in the context of classes

  • How to implement the copy constructor

  • How C++/CLI classes differ from native C++ classes

  • How to define and use properties in a C++/CLI class

  • How to define and use literal fields

  • How to define and use initonly fields

  • What a static constructor is

This chapter is about creating your own data types to suit your particular problem. It's also about creating objects, the building blocks of object-oriented programming. An object can seem a bit mysterious to the uninitiated, but as you will see in this chapter, an object can be just an instance of one of your own data types.

THE STRUCT IN C++

A structure is a user-defined type that you define using the keyword struct, so it is often referred to as a struct. The struct originated back in the C language, and C++ incorporates and expands on the C struct. A struct in C++ is functionally replaceable by a class insofar as anything you can do with a struct, you can also achieve by using a class; however, because Windows was written in C before C++ became widely used, the struct appears pervasively in Windows programming. It is also widely used today, so you really need to know something about structs. We'll ...

Get Ivor Horton's Beginning Visual C++® 2010 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.