Operator overloading
This presentation is the property of its rightful owner.
Sponsored Links
1 / 20

Operator overloading PowerPoint PPT Presentation


  • 76 Views
  • Uploaded on
  • Presentation posted in: General

Operator overloading. Konsep overloading terhadap operator didasarkan pada operasi yang terjadi sehari-hari misalkan operator + biasa digunakan untuk menjumlahkan bilangan bulat ataupun bilangan pecahan . 4 + 5 3.56 + 3.08

Download Presentation

Operator overloading

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Operator overloading

Operator overloading


Operator overloading

Konsep overloading terhadap operator didasarkanpadaoperasi yang terjadisehari-harimisalkan operator + biasadigunakanuntukmenjumlahkanbilanganbulatataupunbilanganpecahan.

4 + 5

3.56 + 3.08

Bagaimanajika operator + digunakanuntukpenjumlahanbilangankompleks?


Operator overloading

  • Untukmenjumlahkanbilangankompleks, perlusedikitmodifikasidalampenggunaan operator +

  • Penambahanbilangankompleksmelibatkanpenambahansisi real danimajiner

    (3 - 5i) + (6 + 7i) = (3 + 6) + (-5 + 7)i

    = 9 + 2i


Operator overloading

  • Operator overloading artinyamelakukanmodifikasi / overloading pada operator (+, -, *, dsb) sesuaikebutuhan

    Syntax operator overloading:

    Tipeoperator simbol_operator ( argument )


Operator yang dapat di overloading

Operator yang dapatdi-overloading


Operator yang tidak dapat di overloading

Operator yang tidakdapatdi-overloading


Overloading operator biner

Overloading operator biner

Operator binermerupakan operator dengandua operand, contoh operator+.

Bentukumum overloading operator bineradalah:

TipeDataoperator simbol_operator (object )


Operator overloading

#include <iostream>

using namespace std;

class complex

{

float riil; float imajiner;

public:

complex()

{ riil=0;

imajiner=0;

};

complex(float real, float imaginer)

{

riil=real;

imajiner=imaginer;

};

void display(void)

{

cout<<"["<<riil<<"]+["<<imajiner<<"i]\n";

};

complex operator +(complex c)

{

complex temp;

temp.riil = riil+c.riil;

temp.imajiner=imajiner+c.imajiner;

return(temp);

};

};

main()

{

complex c1(2.5, 6.1);

complex c2(3.3, 4.5);

complex c3;

c3=c1+c2;

cout<<"\nC1=";

c1.display();

cout<<"\nC2=";

c2.display();

cout<<"\nC3=";

c3.display();

system("pause");

}


Operator overloading

Lengkapi program diatasuntukoperasiberikut

  • Addition

    (a + bi) + (c + di) = (a + c) + (b + d)i.

  • Substraction

    (a + bi) - (c + di) = (a - c) + (b - d)i.

  • Multiplication

    (a + bi) * (c + di) = (ac - bd) + (ad + bc)i.

    c* (a + bi) = c*a + c*bi

  • Addition dengan bilangan integer

    (a + bi) + x= (a+x) + bi


Operator overloading

Lengkapi program diatasuntukoperasi berikut

  • Increment

    (a + bi)++ = (a ++) + (b++)i

  • Operator << (operator keluaransetelahcout)

    tampil : Riil = a, Imajiner=b;

  • Operator >> (operator masukansetelahcin)

    tampil : Masukkan bilangan riil :

    Masukkan bilangan imajiner :


Operator overloading

Operator increment adaduamacamyaitu:

Pre increment, dengan format:

Nama_kelasoperator++();

Post increment dengan format:

Nama_kelasoperator++(int);

Operator decrement jugaadaduayaitu:

Predecrement, dengan format:

Nama_kelasoperator--();

Post decrement, dengan format:

Nama_kelasoperator--(int);

Overloading operator increment dan decrement


Contoh program

Contoh Program

// Program c++dengan operator decrement &increment

#include <constream.h>

class Date {

private:

intmonth, day, year;

static const intdays[ ] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

void helpIncrement();

public:

Date(intm=1, intd=1, inty=1990);

void setDate(intm, intd, inty);

Date operator++();

Date operator++( int);

const Date operator+= ( int);

intleapYear( int);

intendOfMonth( int);

};


Operator overloading

// pre increment

Date Date::operator++() {

helpIncrement();

return (*this)

}

// post increment

Date Date::operator++(int){

Date temp = *this

helpIncrement();

return (*this)

}

void Date::helpIncrement(){

if (endOfMonth( day ) && month == 12) {

day = 1;

month = 1;

++year;

}


Operator overloading

else if ( endOfMonth(day) ) {

day = 1;

++month;

}

else

++day;

}

intDate::endOfMonth( intd) {

if ( month ==2 && leapYear( year ) )

return (d == 29);

else

return (d == days[ moth ]);

}

intDate::leapYear( intyr){

if ( (yr%40 == 0) || (yr%100 != 0 && yr%4 == 0) )

return 1;

else


Operator overloading

return 0;

}

const Date operator+= ( intaddays) {

for (int I=0; I < addays; I++)

helpIncrement();

return *this;

}


Operator overloading

Operator overloading yang seringmenggunakanfriend adalah << padacoutdan >> padacin

Dengan overloading inidimungkinkanmenuliskan statement:

cin>> object;

cout<< object;

Operator overloading dengan friend


Operator overloading

Bentukumumdari overloading terhadap operator << adalah:

ostream& operator << (ostream& output, object) {

// tubuhfungsi

return (output);

}

Bentukumumdari overloading terhadap operator >> adalah:

istream& operator >> (istream& input, object &) {

// tubuhfungsi

return (input);

}

Operator overloading dengan friend


Contoh program1

Contoh Program

// program C++ operator >> dan << dengan friend

#include <constream.h>

#include <string.h>

class Buku {

private:

intNomor;

char Judul[30];

char Pengarang[10];

public:

Buku();

friend ostream& operator << (ostream& o, Buku b1);

friend istream& operator >> (istream& i, Buku &b1);

};


Operator overloading

// definisifungsi

Buku::Buku() {

Nomor = 0;

strcpy(Judul, "");

strcpy(Pengarang, "");

}

//definisifungsi friend

ostream& operator<<(ostream& out, Buku b1){

out << "Nomorbuku: " << b1.Nomor << endl;

out << "Judulbuku: " << b1.Judul << endl;

out << "Pengarang: " << b1.Pengarang << endl;

return (out);

}

istream& operator>> (istream& in, Buku& b1){

cout<< "inputkanNomorbuku ";

in >> b1.Nomor;

cout<< "inputkanJudulbuku ";


Operator overloading

in >> b1.Judul;

cout<< "inputkanPengarang ";

in >> b1.Pengarang;

return (in);

}

void main() {

Bukubuku_C;

cin >> buku_C;

cout << buku_C;

}


  • Login