Dynamic Memory

Chapter 16 - Object-oriented Programming Foundations

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

A First Look at the C++ Class
It has already been stated that the C++ class type is an extension of C’s struct type. In this section, you learn how you can use the struct type in C++ to form a primitive class, complete with data and members. Next, you examine the formal syntax for defining a class and see several simple examples of its implementation. The section discusses the differences between a primitive struct class type and an actual C++ class and presents several simple examples to illustrate class concepts. (Chapter 17 is devoted to a detailed analysis of the C++ class as it applies to object-oriented programming.)
A Structure as a Primitive Class
Chapter 13 discussed structures for C and C++. In many respects, the structure in C++ is an elementary form of a class. You use the keyword struct to define a structure. Examine the following code:
//
//  sqroot.cpp
//  C++ program using the keyword “struct” to illustrate a
//  primitive form of class. Here several member functions
//  are defined within the structure.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
#include <math.h>

struct math_operations {
 double data_value;
 void set_value(double ang) {data_value=ang;}
 double get_square(void) {double answer;
                          answer=data_value*data_value;
                          return (answer);}
 double get_square_root(void) {double answer;
                               answer=sqrt(data_value);
                               return (answer);}
} math;

main( )
{
 // set numeric value to 35.63
 math.set_value(35.63);

 cout << “The square of the number is: ”
      << math.get_square( ) << endl;
 cout << “The square root of the number is: ”
      << math.get_square_root( ) << endl;
 return (0);
}
The first thing to notice in this code is that the structure definition contains member data and functions. While you are used to seeing data declarations as part of a structure, this is probably the first time you have seen member functions defined within the structure definition. There was no mention of member functions in the discussion of the struct type in Chapter 13 because they are exclusive to C++. These member functions can act upon the data contained in the structure (or class) itself.
Recall that a class can contain member data and functions. By default, in a struct declaration in C++, member data and functions are public. (A public section is one in which the data and functions are available outside the structure.) Here is the output sent to the screen when the program is executed:
The square of the number is: 1269.5
The square root of the number is: 5.96909
In this example, the structure definition contains a single data value:
double data_value;
Next, three member functions are defined. Actually, the code for each function is contained within the structure:
void set_value(double ang) {data_value=ang;}
double get_square(void) {double answer;
                        answer=data_value
*data_value;
                        return (answer);}
double get_square_root(void) {double answer;
                             answer=sqrt(data_value);
                             return (answer);}
The first member function is responsible for initializing the variable data_value. The remaining two member functions return the square and square root of data_value. Notice that the member functions are not passed a value; data_value is available to them as members of the structure. Both member functions return a double.
The program’s main( ) function sets the value of data_value to 35.63 with a call to the member function, set_value( ):
math.set_value(35.63);
Notice that the name math has been associated with the structure math_operations.
The remaining two member functions return values to the cout stream:
cout << “The square of the number is: ”
    << math.get_square( ) << endl;
cout << “The square root of the number is: ”
    << math.get_square_root( ) << endl;
This example contains a structure with member data and functions. The functions are contained within the structure definition. You won’t find an example simpler than this one.
In the next program, the struct keyword is still used to develop a primitive class, but this time the member functions are written outside the structure. This is the way you will most commonly see structures and classes defined.
This example contains a structure definition with one data member, data_value, and seven member functions. The member functions return information for various trigonometric values.
//
//  16TSTRUC.CPP
//  C++ program using the keyword “struct” to illustrate a
//  primitive form of class. This program uses a structure
//  to obtain trigonometric values for an angle.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
#include <math.h>

const double DEG_TO_RAD=0.0174532925;
struct degree {
 double data_value;

 void set_value(double);
 double get_sine(void);
 double get_cosine(void);
 double get_tangent(void);
 double get_secant(void);
 double get_cosecant(void);
 double get_cotangent(void);
} deg;

void degree::set_value(double ang)
{
 data_value=ang;
}

double degree::get_sine(void)
{
 double answer;
  
 answer=sin(DEG_TO_RAD*data_value);
 return (answer);
}

double degree::get_cosine(void)
{
 double answer;
 answer=cos(DEG_TO_RAD*data_value);
 return (answer);   
}

double degree::get_tangent(void)
{
 double answer;
  
 answer=tan(DEG_TO_RAD*data_value);
 return (answer);
}

double degree::get_secant(void)
{
 double answer;
  
 answer=1.0/sin(DEG_TO_RAD*data_value);
 return (answer);
}

double degree::get_cosecant(void)
{
 double answer;

 answer=1.0/cos(DEG_TO_RAD*data_value);
 return (answer);
}
double degree::get_cotangent(void)
{
 double answer;
  
 answer=1.0/tan(DEG_TO_RAD*data_value);
 return (answer);
}

main( )
{
 // set angle to 25.0 degrees
 deg.set_value(25.0);

 cout << “The sine of the angle is: ”
      << deg.get_sine( ) << endl;
 cout << “The cosine of the angle is: ”
      << deg.get_cosine( ) << endl;
 cout << “The tangent of the angle is: ”
      << deg.get_tangent( ) << endl;
 cout << “The secant of the angle is: ”
      << deg.get_secant( ) << endl;
 cout << “The cosecant of the angle is: ”
      << deg.get_cosecant( ) << endl;
 cout << “The cotangent of the angle is: ”
      << deg.get_cotangent( ) << endl;
 return (0);
}
Notice that the structure definition contains the prototypes for the member functions. The variable, deg, is associated with the degree structure type.
struct degree {
 double data_value;

 void set_value(double);
 double get_sine(void);
 double get_cosine(void);
 double get_tangent(void);
 double get_secant(void);
 double get_cosecant(void);
 double get_cotangent(void);
} deg;
Immediately after the structure is defined, the various member functions are developed and listed. The member functions are associated with the structure or class by means of the scope operator (::). Other than the use of the scope operator, the member functions take on the appearance of normal functions.
Examine the first part of the main( ) function:
// set angle to 25.0 degrees
deg.set_data(25.0);
Here, the value 25.0 is being passed as an argument to the set_value( ) function. Observe the syntax for this operation. The set_value( ) function itself is very simple:
void degree::set_value(double ang)
{
 data_value=ang;
}
The function accepts the argument and assigns the value to the class variable, data_value. This is one way of initializing class variables. From this point forward in the class, data_value is accessible by each of the six member functions. The job of the member functions is to calculate the sine, cosine, tangent, secant, cosecant, and cotangent of the given angle. The respective values are printed to the screen from the main( ) function with statements similar to the following:
cout << “The sine of the angle is: ”
    << deg.get_sine( ) << endl;
Use the dot notation, commonly used for structures, to access the member functions. Pointer variables can also be assigned to a structure or class, in which case the arrow operator is used. You will see examples of this in Chapter 17.
The Syntax and Rules for C++ Classes
The definition of a C++ class begins with the keyword class. The class name (tag type) immediately follows the keyword. The framework of the class is very similar to the struct type definition you have already seen.
class type {
 type var1
 type var2
 type var3
     .
     .
     .
public:
 member function 1
 member function 2
 member function 3
 member function 4
     .
     .
     .
} name associated with class type;
Member variables immediately follow the class declaration. These variables are, by default, private to the class and can be accessed only by the member functions that follow. Member functions typically follow a public declaration. This allows access to the member functions from calling routines external to the class. All class member functions have access to public, private, and protected parts of a class.
The following is an example of a class that is used in the next programming example:
class degree {
 double data_value;

public:
 void set_value(double);
 double get_sine(void);
 double get_cosine(void);
 double get_tangent(void);
 double get_secant(void);
 double get_cosecant(void);
 double get_cotangent(void);
} deg;
This class has a type (tag name) degree. A private variable, data_value, will share degree values among the various member functions. Seven functions make up the function members of the class. They are set_value( ), get_sine( ), get_cosine( ), get_tangent( ), get_secant( ), get_cosecant( ), and get_cotangent( ). The name that is associated with this class type is deg. Unlike this example, the association of a variable name with the class name is most frequently made in the main( ) function.
Does this class definition look familiar? It is basically the structure definition from the previous example converted to a true class.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net