zaterdag 30 juni 2007

objects and classes I

The following are the most OOP (Object Oriented Programmin) features:

- Abstraction
- Encapsulation and data hiding
- Inheritance
- Reusability of code

I will now summarize what I read about classes and I should remember.

The class is the single most important C++ enhancement for implementing these features and tying them together!

Procedural and Object-Oriented Programming
In short, with a procedural approach, you first concentrate on the procedures you will follow and then think about how to represent the data.

In short, with an OOP approach, you concentrate on the object as the user perceives it, thinking about the data you need to describe the object and the operations that will describe the user's interaction with the data. After you develop a description of that interface, you move on to decide how to implement the interface and data storage. Finally, you put together a program to use your new design.

Abstraction and Classes
In computing, abstraction is the crucial step of representing information in terms of its interface with the user. That is, you abstract the essential operational features of a problem and express a solution in those terms.
In the softball example (sorry, not showen here), the interface describes how the user initializez, updates and display the data. From abstraction, it is a short step to the user-defined type, which in C++ is a class design that implements the abstract interface.

What is a type?
At first you tend to think of a datatype in terms of its appearance--how it is stored in memory.
A char, for example is 1 byte of memory, and a double is often 8 bytes of memory.
But a little reflection leads you to conclude that a data type is also defined in terms of operations that can be performed on it.
For example, the int type can use all the arithmetic operations. You can add, subtract, multiply, and divide integers. You can also use the modulus operator (%) with them.

On the other hand, consider pointers. A pointer might very well require the same amount of memory as an int. It might even be represented internally as an integer. But a pointer doesn't
allow the same operations that an integer does. You can't for example, multiply two pointers by eachother.
Thus when you declare a variable as an int or as a pointer-to-float, you're not just allocating memory--you are also establishing which operations can be performed with the variable.

In short, specifying a basic type does three things:

- It determines hwo much memory is needed for a data object.
- It determines how the bits in memory are interpreted. (A long and a float might use the same number of bits in memory, but they are translated into numeric values differently.)
- It determines what operations, or methods, can be performed using the data object.

For built-in types, the information about operations is built in to the compiler. But when you define a user-defined type in C++, you have to provide the same kind of information yourself.
In exchange for this extra work, you gain the power and flexibility to custom fit new data types to match real-world requirements.

Classes in C++
A class is a C++ vehicle for stranslating an abstraction to a user-defined-type. It combines data representation and methods for manipulating that data into one neat package.

To define a class, Generally, a class specification has two parts:

- A class declaration, which describes the data component, in terms of data members, and the public interface, in terms of member functions, termed methods
- The class method definitions, which describe how certain class member functions are implemented

Roughly speaking, the class declaration provides a class overview, whereas the method definitions supply the details.

What is an Interface?
For classes, we speak of the public interface. In this case, the public is the program using the class, the interacting system consists of the class objects, and the interface consists of the methods provided by whoever wrote the class. The interface enables you, the programmer, to write code that interacts with class objects, and thus it enables the program to use the class objects. For example, to find the number of characters in a string object, you don't open up the object to what is inside; you just use the size() method provided by the class creators. It turns out that the class design denies direct access to the public user. But the public is allowed to use the size() method. The size() method, then, is part of the public interface between the user and a string class object.
Similarly, the getline() method is part of the istream class public interface.

If you want a more personal relationship, instead of thinking of the program using a class as the public user, you can think of the person writing the program using the class as the public user. But in any case, to use a class, you need to know its public interface; to write a class, you need to create its public interface.

The keywords private public describe access control for class members.
Any program that uses an object of a particular class can access the public portions directly. A program can access the private members of an object only by using the public member functions. Thus, the public member functions act as go-betweens between a program and an object's private members; they provide the interface between object and program.

data hiding, this isulation of data from direct access by a program is called data hiding.

A class design attempts to separate the public interface from the specifics of the implementation. The public interface represents the abstraction component of the design.
Gathering the implementation details together and separating them from the abstraction is called encapsulation. Data hiding (putting data into the private section of a class) is an instance of encapsulation, and so hiding functional details of an implementation in the private section.
Another example of encapsulation is the usual practice of placing class function definitions in a seperate file from the class declaration.

Note that data hiding not only prevents you form accessing data directly, but it also absolves you (in the roll as user of the class) from needing to know how the data is represented.
For example a class show() member can display the total value of a holding, this value can be stored as part of an object, or it can be calculated when needed.
From the standpoint of the user, it makes no difference which approach is used.
What you need to know is what the different member functions accomplish; that is, you need to know what kinds of arguments a member function takes and what kind of return value it has.
The principle is to separate the details of the implementation from the design of the interface.
If you later find a better way to implement the data representation or the details of the member functions, you can change those details without changing the program interface, and that makes programs much easier to maintain.

Implementing Class Member Functions
You still have to create the second part of the class specification: providing code for those member functions represented by a prototype in the class declaration. Member function definitions are much like regular functions. Each has a function header and a function body. Member function definitions can have return types and arguments. But they also have two special characteristics:

- When you define a member function, you use the scope-resolution operator (::) to identify the class to which the function belongs
- Class methods can access the private components of the class

First, the function header for a member function uses the scope-resolution operator (::) to indicate to which class the function belongs.

See this example:

void Stock::update(double price)

This notation means you are defining the update() function that is member of the Stock class.

Thus, the scope-resolution operator resolves the identity of the class to which a method definition applies. We say that the identifier update() has class scope. Other member functions of the Stock class can, if necessary, use the update() method without using the scope-resolution operator. That is because they belong to the same class, making update() in scope.

The second special characteristic of methods is that a method can access the private members of a class. For example, the show() method can use code like this:

cout << "Company: " << company
<< " Shares: " << shares << endl;
<< " Share Price: $" << share_val
<< " Total Worth: $" << total_val << endl;

Where all those variables are in the private section of the class.

Here company, shares, and so on are private data members of the Stock class. If you try to use a nonmember function to access these data members, the compiler stops you cold in your tracks. (However, friend functions, later discussed provide an exception).

Which Object Does a Method Use?
Now we come to one of the most important aspects of using objects: how you apply a class method to an object. Code such as
shares += num;
uses the shares member of an object. But which object? That is an excellent question! To answer it, first consider how you create an object. The simplest way is to declare class variables:

Stock kate, joe;

This creates two objects of the Stock class, one named kate and one named joe.

Next, consider how to use a member function with one of these objects. The answer is, as with structures and structure members, is to use the membership operator:

kate.show(); // the kate object calls the member function
joe.show(); // the joe object calls the member function

The first call here invokes show() as a member of the kate object. This means the method interprets shares as kate.shares and share_val as kate.share_val. Similarly, the call joe.show() method interprets shares and share_val as joe.shares and joe.share_val respectively.

Each new object you create contains storage for its own internal variables, the class members. But all objects of the same class share the same set of class methods, with just one copy of each method.
Suppose, for example, that kate and joe are Stock objects. In that case, kate.shares occupies one chunk of memory and joe.shares occupies a second chunk of memory. But kate.show() and joe.show() both invoke the same method--that is, both execute the same block of code. They just apply the code to different data. Calling a member function is what some OOP languages term sending a message. Thus, sending the same message to two different objects invoke the same method but applies it to two different objects.

When the Stock class works as you want it to, you can use it as a user-defined type in other programs. The critical point in using the new type is to understand what member functions do;
You shouldn't have to think about the implementation details.

The Client/Server Model
OOP programmers often discuss program design in terms of a client/server model. In this conceptualization, the client is the program that uses the class. The class declaration, including the class methods, constitude the server, which is a resource that is available to the programs that need it. The client uses the server through the publicly defined interface only. This means that the client's only responsibility, and, by extension, the client's programmer's only responsibility, is to knwo that interface. The server's responsibility, and by extension, the server's designer's responsibility, is to see that the server reliably and accurately performs according to that interface. Any changes the server designer makes to the class design should be to details of the implementation, not to the interface. This allows programmers to improve the client and the server independently of each other, without changes in the server having unforseen repercussions on the client's behavior.

Full Qualified Name

char * Bozo::Retort()

The full, or qualified name of the above funtion is: Bozo::Retord(). The name Retort(), on the other hand, is an abbreviation of the qualified name, and it can be used only in certain circumstances, such as in the code for the class methods.

To create an object, which is a particular example of a class, you use the class name as if it where a type name:

Bozo bozetta;

This works because a class is a user-defined type.

You invoke a class member function, or method, by using a class object. You do so by using the dot membership operator:


This invokes the Retort() member function, and whenever the code for that function refers to a particular data member, the function uses the value that member has in the bozetta object.

Geen opmerkingen: