473,657 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator Overloading Problem(->, <, >)

55 New Member
Hello all expert C++ programmer, i fairly new to C++ programming.

I have a class cellphone which contain dynamic pointer.

I have create (example)ten cellphone.

I want to ask user for the input and store in the list and finally sort the list according to the brand of cellphone.

I learn C++ by myself. Therefore, i hope someone can guide me in this matter. I not asking for solution but guideline.

Why i need overload ->, << , < or >> in order to manipulate my class ?

Below is my program :

Header File :

Expand|Select|Wrap|Line Numbers
  1. // Header File 
  2.  
  3.  
  4. /* Don't define user copy constructor and 
  5.    assignment operator if the class has
  6.    embedded object such as std::string
  7. */
  8.  
  9. #pragma once
  10.  
  11. #include <iostream>
  12. #include <string>
  13. #include<list>
  14.  
  15. using std::ostream;
  16. using std::string;
  17. using std::list;
  18.  
  19. class cellphone
  20. {
  21.     int phone_number;
  22.     string service;
  23.     string brand;
  24. public:
  25.     cellphone() : phone_number(0), 
  26.         service(), brand(){}
  27.     // User Define Constructor
  28.  
  29.     cellphone(const cellphone &nokia)
  30.         :phone_number(nokia.phone_number), 
  31.         service(nokia.service),
  32.         brand(nokia.brand){}
  33.     // Copy Constructor
  34.  
  35.     cellphone& operator=(const cellphone &nokia)
  36.     {
  37.         if (this != &nokia)
  38.         this->phone_number = nokia.phone_number;
  39.         this->service = nokia.service;
  40.         this->brand = nokia.brand;
  41.         return *this;
  42.     }
  43.     // Assignment Operator
  44.  
  45.     friend ostream& operator<<(ostream& output, const cellphone &nokia)
  46.     {
  47.         output << nokia.phone_number 
  48.             << nokia.service << nokia.brand;
  49.         return output;
  50.     }
  51.     // User Defined Insertion Operator
  52.  
  53.     void setPhone_Number();
  54.     void setService();
  55.     void setBrand();
  56.     void dispaly(list<cellphone> &);
  57.  
  58.     ~cellphone(){}
  59. };
  60.  
  61. int setNum_Phone();
  62.  
Exception Header File :
Expand|Select|Wrap|Line Numbers
  1. // Exception Header File
  2. #pragma once
  3.  
  4. #include<string>
  5.  
  6. using std::string;
  7.  
  8. class invalid
  9. {
  10.     string description;
  11. public:
  12.     invalid() : description(){}
  13.     invalid(string error) : description(error){}
  14.  
  15.     string getDescription()const{return description;}
  16.     ~invalid(){}
  17. };
  18.  
  19.  

Implementation File :
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<list>
  3. #include<exception>
  4. #include "List-1.h"
  5. #include "Invalid.h"
  6.  
  7. using namespace std;
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     int loop, result;
  12.     try
  13.     {
  14.         result = setNum_Phone();    
  15.     }
  16.     catch(const invalid &error)
  17.     {
  18.         cerr << error.getDescription();
  19.     }
  20.  
  21.     cellphone *lg = new cellphone [result];
  22.  
  23.  
  24.     for (loop=0;loop<result;loop++)
  25.     {
  26.         try
  27.         {
  28.             lg[loop]->setPhone_Number();
  29.         }
  30.         catch(invalid &any)
  31.         {
  32.             cerr << any.getDescription();
  33.         }
  34.  
  35.         lg[loop]->setService();
  36.         lg[loop]->setBrand();
  37.     }
  38.  
  39.     list<cellphone> nokia;
  40.     nokia.push_back(lg);
  41.  
  42.     lg.dispaly(nokia);
  43.  
  44. //    nokia.sort();
  45. /*
  46.     cellphone samsung(lg);
  47.     cellphone motorola;
  48.     motorola = lg;  
  49.     cout << motorola << "\n" << samsung;
  50. */    
  51.     delete [] lg;
  52.     return 0;
  53. }
  54.  
  55. int setNum_Phone()
  56. {
  57.     int number;
  58.     cout << "How many Cellphone : ";
  59.     cin >> number;
  60.     invalid error("Number of cellphone cannot zero");
  61.     if (number == 0)throw error;
  62.  
  63.     system("cls");
  64.     return number;
  65. }
  66.  
  67. void cellphone::setPhone_Number()
  68. {
  69.     cout << "Enter Phone Number : ";
  70.     cin >> phone_number;
  71.     while(cin.get() !='\n');
  72.     if (phone_number <0)
  73.     {
  74.         invalid error("Phone Number cannot zero");
  75.         throw error;
  76.     }
  77. }
  78.  
  79. void cellphone::setService()
  80. {
  81.     cout << "Enter Service : ";
  82.     getline(cin, service);
  83. }
  84.  
  85. void cellphone::setBrand()
  86. {
  87.     cout << "Enter Brand of Cellphone :";
  88.     getline(cin, brand);
  89. }
  90.  
  91. void cellphone::dispaly(std::list<cellphone> &nokia)
  92. {
  93.     list<cellphone>::const_iterator citera;
  94.  
  95.     for (citera=nokia.begin();citera!=nokia.end();citera++)
  96.     {
  97.         cout << "\n\n\n" << *citera;
  98.     }
  99. }
  100.  
  101.  
What is embedded object ?

I want to know more except string.

I guess it something like vector or list.
Thanks for your help.

Your help is greatly appreciated by me and others.
Jun 13 '07 #1
3 1898
Peterwkc
55 New Member
I have typed wrong information. My class doesn't have any pointer.
Jun 13 '07 #2
Peterwkc
55 New Member
I am a C++ programmer from C.

Thanks.
Jun 13 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
The short answer is that you do not need to overload operators to manipulate your class.

For example, to display an object of your class you could:
Expand|Select|Wrap|Line Numbers
  1. cellphone thephone;
  2. thephone.display();
  3.  
by writing a display method. However, if you want you display your class by:
[code=cpp]
Expand|Select|Wrap|Line Numbers
  1. cellphone thephone;
  2. cout << thephone << endl;
  3.  
then you will need to overload the << operator.

If you want to sort a bunch of cellphone objects and you use the STL sort, the sort will need to compare two cellphones. It will call the operator<. To use the sort, you would need an operator<.

I would start by not overloading any operators. Then add the overloads as I need them.
Jun 13 '07 #4

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

Similar topics

2
5876
by: victor75040 | last post by:
Before you all start flaming me, I am not a student and this is not for any homework. Just someone learing c++ on their own. I am now up to the chapter in my book that describes operator overloading. I just cannot find any explanation that clearly point out what the parts of the statement refer to. For example the book says: comp operator+(comp b)
16
3081
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 has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
4
1404
by: winbatch | last post by:
Hi, I'm trying to play with classes and I'm having trouble with a theoretical problem. I've create a 'Log' class, who creates and writes to log files. I was able to overload the << operator so that I could write to my log as follows: Log logOne("log.txt", true, "\n" );
51
3570
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data; Matrix() {}
7
1825
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class xydata with operator- overloaded: class xydata { public:
10
1400
by: Lee | last post by:
Does VB.NET support overloaded operators? For instance, if I create a complex number class, Complex, can I do code like this? Dim a As New Complex(1.2, 3.4) Dim b As New Complex(5.6, 7.89) b = b * 4 a = a + b I'm not sure if Operator Overloading is an OOP (and thus maybe .NET) feature, or a C++/C# language feature.
0
1662
by: PaperPilot | last post by:
While trying to copy data into the vector I get the following error messages: stl_algobase.h:247: error: no match for 'operator*' in '*__result' stl_algobase.h:249: error: no match for 'operator++' in '++__result' __result is the vector's OutputIterator the ecode segement that gives me this problem is: typedef std::vector< float > floatV;
3
3272
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, obj2 ;
22
3608
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float v);
8
2968
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, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8522
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2745
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 we have to send another system
2
1973
muto222
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.