Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selamat Idul Fitri 1430 H Taqabalallahu minna waminkum Taqabbal ya karim… Mohon maaf lahir dan batin.

Similar presentations


Presentation on theme: "Selamat Idul Fitri 1430 H Taqabalallahu minna waminkum Taqabbal ya karim… Mohon maaf lahir dan batin."— Presentation transcript:

1 Selamat Idul Fitri 1430 H Taqabalallahu minna waminkum Taqabbal ya karim… Mohon maaf lahir dan batin

2 Constructors and Destructors Constructors Parameterized Constructors Multiple Constructors Constructors with Default Arguments Copy Constructors Destructors

3 Background Tidak bisa dilakukan inisialisasi secara langsung di data member sebuah class dengan suatu nilai/ value. Misal : int kode = 1; Inisialisasi data member harus melalui sebuah fungsi anggota (member function) class yang bersangkutan. C++ menyediakan fungsi khusus untuk melakukan tugas ini  constructor.

4 CONSTRUCTOR Is a special member function. Its name is the same as the class name Invoke when the object of its associated class is created Enables an object to initialize itself when it is created  Known as automatic initialization Should be declared in the public section Do not have return types/ return value Can have default arguments

5 Simple Constructor class coba { int kode; public : coba() {kode = 1; }; void show() {cout << “Kode = “ << kode << endl; }; Void main() { coba cobaku; cobaku.show(); }

6 Parameterized Constructor There are arguments/ parameters in the constructor header function. Use the arguments’s value to initialise data members. Example : class coba { public : coba (int a, int b) { … }; }

7 //parameterized constructor #include "iostream.h" class integer {int m, n; public:integer ( int x, int y); void display ( void ) { cout << "m = "<< m <<" n = "<< n <<"\n"; } }; integer :: integer ( int x, int y = 0) //default argument {m = x;n = y; }; main() {integer int1 ( 10, 100 );//implicit call integer int2 = integer ( 50 );//explicit call int1.display(); int2.display(); }

8 Multiple Constructors There are many constructors in a single class. These show the all variation/ alternatives way to initialise data members in a class. Example : class coba { … public : coba() {…}; coba(int a) {…}; coba(int a, int b) {…}; }

9 // multiple constructors #include "iostream.h" class integer {int m, n; public: integer() { m=0; n=0; };//default arguments integer( int x, int y )//parameterized {m=x; n=y; }; integer(integer &i)//copy constructors {m=i.m; n=i.n; }; void display(void) {cout<<"m = "<

10 Copy Constructor Sometimes we need to copy all the data members from one existing object to another new object directly. Its better than write/ copy them one by one. C++ facilitate that method, we call that technique as copy constructor. Using object as constructor’s parameter.

11 #include "iostream.h" class code {int id; public: code() { }; code( int x) { id=x; }; //code(code &c)//copy constructors { id = c.id; }; void display(void) { cout<<“id = "<

12 DESTRUCTORS Its name is the same with the class name but preceded by a tilde (~). Destroys the Objects when they are no longer required. Its release memory space for future use. Never takes any argument nor return any value. Invoke upon exit program/ block/ function.

13 #include "iostream.h" int count = 0; class alpha {public: alpha() {count++; cout << "\nno. of object created" << count; } ~alpha()// destructor defined {cout << "\nno. of object destroyed" << count; count--; } }; void main() {cout << "\nEnter MAIN\n\n"; alpha a1, a2, a3, a4; { cout << "\nEnter Blok 1\n"; alpha a5; }// call destructor a5 { cout << "\nEnter Blok 2\n"; alpha a6; }// call destructor a6 }// call destructor a1, a2, a3, a4

14 Wise Words Today… Kasih Ibu Sepanjang Jalan Kasih Anak Sepenggalan…


Download ppt "Selamat Idul Fitri 1430 H Taqabalallahu minna waminkum Taqabbal ya karim… Mohon maaf lahir dan batin."

Similar presentations


Ads by Google