You are on page 1of 1

Bài tập thực hành 28/04/2022

Bài 1: Mở file Fraction1.cpp ra và bổ sung các hàm chồng toán tử vào


Cài đặt các hàm chồng toán tử trong lớp Fraction Ví dụ minh hoạ

//toan tu tang giam Fraction p(1,2); Fraction f(3,4)


Fraction operator ++(); ++p; p.output(); // p = 3/2
Fraction operator --(); --p; p.output(); // p = -1/2

//toan tu 1 ngoi : dao dau


Fraction operator-(); p = - p; p.output(); // p = 1/2

//toan tu mo rong
Fraction operator+=(int x); p += 4; p.output(); // p = 9/2
Fraction operator+=(Fraction f2); p += f; p.output(); // p = 21/4
Fraction operator-=(int x);
Fraction operator-=(Fraction f2);
Fraction operator*=(int x);
Fraction operator*=(Fraction f2);
Fraction operator/=(int x);
Fraction operator/=(Fraction f2);

//so sanh
bool operator==(Fraction f2); System.out.println( p == f); //false
bool operator!=(Fraction f2);
bool operator<(Fraction f2);
bool operator<=(Fraction f2);
bool operator>(Fraction f2);
bool operator>=(Fraction f2);

Bài 2: Mở file Set.h ra cài đặt bổ sung các hàm chồng toán tử
Cài đặt các hàm chồng toán tử trong lớp Fraction Ví dụ minh hoạ

bool operator==(Set &s); //bang Tự gọi các chồng toán tử ở hàm main
bool operator!=(Set &s); //khong bang
Set operator *(Set &s); //giao cua 2 Set
Set operator-(Set &); // tru cua 2 Set
Set operator+ (Set &); //hop cua 2 Set
//chong toan tu get(index)
int operator[](int index){return a[index];}

You might also like