2.2 Constructor

download 2.2 Constructor

of 17

Transcript of 2.2 Constructor

  • 8/9/2019 2.2 Constructor

    1/17

    Introduction To OOP1.0

    Classes And Objects

    2.0

    Inheritance and Polymorphism3.0

    Exception handling4.0

    1

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 8/9/2019 2.2 Constructor

    2/17

    2

  • 8/9/2019 2.2 Constructor

    3/17

    Define Constructor1

    Characteristics ofConstructor2

    Differentiate the types ofConstructor3

    Use constructor in Program4

    3

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 8/9/2019 2.2 Constructor

    4/17

    1. A constructor is a member (method/function) that is called automatically each time when

    an object is created.

    2. The constructor allocates sufficient memory space for the object.

    3. C++ will automatically supply a constructor if it is not supplied .

    4

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 8/9/2019 2.2 Constructor

    5/17

    1. Constructor has the same name as the class. For example constructor for class Employee

    is declared as:

    5

    class Employee{public:

    Employee ( ); /* pengisytiharanconstructor */

    };

    Nama kelas

    Nama method constructor

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 8/9/2019 2.2 Constructor

    6/17

    6

    2. The constructors task is to built and create object in memory. However a constructor

    does not return a value.

    3. An argument can be sent to the constructor

    4. The constructor is called automatically when an object is created.This means, we dont

    have to invoke it like other functions.

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 8/9/2019 2.2 Constructor

    7/17

    7

    1.Default Constructor

    2.Parameterized Constructor

    3.Initialization Constructor

    4.Copy Constructor

    5.Overloading Constructor

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 8/9/2019 2.2 Constructor

    8/17

    1. Does not require any value (parameter).

    2. Only have to declare the object.

    3. C++ will automatically supply a constructor if it is not supplied.

    8

    1

    . fa lt str ct r

    Secara automatik,

    pengisytiharan objek ini akan

    menyebabkan aturcara

    memperuntukkan ruang

    memori untuk objek box1.Untuk mewujudkan beberapa

    objek, kelas harus mempunyai

    constructorlalai.

    NOTA

    #includeclass box {private:

    float length, width;public:box ( ); // default constructorvoid input( );void area ( );

    };void main(){boxbox1; // pengistiharan kelas}

    Method constructor tidak menerima sebarang nilai

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 8/9/2019 2.2 Constructor

    9/17

    1. A constructor can have many parameters.

    2. If the constructor receives a parameter, when the object is created the parameter must be

    included with the declaration of the object.

    9

    2

    . ara t riz str ct r

    #includeclass SegiEmpat{int panjang,lebar;public:SegiEmpat(int a, int b):panjang(a),lebar(b){}int kira_luas(){return panjang * lebar;}};void main(){int m,n;coutm;coutn;SegiEmpat S(m,n);cout

  • 8/9/2019 2.2 Constructor

    10/17

    1. The value is entered after an argument list for function, starting with single point ( : )

    10

    3.I itializati str ct r

    class SegiEmpat{

    protected:int panjang,lebar;

    public:// method constructor

    SegiEmpat ():panjang(10),lebar(20){}};

    y Data panjang akan diberikan nilai 10 manakala nilai data lebar akan diberikan

    nilai 20.

    y { } masih diperlukan.

    PENERANGAN

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 8/9/2019 2.2 Constructor

    11/17

    The example of a program is to calculate the area of a rectangle based on the value given on methodconstructor.

    #includeclass SegiEmpat{int panjang,lebar;public:SegiEmpat():panjang(10),lebar(20){}

    int kira_luas(){return panjang * lebar;}};void main()

    {SegiEmpat a;cout

  • 8/9/2019 2.2 Constructor

    12/17

    1. Changing the private data value through a constructor.

    1

    4. y str ct r

    SegiEmpat a (1,2);SegiEmpat b(a);

    Objek b akan menyalin nilai yang

    dimiliki oleh objek a

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 8/9/2019 2.2 Constructor

    13/17

    #include

    class SegiEmpat{int panjang,lebar;public:SegiEmpat(int a, int b):panjang(a),lebar(b){}int kira_luas(){return panjang * lebar;}

    };

    void main(){int m,n;coutm;coutn;SegiEmpat S(m,n);

    SegiEmpat Z(S);cout

  • 8/9/2019 2.2 Constructor

    14/17

    #include

    class SegiEmpat{int panjang,lebar;public:SegiEmpat(int a, int b):panjang(a),lebar(b){}int kira_luas(){return panjang * lebar;}

    };

    void main(){int m,n;coutm;coutn;

    SegiEmpat S(m,n);//SegiEmpat Z(S);cout

  • 8/9/2019 2.2 Constructor

    15/17

    #include

    class SegiEmpat{

    int panjang,lebar;public:SegiEmpat(){panjang=0;lebar=0;}

    SegiEmpat(SegiEmpat & T){panjang=T.panjang;lebar=T.lebar;}

    void SetData(int a,int b){panjang=a;lebar=b;}

    int kira_luas(){return panjang * lebar;}};

    Method constructor

    salinan

    Selain itu, anda sebagai pengaturcara juga boleh menakrifkan sendiri

    method constructor salinan jika anda mahu.

    Contoh : Menakrifkan fungsi constructor.

    void main(){int m,n;coutm;coutn;

    SegiEmpat a;a.SetData(m,n);SegiEmpat b(a);cout

  • 8/9/2019 2.2 Constructor

    16/17

    1. Just like other methods, constructor can also be overloaded.

    2. You only have to list down all the different version of constructor needed to be used in

    the class.

    3. The main purpose of overloading a constructor is to provide options or give the object

    initial value or not.

    4. For example, in the following program, o1 will be given initial value but not for o2.

    16

    5. v rl a i g str ct r

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 8/9/2019 2.2 Constructor

    17/17

    #includeclass SegiEmpat_Sama{int panjang ;public:

    SegiEmpat_Sama ( ) { panjang = 5; }SegiEmpat_Sama ( int n ) { panjang=n;}int luas(){return panjang*panjang;}};int main(){SegiEmpat_Sama o1(10); /* isytiharkan

    objek dengan nilai awalan */

    SegiEmpat_Sama o2; /* isytiharkantanpa nilai Awalan */

    cout