Digital Music

12/25/2009

Class and Object in C++

A. A glimpse of the class

Class is made to present a particular object that will assist in the process of solving complex problems. If we observe, the definition of a class that would be the creation of new data types. As an example we can make the types associated with an object such as cars, houses, employees and others.

B. What is the class?

Class is a form of simplification of the problems associated with the object. Classes can also be defined as something that has the data (properties) and funsi (behavior). Abstract class, therefore we must do Instantiation of the class, then instance (or realization of real-life examples) of these classes are also often called the object, As with humans is an example of the class instance or object of man is Irwan, Yana, Eric and others.

C. How do I create a class?

In C + +, the class created using the keyword class. The general form of manufacture, namely:

Class nama_kelas (

Access_specifier1:

Data_members;

Member_functions;

... ... ...

Access_specifier2:

Data_members;

Member_functions;

... ... ...

... ... ...

);

To make the implementation-fungsiyang functions found on a class, we use the operator::. The general form of manufacture, namely:

Tipe_data nama_kelas:: nama_fungsi (daftar_parameter) (

Statemen_yang_akan_dilakukan;

... ... ..

)

Sedangka to access data or funsi contained in these classes, we use a dot. The general form of manufacture, namely:

Nama_instance.data

Or

Nama_instance.nama_fungsi (daftar_parameter)

D. CONTRUCTOR and destructor

1. Constructor Definition

Constructor is a function that automatically called every time do Instantiation of a class. Constructor used to initialize the value of the data contained in the relevant class.

2. Destructor Definition

Destructor is a function which is the opposite of the constructor, which is useful to destroy or dispose of an object (class) of memory. The name of the function is the same as destruktor class name or the name of the constructor, only the tilde sign in front added ('~').

In C + +, there are three access levels are:

1. Private

This access level is useful to provide data access rights only to the respective class only. This means derivative classes or external environment within the program are not allowed to access the data. Here is a picture of defining a class that uses the private access level.

Class EXAMPLE (

Int x; / / By default, the private nature Xakan

Public:

Int Y; / / Y will be public

Private:

Int Z; / / Z will be private
);

1. Public

This access level serves to provide general access rights (public) to the derived classes as well as the external environment within the program. This section usually contains the functions that will serve as a link (interface)

1. Protected

This access level is used to grant access to data in a class so that data can be accessed by derived classes, but the external environment within the program still does not give rights to the mengakasesnya

E. Members of the class is static

In the C + +, the members of a class (data and functions) can be used as static, ie by increasing the static keyword in front of pendeklarasiannya.

1. Static Data

Please note that when we declare a static data, we do not define it. This means that the previous data is not stored in the memory. For that we need to provide a global definition for the data to any location provided that are outside the relevant class.

1. Static Function

Just as data or variables from a class, that functions contained in a class can also dijadiakan static functions. Same way, namely by adding the keyword static in front of him. But in doing this we need to consider all the following things:

a. Static functions contained in the class does not have this pointer

b. Static functions can not be virtual

c. There should not be two (or more) the same function, although that one is static and the other non-static.

F. Pointer to the object

Pointer to the object will also access data or functions in the classroom using the operator ->, and not use the service period (.) As in the instance which is not a pointer.

This pointer

This pointer is a pointer that is passed secra automatically every time the object was made in memory and will represent the corresponding class name.

G. Objects As Parameters in a Function

In C + +, objects can also serve as a parameter in the definition of a function. This object will be missed by the standard method is by using pass by value (based on value delivered) which aims to access the data (which is public) in a class diman processing functions performed in the outside (not a member function).

H. Returned Value As Object in a Function

In addition to being a parameter, an object can also be used as the value change of a function. In the function will create a temporary object which will then be released immediately after the return value.

0 komentar:

Post a Comment