By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

Operator Overloading Problem(->, <, >)

P: 55
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
Share this Question
Share on Google+
3 Replies


P: 55
I have typed wrong information. My class doesn't have any pointer.
Jun 13 '07 #2

P: 55
I am a C++ programmer from C.

Thanks.
Jun 13 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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

Post your reply

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