Minggu, 14 Juni 2009

Sebuah kelas di C + + encapsulation adalah data anggota dan fungsi yang memanipulasi data. The class can also have some other important members which are architecturally important. Kelas juga dapat mempunyai beberapa anggota yang penting lainnya adalah arsitektur penting.

This C++ Tutorial discusses the components of a C++ class. Ini C + + Tutorial membahas komponen dari kelas C + +. More C++ tutorials will follow. More C + + tutorial akan mengikuti.

C++ Tutorial - Class Data Members: C + + Tutorial - Kelas Data Anggota:

Very important point about the Data members in this C++ Tutorial! Sangat penting tentang Data anggota di C + + Tutorial! This title is not a keyword or a data type in C++. Judul ini bukan merupakan kata kunci atau tipe data dalam C + +. This is just to explain one of the logical classifications of the types of members that are available in C++. Ini hanya untuk menjelaskan salah satu logis klasifikasi jenis anggota yang tersedia di C + +.

The data members can be of any legal data type , a class type, a struct type etc., They can also be declared as pointers and accessible normally as like other data members. Data anggota dapat hukum dari setiap jenis data, jenis satu kelas, satu jenis struct dll, Mereka juga dapat dinyatakan sebagai pointer dan dapat diakses seperti biasanya data lainnya sebagai anggota. The Example class given below in this C++ tutorial has two data members x and y of type integer. Contoh kelas yang diberikan di bawah ini di C + + Tutorial ini memiliki dua anggota data x dan y of type integer.

C++ Tutorial - Function members in classes: C + + Tutorial - Fungsi anggota di kelas:

Functions declared inside a class can be any of the following four types. Fungsi dideklarasikan di dalam kelas dapat menjadi salah satu dari empat jenis berikut. This C++ Tutorial explains each one of them as below. Ini C + + Tutorial menjelaskan setiap satu di antaranya sebagai berikut.

Ordinary member functions : Fungsi anggota biasa:

These are ordinary functions defined with a return type and parameters. Ini adalah fungsi biasa ditetapkan dengan kembali jenis dan parameter. The return type can also be void. Laba jenis juga dapat void. The special trait about member functions is they can access the private/protected data members of their class and manipulate them. Ciri khusus tentang fungsi anggota adalah mereka dapat mengakses swasta / dilindungi data anggota kelas mereka dan memanipulasi mereka. No external functions can access the private/protected data members of a class. Eksternal tidak dapat mengakses fungsi-fungsi swasta / dilindungi data anggota dari kelas. The sample below this C++ Tutorial uses an ordinary member function Add(), returning an integer value. Contoh di bawah ini C + + Tutorial menggunakan fungsi anggota biasa Tambah (), kembali sebuah nilai integer.

Constructors: Constructors:

Constructors in C++ are special member functions of a class. Constructors di C + + adalah fungsi khusus anggota satu kelas. They have the same name as the Class Name. Mereka memiliki nama yang sama dengan Nama Kelas. There can be any number of overloaded constructors inside a class, provided they have a different set of parameters. Tidak boleh ada jumlah keberatan constructors di dalam kelas, asalkan mereka yang berbeda set parameter. There are some important qualities for a constructor to be noted. Ada beberapa kualitas penting untuk menjadi pembina dicatat.
  • Constructors have the same name as the class. Constructors memiliki nama yang sama seperti kelas.
  • Constructors do not return any values Constructors tidak kembali setiap nilai
  • Constructors are invoked first when a class is initialized. Constructors are invoked pertama bila kelas diinisialisasi. Any initializations for the class members, memory allocations are done at the constructor. Apapun initializations untuk anggota kelas, alokasi memori yang dilakukan pada pembina.
In the example class given below in this C++ tutorial has the constructor Example_Class(), with the same name as the class. Pada contoh di bawah ini diberikan dalam kelas ini tutorial C + + memiliki pembina Example_Class (), dengan nama yang sama seperti kelas.

Destructors: Destructors:

Destructors in C++ also have the same name, except for the fact that they are preceded by a '~' operator. Destructors di C + + juga memiliki nama yang sama, kecuali kenyataan bahwa mereka diawali dengan '~' operator. The destructors are called when the object of a class goes out of scope. It is not necessary to declare a constructor or a destructor inside a class. Destructors yang dipanggil ketika objek dari satu kelas pergi dari cakupan. Anda tidak perlu menyatakan sebuah pembangun atau membakar sampah di dalam kelas. If not declared, the compiler will automatically create a default one for each. If the constructor/destructor is declared as private, then the class cannot be instantiated. Check below for the sample class of the C++ tutorial for an example of destructor. Jika tidak dinyatakan, compiler secara otomatis akan membuat satu standar untuk masing-masing. Jika pembina / membakar sampah dinyatakan sebagai pribadi, maka tidak dapat kelas instantiated. Periksa contoh di bawah ini untuk kelas C + + tutorial contoh untuk membakar sampah.

C++ Tutorial - Access Level: C + + Tutorial - Level Akses:

The classes in C++ have 3 important access levels. Kelas di C + + ada 3 tingkat akses penting. They are Private, Public and Protected . Mereka Pribadi, umum dan dilindungi. The explanations are as follows. Dengan penjelasan sebagai berikut.

Private: Swasta:

The members are accessible only by the member functions or friend functions. Para anggota hanya dapat diakses oleh anggota fungsi fungsi atau teman.

Protected: Dilindungi:

These members are accessible by the member functions of the class and the classes which are derived from this class. Para anggota dapat diakses oleh fungsi anggota kelas dan kelas-kelas yang berasal dari kelas ini.

Public: Publik:

Accessible by any external member. Look at the sample class below. Eksternal dapat diakses oleh semua anggota. Lihatlah contoh di kelas di bawah ini.

C++ Tutorial - Example of a class: C + + Tutorial - Contoh kelas:

class Example_class //Sample Class for the C++ Tutorial kelas Example_class / / Contoh Kelas untuk C + + Tutorial
{ (
private: swasta:
int x; //Data member int x; / / data anggota
int y; // Data member int y; / / data anggota
public: publik:
Example_Class() //Constructor for the C++ tutorial Example_Class () / / Pembuat untuk C + + tutorial
{ (
x = 0; x = 0;
y = 0; y = 0;
} )
~Example_Class() //destructor for the C++ Tutorial ~ Example_Class () / / untuk membakar sampah C + + Tutorial
{ } ()
int Add() Tambahkan int ()
{ (
return x+y; return x + y;
} )
}; );

Tidak ada komentar:

Posting Komentar