Tuck+Gin

Done by Chan Tuck Ging 3S105 code format="cpp-qt" class rational{ protected: int num, den;

public:

rational{ num=0; den=1; };

rational(int a, int b){ num=a; den=b; };

~rational{ };

void output{ cout << num << "/" << den; }

void setfrac(int m,int n){ num=m; den=n; }

rational add(rational f2){ int numb, deno; numb=num*f2.den+den*f2.num; deno=den*f2.den; return simplify(numb,deno); }

rational minus(rational f2){ int numb, deno; numb=num*f2.den-f2.num*den; deno=den*f2.den; simplify(numb,deno); return simplify(numb,deno); }

rational multiply(rational f2){ int numb, deno; numb=num*f2.num; deno=den*f2.den; simplify(numb,deno); return simplify(numb,deno); }

rational divide(rational f2){ int numb, deno; numb=num*f2.den; deno=den*f2.num; simplify(numb,deno); return simplify(numb,deno); }

rational simplify(int m, int n){ int hcf=1; for (int i=less(m,n);i>=0;i--){ if (m % i == 0 && n % i == 0){ hcf=i; break; }           }            rational f3(m/hcf,n/hcf); return f3; }

int abs(int m){ if(m<0){ m=-m; }           return m;        }

int less(int m, int n){ if (abs(m)>=abs(n)){ return abs(n); }           else { return abs(m); }       } }; code

Inheritance: code format="cpp-qt"
 * 1) include

using namespace std;

class point{ protected: int x, y;     public: point(int ptx, int pty){x = ptx; y = pty;}; ~point{}; void setptx(int ptx){x = ptx;}; void setpty(int pty){y = pty;}; int getptx{return x;}; int getpty{return y;}; void Display{cout << x << "," << y; }; };//Base Class point

class point3d:public point{ protected: int z;     public: point3d(int a, int b, int c):point(a,b){z=c;}; ~point3d{}; void setptz(int ptz){z = ptz;}; int getptz{return z;}; void Display{cout << x << "," << y << "," << z;}; };

int main{ point P1(1,2); point3d P2(1,2,3); P1.Display; cout << endl; P2.Display; cout << endl; P2.point::Display; cout << endl; system ("pause"); return 0; } code

Done by Chan Tuck Ging 3S105