0% found this document useful (0 votes)
5 views

Code OOP

Code OOP C++ KTLT

Uploaded by

Dương Cá Thu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Code OOP

Code OOP C++ KTLT

Uploaded by

Dương Cá Thu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Phan so

#include <bits/stdc++.h>

using namespace std;

int gcd(int a, int b) {


if (b==0)
return a;
else return gcd(b, a%b);
};

class PhanSo {
private:
int tu_so;
int mau_so;
public:
friend istream& operator >> (istream &in, PhanSo &x) {
in >> x.tu_so >> x.mau_so;
return in;
}
friend ostream& operator << (ostream &out, PhanSo x) {
out << x.tu_so << '/' << x.mau_so << endl;
return out;
}

PhanSo toi_gian() {
int hang_tu_chung = gcd(tu_so, mau_so);
this->tu_so = tu_so/hang_tu_chung;
this->mau_so = mau_so/hang_tu_chung;
return *this;
}

PhanSo operator / (PhanSo y) {


PhanSo result;
result.tu_so = this->tu_so * y.mau_so;
result.mau_so = this->mau_so * y.tu_so;

Untitled 1
result.toi_gian();
return result;
// if (result.tu_so % result.mau_so != 0) return resul
// else return result.tu_so / result.mau_so;
}

PhanSo &operator ! (){


int temp;
temp = this->tu_so;
this->tu_so = this->mau_so;
this->mau_so = temp;
return *this;
}

};

int main () {
PhanSo x;
cin >> x;
cout << x;
x.toi_gian();
cout << x;
PhanSo y;
cin >> y;
cout << y;
PhanSo z = x/y;
cout << z;
x = !x;
cout << x;
}

So Phuc
#include <bits/stdc++.h>

using namespace std;

Untitled 2
class SoPhuc {
private:
float Im, Re;
public:

SoPhuc() {
this->Re = 1;
this->Im = 1;
}

SoPhuc(float Re, float Im) {


this->Re = Re;
this->Im = Im;
}

SoPhuc lien_hop() {
SoPhuc khac (this->Re, -this->Im);
return khac;
}

friend ostream& operator << (ostream &out, SoPhuc x) {


if (x.Re != 0 && x.Im == 0) out << x.Re <<endl;
else if (x.Re == 0 && x.Im != 0) out << x.Im <<"j" <<e
else if (x.Re == 0 && x.Im == 0) out << 0 <<endl;
else out << x.Re << ((x.Im > 0) ? " + " : " - ")<<abs
return out;
}

float module (SoPhuc khac) {


return sqrt((khac.Re)*(khac.Re) + (khac.Im)*(khac.Im)
}

SoPhuc operator + (SoPhuc khac) {


SoPhuc result;
result.Re = this->Re + khac.Re;
result.Im = this->Im + khac.Im;
return result;

Untitled 3
}

SoPhuc operator - (SoPhuc khac) {


SoPhuc result;
result.Re = this->Re - khac.Re;
result.Im = this->Im - khac.Im;
return result;
}

SoPhuc operator * (SoPhuc khac) {


SoPhuc result;
result.Re = this->Re * khac.Re - this->Im * khac.Im;
result.Im = this->Re * khac.Im + khac.Re * this->Im;
return result;
}

SoPhuc operator / (SoPhuc khac) {


SoPhuc result;
result.Re = (this->Re * khac.Re + this->Im * khac.Im)
result.Im = (this->Im * khac.Re - this->Re * khac.Im)
return result;
}

};

int main () {
SoPhuc x;
SoPhuc y(4,5);
SoPhuc z(6,1);
cout << x;
cout << y;
cout << z;

SoPhuc t = y.lien_hop();
cout << t;
return 0;
}

Untitled 4
Mang Dong
#include <bits/stdc++.h>

using namespace std;

class DynamicArr {
private:
float* arr;
int size;
public:
DynamicArr() {
size = 0;
arr = new float[size];
}

DynamicArr(int initial_size) {
size = initial_size;
arr = new float[size];
}

~DynamicArr() {
delete[] arr;
}

DynamicArr &operator ++ () {
++size;
float *temp = new float[size];
for (int i=0; i < size-1 ; i++) {
temp[i] = arr[i];
}
temp[size-1] = 0;
delete[] arr;
arr = temp;
return *this;
}

DynamicArr &operator -- () {

Untitled 5
if (size > 0) {
--size;
float *temp = new float[size];
for (int i=0; i < size ; i++) {
temp[i] = arr[i];
}
delete[] arr;
arr = temp;
}
return *this;
}

DynamicArr &operator += (int n) {


size += n;
float *temp = new float[size];
for (int i=0; i < size - n; i++) {
temp[i] = arr[i];
}
for (int i=size-n; i<size; i++) {
temp[i] = 0;
}
delete[] arr;
arr = temp;
return *this;
}

DynamicArr &operator -= (int n) {


if (size > n) {
size -= n;
float *temp = new float[size];
for (int i=0; i < size ; i++) {
temp[i] = arr[i];
}
delete[] arr;
arr = temp;
}
return *this;
}

Untitled 6
};

int main () {
}

String
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

class String {
private:
char* str;
int size;
public:
String() {
size = 0;
str = new char[size+1];
str[0] = '\0';
}
String(char ch){
size = 1;
str = new char[size+1];
str[0] = ch;
str[1] = '\0';
}
String(const char *str){
this->size = strlen(str);
this->str = new char[size + 1];
strcpy(this->str, str);
}

friend ostream& operator << (ostream &out, String str){


out << str.str <<endl;

Untitled 7
return out;
}

// void xuat_thong_tin() {
// cout << str <<endl;
// }

String operator + (String str_khac) const {


String result;
result.size = this->size + str_khac.size;
result.str = new char[result.size+1];
strcpy(result.str, this->str);
strcat(result.str, str_khac.str);
return result;
}

String &operator = (const String& str_khac) {


if (this != &str_khac) {
delete[] this->str;
this->size = str_khac.size;
this->str = new char[str_khac.size + 1];
strcpy(str, str_khac.str);
}
return *this;
}

String operator ! () const {


String result;
result.size = this->size;
result.str = new char[result.size+1];
for (int i=0; i<result.size; i++) {
result.str[i] = this->str[this->size - 1 - i];
}
result.str[result.size] = '\0';
return result;
}

int getLen() {

Untitled 8
return size;
}
};

int main () {
String a;
String b('A');
String c("Nguyen Dang Duong");
cout << a << b << c;

String d("chuoi ghep");


String e("chuoi gan");

String f,g,h;
f = c+d;
g = e;
h = !c;
cout << f << g << h;
}

Untitled 9

You might also like