operator overloading
Download
Skip this Video
Download Presentation
Operator overloading

Loading in 2 Seconds...

play fullscreen
1 / 20

Operator overloading - PowerPoint PPT Presentation


  • 134 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Operator overloading' - lucky


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
slide2

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

4 + 5

3.56 + 3.08

Bagaimanajika operator + digunakanuntukpenjumlahanbilangankompleks?

slide3

Untukmenjumlahkanbilangankompleks, perlusedikitmodifikasidalampenggunaan operator +

  • Penambahanbilangankompleksmelibatkanpenambahansisi real danimajiner

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

= 9 + 2i

slide4

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

Syntax operator overloading:

Tipeoperator simbol_operator ( argument )

overloading operator biner
Overloading operator biner

Operator binermerupakan operator dengandua operand, contoh operator+.

Bentukumum overloading operator bineradalah:

TipeDataoperator simbol_operator (object )

slide8
#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");

}

slide9
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

slide10
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 :

slide11
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);

};

slide13
// 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;

}

slide14
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

slide15
return 0;

}

const Date operator+= ( intaddays) {

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

helpIncrement();

return *this;

}

slide16
Operator overloading yang seringmenggunakanfriend adalah << padacoutdan >> padacin

Dengan overloading inidimungkinkanmenuliskan statement:

cin>> object;

cout<< object;

Operator overloading dengan friend

slide17
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);

};

slide19
// 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 ";

slide20
in >> b1.Judul;

cout<< "inputkanPengarang ";

in >> b1.Pengarang;

return (in);

}

void main() {

Bukubuku_C;

cin >> buku_C;

cout << buku_C;

}

ad