码迷,mamicode.com
首页 > 编程语言 > 详细

c++自学笔记2

时间:2015-01-21 09:00:43      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

#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();  //输出函数 
   
   

}; 


void Complex::print(){

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

}


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


       Complex c1(3,4),c2(5,-10),c3;
       c3=c1.complex_add(c2);
       cout<<"输出c3: ";
  c3.print();
  
    return 0;  
}
这个是通过运算符重载实现的,第一个是通过函数实现的,明显比较麻烦
#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();  //输出函数 
   
}; 


void Complex::print(){

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;  
}


重载运算的符进行分数的运算






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


class Rational{
      private:
    void normalize(); //负责对分数的简化处理 
     
    int numerator;    //分子 
    int denominator;   //分母 
 
 public:
  Rational(int num,int denom){
 
  numerator=num;
  denominator=denom;
 
  normalize();  // 化简 
  }
  Rational operator+(Rational rhs); 
  Rational operator-(Rational rhs);
      Rational operator*(Rational rhs);
   Rational operator/(Rational rhs);
   
 void print(); 
}; 




void Rational::normalize()  // 分子分母进行化简 
{
int t;
if(denominator<0)   // 分子小于0要处理 
{
numerator=-numerator;
denominator=-denominator;
}
int a=abs(numerator);
int b=abs(denominator);

while(b>0)         //欧几里得(辗转求余法) 
{
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::print(){


   if(numerator%denominator==0) //如果可以变成整数 
      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;  //加法重载 
      f1.print();
      cout<<" + ";
      f2.print();
      cout<<" = ";
      res1.print();
      cout<<endl;
      
      Rational res2=f1-f2;  //减法重载 
      f1.print();
      cout<<" - ";
      f2.print();
      cout<<" = ";
      res2.print();
      cout<<endl;
      
      Rational res3=f1*f2;  //乘法重载 
      f1.print();
      cout<<" * ";
      f2.print();
      cout<<" = ";
      res3.print();
      cout<<endl;
      
       Rational res4=f1/f2;  //除法重载 
      f1.print();
      cout<<" / ";
      f2.print();
      cout<<" = ";
      res4.print();
      cout<<endl;
      
      
      
      
      return 0;  
}





































































c++自学笔记2

标签:

原文地址:http://blog.csdn.net/lotluck/article/details/42948685

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!