473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

I want to overload operator with friend function using constructors.

Ajay Bhalala
119 64KB
Hello all,

I want to overload prefix and postfix increment(++) operators with the help of friend function. I also have to use the constructors for this. How can I do this? in C++

Please help me.
Dec 26 '14 #1

✓ answered by weaknessforcats

OK.

I assume that you know a friend function is a regular function that needs access to the class private data. You declare the function a friend in the class to get this access.

So, in your class make your private members public.

Now any function can access the private members.

Now write your operator++ overloads. Since the data is public, these functions can access the data.

After you get the functions working, go back and make the class data members private again.

Add your operator++ as a friend function in the class.

Recompile and your code should still work.

4 1393
weaknessforcats
9,208 Expert Mod 8TB
I need more information here.

Prefix and postfix operator overloads normally don't require friend functions.

Plus constructors for what? The postfix overload makes a copy but the compiler has a default constructor which works as long as the class has no constructors. Otherwise, the compiler will use the class constructors You see my point? The class constructor is present independently of this problem.
Dec 26 '14 #2
Ajay Bhalala
119 64KB
You are right, but it is our practice assignment to do the program of operator overloading with the friend function and using constructor.
I am SY BSC IT student. Our professor give us a task to overload the ++ prefix and postfix operator with the friend function. To use constructor is optional. But I must have to use friend function.
Dec 26 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
OK.

I assume that you know a friend function is a regular function that needs access to the class private data. You declare the function a friend in the class to get this access.

So, in your class make your private members public.

Now any function can access the private members.

Now write your operator++ overloads. Since the data is public, these functions can access the data.

After you get the functions working, go back and make the class data members private again.

Add your operator++ as a friend function in the class.

Recompile and your code should still work.
Dec 26 '14 #4
Ajay Bhalala
119 64KB
Thank you so much for your help. Its really work. And thanks for your reply also.
Dec 27 '14 #5

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

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
2
by: Tim Partridge | last post by:
I'm not very good with namespaces, so my problem is probably a simple one. I can't get the following to compile on gcc 3.3.1. It reports main.cc: In function 'std::ostream &...
5
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
11
by: fungus | last post by:
I have some classes which have a "writeTo()" function but no operator<<. I want to fix it so that they all have an operator<< (for consistency). Can I do something like this: template <class...
19
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use...
2
by: Mahain | last post by:
How we overload '=' operator using friend function in c++. if not why ????????????????????
5
by: Daniel T. | last post by:
The goal is to make a friend function of an inner template class... template < typename T > class Foo { public: class Bar { friend bool operator==( const typename Foo<T>::Bar& lhs, const...
1
by: visweswaran2830 | last post by:
Hi, I have a function in aspx.vb (code behind fucntion). Now I want to call this function using javascript... How can I call?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.