• Tidak ada hasil yang ditemukan

Object Oriented Programming

N/A
N/A
Protected

Academic year: 2025

Membagikan "Object Oriented Programming"

Copied!
11
0
0

Teks penuh

(1)

Object Oriented Programming

Rupesh Nasre.

[email protected]

January 2016 QEEE

(2)

2

Let's make tea: Operator Overloading.

...

int main() { Pot pot;

Burner burner;

Water water(1);

Tealeaves tealeaves(1) Sugar sugar(1);

Milk milk(0.5);

burner.start(pot);

pot.add(water);

pot.add(tealeaves);

burner.boil(2, false);

pot.add(sugar);

pot.add(milk);

burner.boil(2, true);

burner.stop();

std::cout << “Tea is ready.\n”;

return 0;

} ...

int main() { Pot pot;

Burner burner;

Water water(1);

Tealeaves tealeaves(1) Sugar sugar(1);

Milk milk(0.5);

burner.start(pot);

pot += water + tealeaves;

burner.boil(2, false);

pot += sugar + milk;

burner.boil(2, true);

burner.stop();

std::cout << “Tea is ready.\n”;

return 0;

}

(3)

3

Do we need to overload operators?

Language is for communication

English between humans

C++ between human and machine

A good language enables effective communication.

Imagine speaking in C++

Imagine creating facebook in assembly

A language should naturally be able to express human thoughts.

coffee = milk + sugar + coffeepower;

value = base ^ exponent;

(4)

4

Example

class OpOverload { public:

OpOverload(int n): first(n) { }

int operator +(int second) { return first + second; } private:

int first;

};

int main() {

OpOverload o1(4);

OpOverload *o2 = new OpOverload(10);

std::cout << o1 + 10 << ", " << *o2 + 4 << std::endl;

delete o2;

return 0;

}

Type is

OpOverload + int What about

std::cout << 10 + o1?

(5)

5

What does Compiler do?

Operator is just another method.

milk.operator+(sugar);

For supporting polymorphism, compiler tracks function argument types.

list.add(integer_value);

list.add(struct_value);

Together, we can overload operator with different argument types.

milk + sugar;

milk + coffeepowder;

Homework: Give an example of operator overloading in C?

(6)

6

Can you spot second overloading?

...

int main() { Pot pot;

Burner burner;

Water water(1);

Tealeaves tealeaves(1) Sugar sugar(1);

Milk milk(0.5);

burner.start(pot);

pot.add(water);

pot.add(tealeaves);

burner.boil(2, false);

pot.add(sugar);

pot.add(milk);

burner.boil(2, true);

burner.stop();

std::cout << “Tea is ready.\n”;

return 0;

} ...

int main() { Pot pot;

Burner burner;

Water water(1);

Tealeaves tealeaves(1) Sugar sugar(1);

Milk milk(0.5);

burner.start(pot);

pot += water + tealeaves;

burner.boil(2, false);

pot += sugar + milk;

burner.boil(2, true);

burner.stop();

std::cout << “Tea is ready.\n”;

return 0;

}

(7)

7

Overloading <<

std is a namespace, cout is an object in it, whose type is ostream.

ostream has operators overloaded for basic types (int, bool, …).

What if I want to output my object?

std::cout << s; where s is instance-of Student

cout doesn't know about Student or OpOverload classes.

Java solves this problem with toString() method.

(8)

8

Overloading <<

class OpOverload { public:

OpOverload(int n): first(n) { }

int operator +(int second) { return first + second; }

friend std::ostream& operator <<(std::ostream& os, const OpOverload &oo);

private:

int first;

};

std::ostream& operator <<(std::ostream& os, const OpOverload &oo) { os << oo.first;

return os;

}

int main() {

OpOverload o1(4);

std::cout << o1 << std::endl;

return 0;

}

(9)

9

Overloading Restrictions

You can't redefine for existing primitive types.

At least one operand must be user-defined.

Syntax rules cannot be changed.

% operator has to take two operands.

You cannot create new operator symbols.

operator **() is disallowed.

Following operators cannot be overloaded.

sizeof, ., .*, ::, ?:, typeid, cast operators.

(10)

10

Returning a Reference

Recall that we returned from operator << a reference to a const object.

friend std::ostream& operator <<(std::ostream& os, const OpOverload &oo);

This can be done in other situations too.

Often the reason is efficiency. However, we should be aware of the following.

– When we return reference, there is no copying.

– When we return value, a copy constructor gets

invoked.

(11)

11

In these lectures

Introduction to OOP

Classes and Objects

Operator Overloading

Inheritance

Prerequisite:

Programming experience

References:

The C++ Programming Language, Bjarne Stroustrup, 4e, Pearson

C++ Primer Plus, Stephen Prata, 6e, Pearson

Concepts are applicable in general.

We will use C++ and Linux as the environments.

Concepts are applicable in general.

We will use C++ and Linux as the environments.

Referensi

Dokumen terkait

menghantar argumen-argumen 1, ukur1 dan ukur2 ke dalam fungsi luas() yang terdapat dalam kelas generik bentuk dan memberi nilai 1 pada pembolehubah pilihan, nilai ukur1

- Baris 1: PersegiEmpat(15,10); dari potongan baris kode tersebut bisa dilihat bahwa ketika kita membuat objek baru dari class, maka program akan memanggil

(2010:178), Pemrograman berorientasi objek merupakan sebuah teknik pemrograman yang dalam proses pengembangannya menggunakan teknologi objek, dimana setiap objek memiliki

Karakter pertama dan kedua harus berisi data teks, karakter ketiga dan keempat harus berisi angka, dan angka 1 pada posisi field ke-2 menunjukan bahwa karakter literal “-“

Ethanol-water azeotropic binary system examples and Acetone-Butanol, Acetone-Ethanol, Butanol-Ethanol zeotropic binary system examples, simulation of batch distillation with

11 7.aExplain in detail about the various run time casting in C++ 6 b Write a C++ program for calculating the are of rectangle and circle using run time polymorphism 5 8.a Write a

115 Chapter 5 Advanced Topics in Java Learning Outcomes After you have read this chapter, you should be able to • understand scope of variables declaration in Java code •