Lecture 7: Programmer Defined Data Types II: Classes and the UML Type of Design


Class Definition

The foundation of modern programming is based upon the concept of a class. Classes are the basis of the Windows operating systems and most programs written today. Classes are the major building blocks in the computer languages C++, Java and the new language C#.

Classes are not a variable into which data can be stored but a pattern of the way data can be stored as well as a description of how that data may be accessed. Classes are a programmer defined data type. These patterns are used to define data variables of the class. These variables are called instances or objects.

The class definition contains declarations of any data that may be stored in the objects. This data is called data members or attributes (and sometimes fields). Today the term attributes is more frequently used.

The definition may also contain the declarations or definitions of functions that describe the way the objects may be manipulated. These functions are called member functions, operations or methods. Today the name method is used more frequently.

Traditional structured programming design techniques do not adequately describe programming based on classes and objects. Instead a new type of design has been created called Object Oriented Design or OOD to better describe the design of the solutions to today's problems. This type of programming is called Object Oriented Programming or OOP. Several different tools have been created to support this approach to programming. One of these tools is called the Unified Modeling Language or UML. A brief discussion of UML will be considered after an introduction to classes in C++.

Informally in C++ the class definition includes the following:

  • the keyword class

  • a tag or name of the class

  • a pair of beginning and ending braces { and } the last of which is followed by a semicolon

  • a collection of zero or more attribute declarations

  • a collection of zero or more definitions or declarations of methods.

  • the declarations or definitions of these members are organized into one or more access sections where each section is headed by the access section specifier.

The access section specifiers are the keywords:

  • private

  • protected

  • public

Variables that are of a class data type are called instances or objects of the class. Objects may be defined similar to how variables of other data types are defined as the following:

image from book

 ClassName objectName; 

image from book

While the above is one way to define an object, there may be additional accepted definitions like the following:

image from book

 ClassName objectName(a1,a2, ... an); 

image from book

where a1, a2 up to an are arguments that may pass values into the attributes of the object objectName which is being defined in this statement.

Each object contains a copy of each non-static attribute. (Static attributes are discussed below.) The size of each object is greater than or equal to the sum of the sizes of all of its attributes.

An object does not contain a method. It shares the methods with all of the other objects of the class.

Objects get access to the members by using the access member operator (also called the dot operator). That is, if objectName is an object of some class and member1 is some member of that same class, then objectName (under certain circumstances) can get access to member1 through the access member operator as in the following statement:

image from book

 objectName.member1 

image from book

In this example, member1 may be either an attribute or a method.

While the above statement shows how an object may access a member, where an object of a class may access a member depends on which access section the member is defined or declared in and where the object is located in the code when it attempts to access the member.

The construct for a class definition is the following:

image from book

 class className {   private:      memberDataType dataMemberlist;      memberFunctionlist;   protected:     memberDataType dataMemberlist;     memberFunctionlist;   public:     memberDataType dataMemberlist;     memberFunctionlist; }; 

image from book

The private access section contains those class members that:

  • are either defined or declared in a private access section

  • are accessible only by a member of the class (or a friend function) or an object within the class.

  • are similar to local variables of a function in that they are only visible within the class

  • are not accessible anywhere else other than within the class, not even by the instance/object of the class in which they are contained not even using the access operator (i.e. the dot operator}.

The protected access section will contain those class members that:

  • are either defined or declared in a protected access section

  • have the same accessibility and properties as those members defined in the private access section except as they will relate to class members of derived classes (as will be discussed later).

The public access section will contain those class members that:

  • are either declared or defined in a public access section

  • have the same accessibility anywhere that an object is defined by using the access operator (the dot operator) For example:

    image from book

     theObject.publicClassMember 

    image from book

There can be as many of any one access section as the programmer desires. The recommendation is to have only one access section of each type needed. The access sections may be listed in any order that the programmer would like. In these notes the first section listed will be private and then protected and that will be followed by public (if and only if these sections exist for the class.) Some programmers and therefore some book authors list the private access section last.

The access specifiers are not required. If no access specifier has been indicated, the compiler selects a default access specifier. The default for classes is private. By this is meant that when no access specifier precedes a member declaration or definition within the class definition, then the access specifier for the member defaults to private.

Structures as well as classes may be defined with or without access specifiers sections. The default access specifier for structures is public. By this is meant that when no access specifier precedes a member declaration or definition within the structure's definition, then the access specifier for that member defaults to public.

What is wrong with the code below?

image from book

 class Date {    int theMonth,        theDay,        theYear; }; Date invoiceDate; invoiceDate.theMonth = 2; invoiceDate.theDay = 20; invoiceDate.theYear = 95; cout << "The month is " << invoiceDate.theMonth; 

image from book

Since no access specifier was listed in the class: Date, it has only a private access specifier by default. Therefore the attributes theMonth, theDay, and theYear are in the private access section. As a result, the object invoiceDate can not access them even using the dot operator outside of the class construct.

To better understand these concepts see account.cpp and dateclas.cpp. Copy each of these programs to your compiler, compile and run them. Notice how these programs are different from procedural programs that were encountered in the previous lectures. This difference will require the addition of UML charts during the design phase and possible a reduction in the number of modules needed in the structure chart. In fact in some cases there may be no need for a structure chart.

In the program account.cpp the definition of the class theAccount and the definitions of its methods are all listed in the program. However as with a previous discussion about structures, the class definition should be placed into a header file and the class methods should be placed into their own source file. For example account2.cpp is the same program except the class Account and its methods are placed in their respective files: theAccount.h and theAccount_h.cpp respectively. Copy these files to an empty console project account2. Next compile and execute the program. Notice that it runs the same as the program account.cpp above.

 Note:  While creating a separate header file for the class and a separate source file for the class' methods is what you should do in your work, most of the following lecture examples will instead have these definitions included in the program. Doing otherwise would make the notes more complex and would make it more difficult for you to copy and to compile these examples.

From the examples above you can see that the user of the program does not know what is happening inside of any object of the class Date. The inter workings of a class object are hidden from view especially for those members of the object in the private access section. This property is referred to as encapsulation.

Frequently, the data members of a class are declared in the private access section and the member functions are defined or declared in the public access section. However placing the definition or the declaration of a method in the public access section is not required as the example: prvtfnct.cpp demonstrates. As you construct your classes, it is recommended that any method which does not need to be accessed outside of the class should be defined/declared in the private access section as in this example.

Comment: In general an attribute has no existence independent of the object that contains it. (In another section static data members will be discussed. The statements in the discussion above refer to non static data members. The static data members are defined by the class' definition and are not a part of any objects. Therefore non static data members must be initialized via its object.

A non static data member is declared within the class definition and is defined when its object is defined. The memory associated with these data members is part of the object's memory. On the other hand, the member functions are not contained within the class objects. They are independent of the object and may be defined within the class definition. The memory associated with these member functions is entirely separate from the memory of an object.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

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