C++ :: Rational Number Class Implementation - Overloaded Operators

Sep 13, 2013

I'm implementing a rational number class:

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

typedef int64_t RAT_INT;
struct RAT{
RAT_INT Num, Den;

RAT(RAT_INT num = 0, RAT_INT den = 1){
Num = num;
Den = den;

[Code].....

Two questions:
1) In the second line in main, how does C++ know to convert 2 to the appropriate RAT?
2) Is it possible to make the third line in main valid without adding global operators for all the member operators to support plain integers?

View 5 Replies


ADVERTISEMENT

C/C++ :: Open A File And Use Overloaded Operators For Complex Number Class

Apr 10, 2015

This code is meant to open a file and use overloaded operators for a complex number class. I am getting a lot of errors in my class declaration/definition but I am not sure why.

#include <iostream>
#include <cmath>
#include <fstream>
class CN {
public:
double real;
double im;

[Code] ....

View 5 Replies View Related

C++ :: Rational Number Using Class

Nov 1, 2013

I complete this program, but the only thing is that it won't execute the program.the other problem is the program is not running as it is suppose to.

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cctype>
using namespace std;
class Rational

[code]....

View 1 Replies View Related

C/C++ :: Rational Number Class?

Mar 5, 2015

Assignment to make a rational numbers class as follows:

create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form, and avoids negative denominators.

Overload the addition, subtraction, multiplication and division operators for this class.

Overload the relational and equality operators for this class. My code so far is as follows:

header file:

#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>

[Code].....

I'm getting an error:

Build started: Project: 11.10rationalnumberclass, Configuration: Debug Win32 ------
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Rational::setFraction(int,int)" (?setFraction@Rational@@QAEXHH@Z) referenced in function _main
1>C:UsersJacobDocumentsVisual Studio 2010ProjectsComplexnumbers11.8projectDebug11.10rationalnumberclass.exe : fatal error LNK1120: 1 unresolved externals

part of the problem might be that I cannibalized another program dealing with complex numbers to make this. While I don't need this class to be perfect, I'm not sure I entirely understand everything about classes and overloading yet.

View 2 Replies View Related

C++ :: Rational Number Class - Handling Numbers Of Different Denominators

Apr 6, 2014

My assignment is to handle rational numbers of different denominators...

Develop rational number class that can
•add
•subtract
•multiply
•divide
•reduce to simplest form
Your class must be able to handle rational numbers of different denominators

This is the errors I am getting

error C4716: 'Rational::addition' : must return a value
error C4716: 'Rational::multiplication' : must return a value
error C4716: 'Rational::division' : must return a value
error C4716: 'Rational::subtraction' : must return a value

View 1 Replies View Related

Visual C++ :: Adding Rational Number Using Only Class Functions

Feb 22, 2015

I'm having trouble finishing this program. What I'm trying to do is create a class for Rational numbers. Get input from user for denominator and numerator. Store the numbers without a sign, and for the sign store it separately in char. I'm not supposed to use accessor functions. The part that I can't seem to know how to do is use the info that was stored in the addUp function. This function need to add two rational numbers. i need to make this function general enough that it can handle two fractions, or a fraction and a whole number, or two whole numbers. What I already have here is readin function which reads in the numerator and denominator. setnumerator and setdenominator to assign positive values. setsign should get the sign of the fraction. Finally addUp should addUp which I explained before. I have some ideas about writing the tests, but can't seem to know how to implement it to the program. The main program is still empty but all I'm supposed to do there is call the class functions.

Code:
#include <iostream>
using namespace std;
class Rational {
private:
int numerator, denominator;
char sign;

[Code] .....

View 14 Replies View Related

C++ :: Templates / Lambdas And Overloaded Operators

Feb 16, 2015

What I'm trying to do is create a class for constructing an 'op tree' for parsing infix notation.

I started with a base class that uses a map of lambdas to actually calculate the operations (since they are mostly 1 line functions) of passed in integer or float values.

This base class just uses a templated T type as the lvalue and rvalue. I realized though that if I overload the math operators, +, -, etc.. I could also use the class itself as a type for the lvalue and rvalue. This lead me to think I could easily create the op tree by using Operation class members themselves as operands, which I think makes sense but I'm having some trouble expressing the code.

Heres what I have thus far

Code:
#include <map>
#include <string>
#include <algorithm>
#include <iostream>

namespace Calc {

[Code] .....

Example, if you look at the main() function I create normal operations easily with integer values. I then try to create a "tree" operation that includes 2 sub-operations as it's rvalue and lvalue, that is where I'm having some conceptual problems as far as implementing the code to do that.

View 2 Replies View Related

C++ :: Output Is Printing Twice With Overloaded Operators

Feb 16, 2013

I'm having an issue with output, luckily everything else works!! I'm working with Mixed Numbers and operations on them. So, here's the code I'm testing with:

int main() {
Mixed m1(5), m2(1,1,1), m5(2,2,3);
cout << "m1+m2= " << m1+m2 << endl;
cout << "m1 + 10=" << m1+10 << endl;
cout << "m1 - 10=" << m1-10 << endl;
return 0;
}

And here is the output for that code:

File name: fract.h

#ifndef fract_H
#define fract_H

#include <iostream>
using namespace std;

const int DEFAULT_VAL = 0;

[Code] ....

View 2 Replies View Related

C++ :: Calling Overloaded Operators In Main

Jan 10, 2013

I believe I have the syntax correct but I'm having difficulty calling my overloaded == operator in main (last snip-it of code). Below are several files explaining the code.

Commission.h here is where the friend bool operator == exists and I believe I have it initialized correctly.

#ifndef COMMISSION_H_INCLUDED
#define COMMISSION_H_INCLUDED
using namespace std;
class Commission {

public:
Commission();
Commission(int, double, double, double );
~Commission();

[Code] .....

View 3 Replies View Related

C++ :: Vector Of Classes With Overloaded Operators Not Compiling

Dec 6, 2014

I am working on a school project and am stuck in the debugging. I am compiling with g++ and am using c++11.

Error message

In file included from /usr/include/c++/4.7/algorithm:63:0,
from date.h:7,
from date.cpp:1,
from schedule.h:1,
from schedule.cpp:1,
from date_driver.cpp:1:

[Code] ....

Here are my overloaded operators for my date class.

bool Date :: operator == (Date otherDate){
if (((otherDate.getDay () == getDay ()) && (otherDate.getMonth () == getMonth ())) && (otherDate.getYear () == getYear ()))

[Code] .....

View 1 Replies View Related

C++ :: Ambiguous Call On Overloaded Variadic Template Operators

Oct 30, 2013

When compiling the code

#include "tensor.h"
int main() {
Tensor<2,-2> m = {{1,2},{1,3}};
Tensor<2> v = {1,5};
std::cout<<m*v<<"

[Code] ....

Why do I get an ambiguity and why is not the wanted operator*-overload (the last one in the tensor.h file) not even mentioned as one of the candidates? Is it clear what I want to do? And if so, what can I do to make the call unambiguous?

View 3 Replies View Related

C++ :: Rational Number Does Not Print As Reduced

Apr 10, 2013

The program works, but the rational number doesn't print as reduced. I guess i have to call the reduction function, but i'm not sure where

the output that i get is:
2/6 + 7/8 = 58/48
58/48 = 1.20833

2/6 - 7/8 = -26/48
-26/48 = -0.541667

2/6 x 7/8 = 14/48
14/48 = 0.291667

2/6 / 7/8 = 16/42
16/42 = 0.380952

the output that i suppose to get is:
1/3 + 7/8 = 29/24
29/24 = 1.20833

1/3 - 7/8 = -13/24
-13/24 = -0.541667

1/3 x 7/8 = 7/24
7/24 = 0.291667

1/3 / 7/8 = 8/21
8/21 = 0.380952

here is my header file

//Prevent multiple inclusions of header
#ifndef RATIONAL_H
#define RATIONAL_H
//Rational class definition
class Rational {

[Code] .....

View 3 Replies View Related

C++ :: Use Class Structure To Create Program That Reads In Two Rational Numbers?

Nov 4, 2013

how to use a Class structure to create a program that reads in two rational numbers and adds them, subtracts, multiplies, and divides them.

View 3 Replies View Related

C++ ::  Overloaded Assignment Operator For Class Template Objects?

May 23, 2013

I designed a class template to create unique arrays. I was able to successfully input data to and output data from my array objects, irrespective of the datatype. However, I can't for the life of me fathom why my overloaded assignment operator worked perfectly well only for integer datatype and not for double/string datatypes.

Here is the class definition:

template <class dataType>
class myArray {
public:
void setArrayData();

[code]....

And here is the definition of the overloaded assignment operator:

template<class dataType>
const myArray<dataType>& myArray<dataType>::operator=(const myArray<dataType>& rightArray) {
int i;
if(this != &rightArray) {
delete [] arrayPtr;

[Code] ....

And here is my main function that tests the operations on objects of the class:

int main(){
//object declarations
myArray<double> list(5); //a single-parameter object declaration of class myArray
myArray<double> myList(2,13); //a two-parameter object declaration of class myArray

[code]....

The problem I'm having starts from where the assignment operator is being tested: for double and string datatypes, the upper input/output section works fine, but the assignment section freezes the display until the program execution is manually terminated!

View 19 Replies View Related

C/C++ :: Number Conversions Using Bitwise Operators

Feb 13, 2014

I have a project assignment for school to write a program that does number conversions using bitwise operators. The premise is that the user enters a number with one of three letter prefixes -- Q1232, O6322, H762FA, etc. -- and the program will take that number and convert it to the other two number bases. Q is for quarternary, O is for octal, and H is for hexadecimal. The transformations should be done using bitwise operators and bit shifting. I am guessing I need to scan the number, convert it to binary, then convert it to the other two bases.

However, I am completely new to bitwise operators and bit shifting, so how to convert numbers of different bases to binary and then binary to other bases using these bit and bitwise functions. I don't have much code done yet, since I am still unsure of how to approach it, but I'll post what little I have.

Here it is:

#include <stdio.h>
#include <string.h>
int main() {
char numType;
printf("
The user will enter a number up to 32 digits in quarternary
");
printf("(base 4), octal (base 8), or hexadecimal (base 16). If in
");

[Code] ....

I figure in each case I can write a function that converts the entered number to binary, then maybe two more functions that convert said binary number to the other bases. For default in the switch I will tell the user they entered an invalid number. I don't have the program looping until the user types 'EXIT' yet, but I will once I figure out anything about these bitwise operators.

View 2 Replies View Related

C++ :: Significance Of Assigning A Number Using Bitwise Operators

May 22, 2013

What is the difference between at performance level, if any, between the following cases, during assignment?

case 1: #define Value_16 16

and

case 2: #define Value_16 (1<<4)e.

View 2 Replies View Related

C++ :: Class Template Friend Operators

Sep 19, 2013

The code below doesn't compile. Two things to clear up:

1) I know x is going to be garbage.
2) I used the same type name label ElementType.

#include <iostream>
#include <ostream>
template <typename ElementType>
class Example {

[Code] .....

View 3 Replies View Related

C++ :: Arithmetic Operators For Custom Class?

Oct 3, 2014

I have made a custom class matrices class which allows me to add, multiply, subtract (etc.) matrices. I have the following method for multiplication but I get an error that says

'invalid use of 'this' outside of a non-static member function'

How can I refer to the current instance without getting this error.

void operator *(Matrices& matrix2) {
this.multiplyMatrix(matrix2);
}

View 2 Replies View Related

C++ :: Force Derived Class To Overload Certain Operators

Dec 14, 2014

Is is possible to force derived classed to overload certain operators, like we do with pure virtual functions?

Is this possible to dynamically bind objects to their respective overloaded operators?

I am getting errors with undefined references to my base class vtable when I hackly try to overload: Code: operator+ I am not sure whether this is possible.

View 7 Replies View Related

C++ :: Arithmetic Operators Overloading For Class With Pointer

Nov 11, 2014

I am stucked in a problem of overloading arithmetic operators such as "+,*" for a class in the form

class Point {
int N; // dimension of the point
double *Pos; // length of N
}

My assign operator is :
Point& Point::operator= (const Point& pt) {
N= pt.N;
if(Pos == NULL) Pos = new double[N];
memcpy(Pos, pt.Pos, N*sizeof(double));

[Code] ....

The add operator "+" is:
Point operator+( const Point& pt1, const Point& pt2 ) {
Point ptr = Point(pt); // this is a constructor
for (int i=0; i<pt1.N; i++) ptr.Pos[i] += pt2.Pos[i];
return ptr;
}

Based on the above overloading, What I am going to do is :

P = alpha*P1 + beta*P2; // alpha and beta are double constants, P1 and P2 are Points objes

It is ok with Intel C++ 14.0 compiler, but does not work with the microsoft visual c++ 2012 compiler in debug mode in visual studio 2012.

I stepped in those operators and found that visual c++ compiler deconstructs the ptr in operators "*" and "+" before its return while intel c++ finished the operation P = alpha*P1 + beta*P2; and delete those ptrs at last.

Portability of my operator overloading is worse. How to get those arithmetic operators overloading for class with pointers in it.

View 3 Replies View Related

C/C++ :: Overload Two Different Operators One Inside Class And Other Outside Using Friend

Nov 23, 2014

I'm trying to understand the basics of oop ...

#include <iostream>
using namespace std;
template <typename T>
class max_vector {
private:
T* elemente;
int lungime;

[Code] ....

The purpose of this program is to overload two different operators one inside the class, and the other one outside using friend. The problem is that i get 1 error at the '*' one.

View 1 Replies View Related

Visual C++ :: Class Fraction - Overloading Operators

Sep 25, 2013

the question am having problems with..

1.Write a class function that defines adding, subtracting, multiplying and dividing fractions by overloading standard operators for the operations.

2. Write a function member for reducing factors and overload I/O operators to input and output fractions. how would i set this up?

View 5 Replies View Related

C/C++ :: Fibonacci Number Sequence Implementation?

Jun 19, 2013

I need to implement the fibonacci number sequence. Here is an example output of the program:

Server:Waiting for connection
Server:Receive connection from client
Client:10
Server:Print 10 Fibonacci
Server:1 1 2 3 5 8 13 21 34 54
Client:1 1 2 3 5 8 13 21 34 54

if Client send value of 5, the output would be 1 1 2 3 5

View 2 Replies View Related

C++ :: Number Of Characters / Operators / Uppercase Letters And Numerical Digits

May 5, 2014

I have to code a simple program who determining the number of Characters (A character could be any alphabets, digits, punctuation marks, or special , Operators ( Operators are those symbols that are used in mathematica expression, such as,'+', '*', '/', '-', and so on.), Uppercase letters (Uppercase characters are those from A..Z) and Numerical digits ( A digit is any of the Hindu-Arabic numerals from 0..9). Why the output is wrong!

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std ;
int main() {
char text;

[Code] .....

This is my input file This is a possible factorial function in a programming language called LISP

(defun factorial (n)
(if (< n 2)
1
(* n (factorial (1- n)))))

This is my output:

The number of characters = 113
The number of operators = 3
The number of numerical digits = 3
Uppercase letters = 5

I think that "characters" is wrong, but I do not know why !

View 4 Replies View Related

C++ :: Implementation Of Named Class

Sep 12, 2014

I am trying to implement some kind of named class. It would look something like this:

class MyClass {
virtual std::string getName() = 0;
};

And now (what doesn't pass the compilation)

template <std::string NAME> class MyNamedClass {
std::string getName() { return NAME;}
};

And so every time I would like to have a class with a name, I could just do the following:

FinalClass : public MyNamedClass<"FinalClass">{};

The idea is not to have to always reimplement getName(), and just do it concisely in the declaration.

View 7 Replies View Related

C++ :: Overloading Binary And Assignment Operators In Vector Class

Feb 5, 2013

I am making a vector class and am having some problems creating the overloaded arithmetic operators and assignment operators.

Here is an example of my "+=" code as it stands the errors are similar/the same for the other operators except "=" operator which works fine:

Vector3& Vector3::operator+=(const Vector3 &rhs) {
Vector3 tmp = *this;
Set(tmp.getX() + rhs.getX(), tmp.getY() + rhs.getY(), tmp.getZ() + rhs.getZ());
return *this;
}

I have tried a lot of different approaches ad always get the error:

error: passing 'const Vector3' as 'this' argument of 'double Vector3::getX()' discards qualifiers
error: passing 'const Vector3' as 'this' argument of 'double Vector3::getY()' discards qualifiers
error: passing 'const Vector3' as 'this' argument of 'double Vector3::getZ()' discards qualifiers

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved