This programe is made for biggners in c++ to made programe efficently and hide your prevate data form the user of your programe.This programe is made with the use of this pointer and also use the operator overloading. I will share this programme with others to understand it.

//this programme will gives the addition,subtraction,multiplication and division with reduce fraction
//and it also tells u to select your own choice
#include<iostream>
#include<conio.h>
using namespace std;
class fraction
{
    private://class attributes
        int num;
        int denum;
    public://class behaviors
        fraction()//constructor without parameters
        {
            num=denum=1;
        }
        fraction(int n,int m)//constructor with parameters
        {
            num=m;
            denum=m;
        }
        void setnum (int );
        int getnum();
        void setdenum(int  );
        int getdenum();
        //fraction add(fraction );
        // word operator is pre-defined
        fraction operator +(fraction );//operator overloadin for addition
        //fraction sub(fraction );
        fraction operator -(fraction );//operator overloading for subtraction
        //fraction multi(fraction );
        fraction operator *(fraction );//operator overloading for multiplication
        //fraction div(fraction );
        fraction operator /(fraction );//operator overloading for division
        fraction display();//use to display the values
        fraction reduce();//use to reduce the fraction
};
fraction result;//class object to save the result of numerator and dunumenator and to separate the data from the parameter's data which we pass to the
// ...functions
void fraction::setnum(int n)//set the value of numenator
{
    num=n;
}
void fraction::setdenum(int m)//set value of denumenator
{
    denum=m;
}
int fraction::getnum()//get the value of the numenator
{
    return num;
}
int fraction::getdenum()//get the value of denumenator
{
    return denum;
}
fraction fraction::operator +(fraction b)//the formula applied here on fractions a/b + c/d to solve them.
                                        //The formula is  ad + bc/bd
{
    //fraction result;
    result.num=(num*b.denum)+(b.num*denum);
    result.denum=denum*b.denum; 
}
fraction fraction::operator -(fraction b)//subtraction of fractions 
{
    result.num=(num*b.denum)-(b.num*denum);
    result.denum=denum*b.denum;
}
fraction fraction::operator *(fraction b)//multiplication of fractions 
{
    result.num=(num*b.num);
    result.denum=denum*b.denum;
}
fraction fraction::operator /(fraction b)//division of fraction is 
{
    result.num=(num*b.denum);
    result.denum=denum*b.num;
}
fraction fraction::reduce()//here is the algo to reduce the resultant fraction and to check whether the fraction is reducable or not
{
    //if(result.num>result.denum)
    {
        int small=result.denum;
        for(int i=small;i>0;i--)
        {
            if((result.num%i==0)&&(result.denum%i==0))
            {
                result.num/=i;
                result.denum/=i;
                //cout<<"\nReduced fraction is "<<reducenum<<"/"<<reducedenum<<endl;
            }

        }
        cout<<"\a\nReduced fraction is "<<result.num<<"/"<<result.denum<<endl;
    }
}
fraction fraction::display()
{
    int count=0;    
    for(int i=2;i<=100;i++)
    { 
        if(result.num%i==0)
        {
            if(result.denum%i==0)
            {
                count++;
                //cout<<"Fraction is reducable by  :"<<i<<endl;
            }
        }
    }

    if(count>=1)
    {
        cout<<"\aFraction is reducable   \n";
        cout<<result.num<<"/"<<result.denum<<"\n";
    }
    else
    {
        cout<<"\aFraction is not reducable  \n";
        cout<<result.num<<"/"<<result.denum<<"\n";
    }
}
int main()
{
    int a,b,c,d,x;
    fraction f1=fraction();
    fraction f2=fraction();
    cout<<"********************************************************************************"<<endl;
    //cout<<"Enter only positive intigers\n";
    cout<<"\tFraction 1:\n";
    cout<<"\aEnter numenator  ";
    cin>>a;
    cout<<"\aEnter denumenator  ";
    cin>>b;
    cout<<"\tFraction  2:\n";
    cout<<"\aEnter numenator  ";
    cin>>c;
    cout<<"\aEnter denumenator  ";
    cin>>d;
    cout<<"********************************************************************************"<<endl;
    f1.setnum(a);
    f1.setdenum(b);
    f2.setnum(c);
    f2.setdenum(d);
    cout<<"\aIf you need Addition of fraction Enter :1 "<<endl;
    cout<<"If you need Subtraction of fractions Enter :2\n";
    cout<<"If you need Multiplication of fractions Enter :3\n";
    cout<<"If you need Division of fractions Enter :4\n";
    cin>>x;
    cout<<"********************************************************************************\n";
        if(x==1)
        {
            cout<<"  Addition\n";
            //f1.add(f2);
            f1+f2;
            f2.display();
            f2.reduce();
        }
        if(x==2)
        {
            cout<<"  Subtraction\n";
            //f1.sub(f2);
            f1-f2;
            f2.display();
            f2.reduce();
        }
        if(x==3)
        {
            cout<<"  Multiplication\n";
            //f1.multi(f2);
            f1*f2;
            f2.display();
            f2.reduce();
        }
        if(x==4)
        {
            cout<<"  Division\n";
            //f1.div(f2);
            f1/f2;
            f2.display();
            f2.reduce();
        }
    return 0;
}

There are a number of issues with your post, starting with initializing member variables inside the body of the constructor instead of in the initializer list. Try, try again!

No, this progamme is properly working and there is no problem in my constructor.

Just because code works doesn't mean it is well written! I used to teach advanced C++ programming classes to our engineers from all over the world and covered these issues in depth.
Here is how your constructors should be:

public:
fraction() : num(1), denum(1)
{}
fraction(int n,int m) : num(n), denum(m)
{}

Note the error in your constructor with arguments where you set both num, and denum with the argument n. I assume you meant to set denum to argument m.

Edited 1 Year Ago by rubberman

This article has been dead for over six months. Start a new discussion instead.