Completely Solved C, C++ Programs Assignment.




C++ Program to represent fraction with numerator and denominator as integer pointers

Filed Under:

Problem Statement: Implement a class FRACTION to represent fractions with numerator and denominator as integer pointers, and appropriate constructor,copy constructor, assignment operator, addition operator, multiplication operator, subtraction operator and division operator as members.

Logical Approach :
The above program is done using an Abstract Data Type (ADT) called class. A class is a mechanism to create objects. In this program a class named fraction is created.
It contains the following data members :
• num(denoting numerator)
• deno(denoting denominator)
It contains the following member functions :
• fraction()-Default constructor.
• fraction(int,int)-Parameterized constructor, used to initialize a fraction.
• fraction(const fraction&)-Copy constructor, used to make a new copy of a fraction.
• printfrac()-Used to display a fraction.
• operator+(fraction)-Used to add two fractions.
• operator-(fraction)-Used to subtract two fractions.
• operator*(fraction)-Used to multiply two fractions.
• operator/(fraction)-Used to divide two fractions.
• operator=(fraction)-Assignment operator.

In this program we have used operator overloading feature. Operator overloading (less commonly known as operator ad-hoc polymorphism) is a specific case of polymorphism in which some or all of operators like +, =, or == have different implementations depending on the types of their arguments. Sometimes the overloadings are defined by the language; sometimes the programmer can implement support for new types.
To define an additional task to an operator,we must specify what it means in relation to the class to which the operator is applied. This is done with help of an operator function,which describes the task. Operator functions must be either member functions(non-static) or friend functions. In this program,we have done all the operations by using member functions.
First user should enter two fractions on which he or she wants to perform some arithmetic
operations and visualize the result on the screen. Then main menu will be appeared on the screen from which user can choose following operations to perform:
1.Addition
2.Multiplication
3.Subtraction
4.Division
5.Copy using copy constructor
6.Copy using assignment operator

After performing an operation user can exit from the program, can continue operation with previous two fractions or can continue operation after taking another two fractions as input; choice is totally up to user. We also use a few other non-member functions to find L.C.M,G.C.D of two numbers as they are needed in the program.

Algorithm :
Creating an ADT with name fraction

Step 1:Creating an ADT fraction
Step 2:Declaring private data members
int* num//Declaring an integer pointer
int* deno//Declaring an integer pointer
Step 3:Declaring public member functions
fraction(int,int)
fraction(const fraction&)
printfrac()
operator+(fraction)
operator-(fraction)
operator*(fraction)
operator/(fraction)
operator=(fraction)

Default constructor

fraction()
{
Initializing num and deno as 0;
}
Parameterized constructor

fraction( num,deno)
{
Initializing num and deno with the values passed as parameters;
}
Copy constructor

fraction(const fraction&f)
{
//Allocating new locations
num= new int;
deno= new int;
num= f.num;
deno= f.deno;
}

This function is used to withdraw balance from an account

procedure operator=(fraction f)
{
num= f.num;
deno= f.deno;
}

This function is used to add two fractions

procedure operator+(fraction f1)
{
slcm= lcm(f1.deno,deno);
num1= f1.num*(slcm/f1.deno);
num2= num*(slcm/deno);
num=num1+num2;
t=num;
num=num/gcd(num,slcm);
slcm=slcm/gcd(t,slcm);
fraction f(num,slcm);
}

This function is used to subtract two fractions
procedure Operator-(fraction f1)
{
slcm= lcm(f1.deno,deno);
num1= f1.num*(slcm/f1.deno);
num2= num*(slcm/deno);
num=num1-num2;
t=num;
num=num/gcd(num,slcm);
slcm=slcm/gcd(t,slcm);
fraction f(num,slcm);
}

This function is used to multiply two fractions
procedure operator*(fraction f1)
{
num1=(f1.num)*num;
num2=(f1.deno)*deno;
t=num1;
num1=num1/gcd(num1,num2);
num2=num2/gcd(t,num2);
fraction f(num1,num2);
}

This function is used to divide two fractions
procedure operator/(fraction f2)
{

num1=num*(f2.deno);
num2=deno*(f2.num);
t=num1;
num1=num1/gcd(num1,num2);
num2=num2/gcd(t,num2);
fraction f(num1,num2);
}

This function is used to display a fraction
printfrac()
{
Print num “/” deno;
}

Main procedure
while(true)
{
Read numerator and denominator of 1st fraction in n1 and d1;
Read numerator and denominator of 2nd fraction in n2 and d2;
fraction f1(n1,d1);
fraction f2(n2,d2);
do
{
Print "
1.Addition
2.Multiplication
3.Subtraction
4.Division
5.Copy using copy constructor
6.Copy using assignment operator";
Print “Enter which operation you want to perform";
Read c;
if(c=1)
{
fraction res=f1+f2;
res.printfrac();

}
else if(c=2)
{
fraction res=f1*f2;
res.printfrac();
}
else if(c=3)
{
fraction res=f1-f2;
res.printfrac();
}
else if(c=4)
{
fraction res=f1/f2;
res.printfrac();
}
else if(c=5)
{
Print "New copy of 1st fraction";
fraction res1=f1;
res1.printfrac();
Print "New copy of 2nd fraction";
fraction res2=f2;
res2.printfrac();
}
else if(c=6)
{
Print "New copy of 1st fraction";
fraction res1;
res1=f1;
res1.printfrac();
Print "New copy of 2nd fraction";
fraction res2;
res2=f2;
res2.printfrac();
break;
}
else
Print "Invalid choice!!!";
Print "Do you want to exit from program(Y/N)";
Read ch;
if(ch='Y' OR ch='y')
Exit from algorithm;
Print "Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N)";
Read ch;
}while(ch='Y' OR ch='y');
}
}


Program listing :
/*C++ program Implement a class FRACTION to represent fractions with numerator and denominator as integer pointers, and appropriate constructor,copy constructor, assignment operator, addition operator, multiplication operator, subtraction operator and division operator as members.*/

#include<iostream>

using namespace std;
//Creating a Class fraction
class fraction
{
private:
int* num;
int* deno;
public:
fraction();
fraction(int,int);
fraction(const fraction&);
void printfrac();
fraction operator+(fraction);
fraction operator-(fraction);
fraction operator*(fraction);
fraction operator/(fraction);
void operator=(fraction);
};
//Used to display a fraction
void fraction::printfrac()
{
cout<<*(fraction::num)<<"/"<<*(fraction::deno);
}
//Default constructor
inline fraction::fraction()
{
this->num=new int(0);
this->deno=new int(0);
}
//Parameterized constructor, used to initialize a fraction
inline fraction::fraction(int num,int deno)
{
this->num=new int(num);
this->deno=new int(deno);
}
//Copy constructor, used to make a new copy of a fraction
inline fraction::fraction(const fraction&f)
{
fraction::num=new int;
fraction::deno=new int;
*(this->num)=*(f.num);
*(this->deno)=*(f.deno);
}
//This function is used to swap two variables
void swap(int &x,int &y)
{
int tmp=x;
x=y;
y=tmp;
}
//This function is used to find L.C.M of two numbers
int lcm(int l,int s)
{
if(l<s)
swap(l,s);
int m=2,result=l;
while(result%s)
{
result=l*m;
m++;
}
return result;
}
//This function is used to find G.C.D of two numbers
int gcd(int a,int b)
{
if(a<b)
swap(a,b);
while(1)
{
int c=a%b;
if(c==0)
return b;
a=b;
b=c;
}
}
//Assignment operator
void fraction::operator=(fraction f)
{
*(this->num)=*(f.num);
*(this->deno)=*(f.deno);
}
//Used to add two fractions
fraction fraction::operator+(fraction f1)
{
int slcm=lcm(*(f1.deno),*(this->deno));
int num1=*(f1.num)*(slcm/ *(f1.deno));
int num2=*(this->num)*(slcm/ *(this->deno));
int num=num1+num2;
int t=num;
num=num/gcd(num,slcm);
slcm=slcm/gcd(t,slcm);
fraction f(num,slcm);
return f;
}
//Used to subtract two fractions
fraction fraction::operator-(fraction f2)
{
int slcm=lcm(*(this->deno),*(f2.deno));
int num2=*(f2.num)*(slcm / *(f2.deno));
int num1=*(this->num)*(slcm / *(this->deno));
int num=num1-num2;
int t=num;
num=num/gcd(num,slcm);
slcm=slcm/gcd(t,slcm);
fraction f(num,slcm);
return f;
}
//Used to multiply two fractions
fraction fraction::operator*(fraction f1)
{
int num1=*(f1.num)*(*(this->num));
int num2=*(f1.deno)*(*(this->deno));
int t=num1;
num1=num1/gcd(num1,num2);
num2=num2/gcd(t,num2);
fraction f(num1,num2);
return f;
}
//Used to divide two fractions
fraction fraction::operator/(fraction f2)
{

int num1=*(this->num)*(*(f2.deno));
int num2=*(this->deno)*(*(f2.num));
int t=num1;
num1=num1/gcd(num1,num2);
num2=num2/gcd(t,num2);
fraction f(num1,num2);
return f;
}
int main()
{
int n1,n2,d1,d2,c;
char ch;
while(true)
{
cout<<"\nEnter numerator and denominator of 1st fraction:";
cin>>n1>>d1;
cout<<"\nEnter numerator and denominator of 2nd fraction:";
cin>>n2>>d2;
fraction f1(n1,d1);
fraction f2(n2,d2);
cout<<"Fraction 1: ";
f1.printfrac();
cout<<"\nFraction 2: ";
f2.printfrac();
cout<<"\n";
do
{
cout<<"\n Menu\n"<<"----------------\n"<<"1.Addition\n2.Multiplication\n3.Subtraction\n4.Division\n5.Copy using copy constructor\n6.Copy using assgnment operator\n";
cout<<"\nEnter which operation you want to perform:";
cin>>c;
switch(c)
{
case 1:
{
fraction res=f1+f2;
cout<<"\nResult of addition:";
res.printfrac();
break;
}
case 2:
{
fraction res=f1*f2;
cout<<"\nResult of multiplication:";
res.printfrac();
break;
}
case 3:
{
fraction res=f1-f2;
cout<<"\nResult of subtraction:";
res.printfrac();
break;
}
case 4:
{
fraction res=f1/f2;
cout<<"\nResult of division:";
res.printfrac();
break;
}
case 5:
{
cout<<"\nNew copy of 1st fraction:";
fraction res1=f1;
res1.printfrac();
cout<<"\nNew copy of 2nd fraction:";
fraction res2=f2;
res2.printfrac();
break;
}
case 6:
{
cout<<"\nNew copy of 1st fraction:";
fraction res1;
res1=f1;
res1.printfrac();
cout<<"\nNew copy of 2nd fraction:";
fraction res2;
res2=f2;
res2.printfrac();
break;
}
default:
cout<<"\nInvalid choice!!!";
}
cout<<"\nDo you want to exit from program(Y/N):";
cin>>ch;
if(ch=='Y'||ch=='y')
return 0;
cout<<"Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):";
cin>>ch;
}while(ch=='Y'||ch=='y');
}
return 0;
}


Output:
Enter numerator and denominator of 1st fraction:2
3

Enter numerator and denominator of 2nd fraction:-3
4
Fraction 1: 2/3
Fraction 2: -3/4

Menu
----------------
1.Addition
2.Multiplication
3.Subtraction
4.Division
5.Copy using copy constructor
6.Copy using assgnment operator

Enter which operation you want to perform:2

Result of multiplication:1/-2
Do you want to exit from program(Y/N):n
Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):y

Menu
----------------
1.Addition
2.Multiplication
3.Subtraction
4.Division
5.Copy using copy constructor
6.Copy using assgnment operator

Enter which operation you want to perform:4

Result of division:-8/9
Do you want to exit from program(Y/N):n
Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):n

Enter numerator and denominator of 1st fraction:4
5

Enter numerator and denominator of 2nd fraction:9
10
Fraction 1: 4/5
Fraction 2: 9/10

Menu
----------------
1.Addition
2.Multiplication
3.Subtraction
4.Division
5.Copy using copy constructor
6.Copy using assgnment operator

Enter which operation you want to perform:3

Result of subtraction:1/-10
Do you want to exit from program(Y/N):n
Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):y

Menu
----------------
1.Addition
2.Multiplication
3.Subtraction
4.Division
5.Copy using copy constructor
6.Copy using assgnment operator

Enter which operation you want to perform:1

Result of addition:17/10
Do you want to exit from program(Y/N):n
Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):y

Menu
----------------
1.Addition
2.Multiplication
3.Subtraction
4.Division
5.Copy using copy constructor
6.Copy using assgnment operator

Enter which operation you want to perform:5

New copy of 1st fraction:4/5
New copy of 2nd fraction:9/10
Do you want to exit from program(Y/N):n
Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):y

Menu
----------------
1.Addition
2.Multiplication
3.Subtraction
4.Division
5.Copy using copy constructor
6.Copy using assgnment operator

Enter which operation you want to perform:6

New copy of 1st fraction:4/5
New copy of 2nd fraction:9/10
Do you want to exit from program(Y/N):y


Discussions :
&#61548; Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.

&#61548; All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.

&#61548; We have used new for dynamically instanciate num and deno.

&#61548; A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed (const or not), which might be followed by parameters of any type (all having default values).

&#61548; A shallow copy of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied -- the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The default copy constructor and assignment operator make shallow copies.
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator, otherwise the copy will point to the original, with disasterous consequences. Here we have used deep copy method in this program.

&#61548; The general form of an operator function is,
&#61548; return type classname :: operator op(arglist)
&#61548; {
&#61569; Function body//task defined
&#61569; }
where return type is the type of value returned by the specific operation and op is the operator being overloaded. The op is preceded by the keyword operator. operator op is the function name.

&#61548; A friend function will have only one argument for unary operators and two for binary operators,while a member function has no arguments for unary operators and only one for binary operators. This is because the object used to invoke the member function is passed implicitly and therefore is available for the member function. This is not the case with friend functions. Arguments may be passed either by value or by reference.
&#61548;
&#61548; We have used inline functions in this program. An inline is an important feature in C++,that is commonly used with classes. These are short functions which are not actually called,rather their code is expanded in line at the point of each invocation.

&#61548; User should enter valid numerator and denominator otherwise the program may behave akwardly.

Back to main directory:  C++ Assignment    Software Practical


Get Free Programming Tutorials and Solved assignments