Expert C Programming

(Jeff_L) #1

hardware instructions, like "floating-point multiply".


The next development allowed programmers to group together various data types into user-
defined records (structs in C), but there was no way to restrict the functions that could
manipulate the data or control access to the individual fields. If a struct was visible at all,
any part of it could be modified in any way. There was no way to tie the functions to the
types so that it was clear they belonged together.


The current state of the art is object-oriented programming languages that enforce data
integrity by bundling together the user-defined data structures plus the user-defined
functions that are allowed to operate on them. No other functions are allowed to access the
data. This extends strong typing from built-in data types to user-defined data types.


Showing Some Class—Giving User-Defined Types the Same Privileges as Predefined
Types


The C++ class mechanism provides OOP encapsulation. A class is the software realization of
encapsulation. A class is a type, just like char, int, double, and struct rec * are types,
and so you must declare variables of the class to do anything useful. You can do pretty much anything
to a class that you can do to a type, such as take its size, or declare variables of it. You can pretty
much do anything to an object that you can do to a variable, for example, take its address, pass it as an
argument, receive it as a function return value, make it a constant value, and so on. An object (variable
of a class type) can be declared just like declaring any other variable:


Vegetable carrot;


Here Vegetable is the name of a class (more about how to create the class itself shortly), and
carrot is an object of that class. It's a helpful convention to start class names with a capital letter.


A C++ class allows user-defined types to:



  • group together user-defined types and the operations on them.

  • have the same privileges and appearance as built-in types.

  • build up sophisticated types out of more basic ones.

Free download pdf