ISM 3230 – C Programming



Creating and Using Objects

A class is a reusable block of code that pertains to data, and operations to be performed on that data, about a particular object (such as a vehicle, an account, a supplier, a product, etc.) A class may (and generally does) have:

1. data members to store the data values for a specific instance of the class (e.g., a particular account)

2. function members that contain the statements to be executed on that data

A class is just the code that will be activated IF an instance of that class is actually created, in the same way that a data type (such as an integer) is just the definition of how a value will be stored and treated IF an instance of that data type is declared.

Creating a class

There are two parts to creating a class: a class declaration and a class definition.

• The declaration shows what the components of the class are, including data members and function members of the class. “Data members of a class” are the variables used to hold the class’s data or properties. “Function members of a class” are the functions that act on the class’s data members. The declaration is the interface to the class. It lets a programmer, and the compiler, know what members are available within the class, and how they should be used.

• The definition is the implementation of the function members. That is, the statements that comprise the functions.

Class members may be ‘public’ or ‘private’.

• ‘private’ means usable only within the class itself

• ‘public’ means usable to functions outside the class

Whether a data member or function member is public or private is stated within the class declaration. You may have all public members, all private members, or some of both. This will be discussed further.

Class definitions are written using the same techniques used before to write functions.

The syntax for a class declaration is:

class classname

{

public:

list of public data members (variables used to hold the class’s data)

list of public function members (prototypes of functions used to act on the class’s data)

private:

list of private data members (variables used to hold the class’s data)

list of private function members (prototypes of functions used to act on the class’s data)

};

The order of the public and private sections may be reversed if desired, and the order of the data members and function members may be reversed if desired. Different programmers have different ideas of which should come first; the compiler doesn’t care.

Placement of class declarations and definitions

There are several possible locations for class declarations and definitions. The simplest is to place them both in the same file as a program code. This has the advantage of only having one file to deal with, but has the disadvantage of not allowing the class to be re-used for another program. Most classes are written in their own separate files. The following discussion will start with placement in a program file, and then extend to placement in a separate class file.

Declaring and defining a class within a program file

The following example declares a class called Order within the program application file. The class must be declared before it is used, so the declaration is placed before main(). This example has no function members, so there is no definition, only a declaration. All data members are public, meaning they can be accessed by other functions outside the class. An instance of the class is created in main() in very much the same way as a variable would be created. The data members are accessed using the dot operator, which is a concatenation of the instance name, a dot, and the class data member name.

Example 1:

#include

#include

using namespace std;

//declare a class

class Order

{

public:

int numBooks;

double bookCost;

string orderID;

};

int main()

{

Order x; //create an instance of the class Order

//get some values

cout > x.orderID;

cout > x.numBooks;

cout > x.bookCost;

//display a calculated value on screen

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download