Selamat Idul Fitri 1430H Taqabalallahu minna waminkum Taqabbal ya karim Mohon maaf lahir dan batin, Constructors and Destructors.

Selamat Idul Fitri 1430H Taqabalallahu minna waminkum Taqabbal ya karim Mohon maaf lahir dan batin, Constructors and Destructors.
paly

This message

  • Uploaded on | 1 Views
  • johanna johanna

About Selamat Idul Fitri 1430H Taqabalallahu minna waminkum Taqabbal ya karim Mohon maaf lahir dan batin, Constructors and Destructors.

PowerPoint presentation about 'Selamat Idul Fitri 1430H Taqabalallahu minna waminkum Taqabbal ya karim Mohon maaf lahir dan batin, Constructors and Destructors.'. This presentation describes the topic on This message. The key topics included in this slideshow are . Download this presentation absolutely free.

Presentation Transcript


Slide1Selamat Idul Fitri 1430 H Taqabalallahu minna waminkum  Taqabbal ya karim…  Mohon maaf lahir dan batin  

Slide2Constructors and Destructors Constructors  Parameterized Constructors  Multiple Constructors  Constructors with Default Arguments  Copy Constructors  Destructors

Slide3Background 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.

Slide4CONSTRUCTOR 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

Slide5Simple Constructorclass coba { int kode; public : coba() { kode = 1; }; void show() { cout << “Kode = “ << kode << endl; }; }; Void main() { coba cobaku; cobaku.show(); }

Slide6Parameterized 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 ) { … }; }

Slide7//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(); }

Slide8Multiple 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) {…}; }

Slide9// 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 = "<<m<<"\t n = "<<n<<"\n"; }; }; main() { integer int1; //call default argument integer int2 ( 10, 100 ); //call parametrized integer int3 ( int1 ); //call copy constructor int1.display(); int2.display(); int3.display(); }

Slide10Copy 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.

Slide11#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 = "<<id<<"\n"; }; }; main() { code A(100); //object A created and initialized code B(A); //call copy constructor code C = A; //call copy constructor code D; //object D created but  not initialized D = A; // assignment , not constructor cout<<“id A = “; A.display(); cout<<“id B = “; B.display(); cout<<“id C = “; C.display(); cout<<“id D = “; D.display(); }

Slide12DESTRUCTORS  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.

Slide13#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

Slide14Wise Words Today…Kasih Ibu Sepanjang Jalan Kasih Anak Sepenggalan…

Related


No related presentations.