C + + self-study NOTE 2

Source: Internet
Author: User

#include <iostream>


using namespace Std;
Class complex{
Private
int rear;
int imag;

Public
Complex () {
rear=0;
imag=0;
}
Complex (int r, int i) {
Rear=r;
Imag=i;
}
Complex Complex_add (Complex &d) {

Complex C;

C.rear=rear+d.rear;
C.imag=imag+d.imag;

return C;
}
void print (); Output function



};


void Complex::p rint () {

cout<< "(" <<rear<< "," <<imag<< ")" <<endl;

}


int main (int argc, char** argv) {


Complex C1 (3,4), C2 (5,-10), C3;
C3=c1.complex_add (C2);
cout<< "output C3:";
C3.print ();

return 0;
}
This is implemented by operator overloading, and the first one is implemented through a function, which is obviously more troublesome
#include <iostream>


using namespace Std;
Class complex{
Private
int rear;
int imag;

Public
Complex () {
rear=0;
imag=0;
}
Complex (int r, int i) {
Rear=r;
Imag=i;
}
Complex operator+ (Complex &d) {

Complex C;

C.rear=rear+d.rear;
C.imag=imag+d.imag;

return C;
}
void print (); Output function

};


void Complex::p rint () {

cout<< "(" <<rear<< "," <<imag<< ")" <<endl;

}


int main (int argc, char** argv) {


Complex C1 (3,4), C2 (5,-10), C3;
C3=C1+C2;
cout<< "c1=";
C1.print ();
cout<< "c2=";
C2.print ();
cout<< "c1+c2=";
C3.print ();

return 0;
}


Operation of fractions with overloaded operators






#include <iostream>
#include <math.h>
using namespace Std;


Class rational{
Private
void normalize (); Responsible for simplifying the processing of fractions

int numerator; Molecular
int denominator; Denominator

Public
Rational (int num,int denom) {

Numerator=num;
Denominator=denom;

Normalize (); Simplification
}
Rational operator+ (rational RHS);
Rational operator-(rational RHS);
Rational operator* (rational RHS);
Rational operator/(rational RHS);

void print ();
};




void Rational::normalize ()//molecular denominator simplification
{
int t;
if (denominator<0)//molecule less than 0 to be processed
{
Numerator=-numerator;
Denominator=-denominator;
}
int A=abs (numerator);
int b=abs (denominator);

while (b>0)//Euclid (the method of finding the remainder)
{
int t=a%b;
A=b;
b=t;
}

Numerator/=a;
Denominator/=a;
}


Rational rational::operator+ (rational RHS) {


int a=numerator;
int b=denominator;
int c=rhs.numerator;
int d=rhs.denominator;

int e=a*b+c*d;
int f=b*d;

Return Rational (E,F);

}
Rational rational::operator-(rational RHS) {

Rhs.numerator=-rhs.numerator;

Return operator+ (RHS);


}
Rational rational::operator* (rational RHS) {

int a=numerator;
int b=denominator;
int c=rhs.numerator;
int d=rhs.denominator;

Return Rational (A*C,B*D);
}
Rational rational::operator/(rational RHS) {

int a=numerator;
int b=denominator;
int c=rhs.numerator;
int d=rhs.denominator;

Return Rational (A*D,B*C);

}


void Rational::p rint () {


if (numerator%denominator==0)//If it can become an integer
cout<<denominator/numerator;
Else
cout<<numerator<< "/" <<denominator;

}


int main (int argc, char** argv) {


Rational F1 (2,16);
Rational F2 (7,8);

Rational Res1=f1+f2; Addition overloading
F1.print ();
cout<< "+";
F2.print ();
cout<< "=";
Res1.print ();
cout<<endl;

Rational Res2=f1-f2; Subtraction overloading
F1.print ();
cout<< "-";
F2.print ();
cout<< "=";
Res2.print ();
cout<<endl;

Rational Res3=f1*f2; Multiply overloading
F1.print ();
cout<< "*";
F2.print ();
cout<< "=";
Res3.print ();
cout<<endl;

Rational Res4=f1/f2; Division overloading
F1.print ();
cout<< "/";
F2.print ();
cout<< "=";
Res4.print ();
cout<<endl;




return 0;
}





































































C + + self-study NOTE 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.