OOPp (bài 1)(phân số) constructor desstructor getter-setter bài tậ

//////file phan_so.h/////////// #pragma once #include<iostream> using namespace std; class phan_so { private: int tu_so,mau_so; public: // khởi tạo mặc định phan_so(void); phan_so(int ,int ); phan_so(int ); //phan_so(const phan_so &); // phá hủy mặc định ~phan_so(void); void xuat(); phan_so tinhtong(phan_so); }; //////////////////file phan_so.cpp/////////////////// #include "phan_so.h" phan_so::phan_so(void) { tu_so =0; mau_so=1; } phan_so::phan_so(int a,int b) { tu_so=a; mau_so=b; } phan_so::phan_so(int a) { tu_so=a; mau_so=1; } phan_so phan_so::tinhtong(phan_so a) { phan_so s; s.tu_so=tu_so*a.mau_so+mau_so*s.tu_so; s.mau_so=mau_so*a.mau_so; return s; } void phan_so::xuat() { cout<<tu_so<<"/"<<mau_so; cout<<endl; } phan_so::~phan_so(void) { } ///////////////////hàm main()//////////////////////////// #include"phan_so.h" int main() { phan_so a; a.xuat(); phan_so b(1,2); b.xuat(); phan_so c(3,4); c.xuat(); phan_so d(3); d.xuat(); phan_so e(-5); e.xuat(); phan_so f(c); f.xuat(); a=b.tinhtong(c); a.xuat(); system("pause"); return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.