1 / 20

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

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

4 + 5

3.56 + 3.08

Bagaimanajika operator + digunakanuntukpenjumlahanbilangankompleks?

• Untukmenjumlahkanbilangankompleks, perlusedikitmodifikasidalampenggunaan operator +

• Penambahanbilangankompleksmelibatkanpenambahansisi real danimajiner

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

= 9 + 2i

Tipeoperator simbol_operator ( argument )

Operator binermerupakan operator dengandua operand, contoh operator+.

TipeDataoperator simbol_operator (object )

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

}

Lengkapi program diatasuntukoperasiberikut

(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

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

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 :

Pre increment, dengan format:

Nama_kelasoperator++();

Post increment dengan format:

Nama_kelasoperator++(int);

Predecrement, dengan format:

Nama_kelasoperator--();

Post decrement, dengan format:

Nama_kelasoperator--(int);

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

};

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;

}

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

return 0;

}

const Date operator+= ( intaddays) {

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

helpIncrement();

return *this;

}

cin>> object;

cout<< object;

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

// tubuhfungsi

return (output);

}

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

// tubuhfungsi

return (input);

}

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

};

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

cout<< "inputkanPengarang ";

in >> b1.Pengarang;

return (in);

}

void main() {

Bukubuku_C;

cin >> buku_C;

cout << buku_C;

}