473,473 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

operator overloading

1 New Member
I need to write overloaded operators in C++ by reading their definitions from the input file.how can i write this code.
any idea?
The problem definition is this:

A definition starts with “> defineop”.
op is the name of the operator, which is one of the special characters { ~, !, @, #, $, %, ^, &, _ }
p1 and p2 are distinct lower case characters of English alphabet {a, b, ... , z}
expressionsare C like expressions made up of {p1, p2, (, ), +, ,*, /, defined operators, integers}
Results of 0 op p1 and p1 op 0 are given as expressions.
Precedence of operator is given as “prec = value1”. value1 is a number between 0 and 100. You
will use this precedence value while evaluating the expressions.
By default, * and / has precedence value of 20, + and – has precedence value of 10. Operators with greater precedences are evaluated earlier.
Associativity(*) of the operator is given as “assoc = value2”. value2 is either right or left. If two operators have same precedences, they will have same associativities.
Associativities of +, ,* and / are all left.
Adefinition ends with the keyword end.


> defineop $ ; a $ b = ( a + b ) * a ^ b ; 0 $ a = a + 1 ; a $ 0 = 0 ; prec = 7 ; assoc = left ; end
> defineop ^ ; a ^ b = a ^ ( b – 1 ) * a ; 0 ^ a = 0 ; a ^ 0 = 1 ; prec = 32 ; assoc = right ; end

// be careful! $ used the operator which is defined after its definition.
// For 0 op 0, use 0 op p1.

A unary operator is defined as follows:
> defineop op ; op p1 = expression1 ; op 0 = expression2 ; prec = value ; end
For example,
> defineop ~ ; ~ a = 0 – a ; ~ 0 = 0 ; prec = 45 ; end

After defining operators, some expressions made up of {(, ), +, ,
*, /, defined operators, integers}
are given. You should print the value of these expressions. Each expression is given as:
> evaluate ; expression ; end

for example input file is:

> defineop $ ; a $ b = ( a + b ) * a ^ b ; 0 $ a = a + 1 ; a $ 0 = 0 ; prec = 7 ; assoc = left ; end
> defineop ^ ; a ^ b = a ^ ( b - 1 ) * a ; 0 ^ a = 0 ; a ^ 0 = 1 ; prec = 32 ; assoc = right ; end
> defineop ~ ; ~ a = 0 - a ; ~ 0 = 0 ; prec = 45 ; end
> evaluate ; 3 * ( 2 + 2 ^ 3 ) ; end
> evaluate ; ~ 2 + 3 $ 2 ; end

output is printed as:
30
3
Nov 3 '05 #1
0 1715

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.