An Introduction to Numerical Methods in C++, Revised Edition

Chapter 5: Classes

The standard types we have dealt with so far all have the property that the usual rules of combination are defined: the language allows us, for example, to add or divide integers and floating point numbers, and to convert between them automatically or at will. We saw, in 1.4.3, how to define structures, and how thereby to define new types; but, as we left them, these types were not fully satisfactory because we did not consider how to combine two or more objects defined in terms of them. In C++, a class is a user-defined type, a generalization of a structure, which includes appropriate rules for construction, initialization and destruction of objects, for combining them into expressions, and for input and output; and which, if it is properly defined, can be used like any standard type. Classes are sometimes referred to as abstract types because their data members may be hidden, but they are no less real than any other type! In this chapter, we shall illustrate the concept of class by constructing class representations of the complex numbers and of strings. We shall also consider arrays of class objects. In this way we shall introduce many of the features and problems of representing data structures by classes, which will be applied in later chapters.

5.1 Complex numbers

The simplest way to represent complex numbers is to declare a structure with two floating point members representing real and imaginary parts:

struct complex {    ...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: LiDAR Sensors
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.