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 : -
// Header File
-
-
-
/* Don't define user copy constructor and
-
assignment operator if the class has
-
embedded object such as std::string
-
*/
-
-
#pragma once
-
-
#include <iostream>
-
#include <string>
-
#include<list>
-
-
using std::ostream;
-
using std::string;
-
using std::list;
-
-
class cellphone
-
{
-
int phone_number;
-
string service;
-
string brand;
-
public:
-
cellphone() : phone_number(0),
-
service(), brand(){}
-
// User Define Constructor
-
-
cellphone(const cellphone &nokia)
-
:phone_number(nokia.phone_number),
-
service(nokia.service),
-
brand(nokia.brand){}
-
// Copy Constructor
-
-
cellphone& operator=(const cellphone &nokia)
-
{
-
if (this != &nokia)
-
this->phone_number = nokia.phone_number;
-
this->service = nokia.service;
-
this->brand = nokia.brand;
-
return *this;
-
}
-
// Assignment Operator
-
-
friend ostream& operator<<(ostream& output, const cellphone &nokia)
-
{
-
output << nokia.phone_number
-
<< nokia.service << nokia.brand;
-
return output;
-
}
-
// User Defined Insertion Operator
-
-
void setPhone_Number();
-
void setService();
-
void setBrand();
-
void dispaly(list<cellphone> &);
-
-
~cellphone(){}
-
};
-
-
int setNum_Phone();
-
Exception Header File : -
// Exception Header File
-
#pragma once
-
-
#include<string>
-
-
using std::string;
-
-
class invalid
-
{
-
string description;
-
public:
-
invalid() : description(){}
-
invalid(string error) : description(error){}
-
-
string getDescription()const{return description;}
-
~invalid(){}
-
};
-
-
Implementation File :
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.
3 1898
I have typed wrong information. My class doesn't have any pointer.
I am a C++ programmer from C.
Thanks.
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: -
cellphone thephone;
-
thephone.display();
-
by writing a display method. However, if you want you display your class by:
[code=cpp] -
cellphone thephone;
-
cout << thephone << endl;
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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)
|
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.
|
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" );
|
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() {}
|
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:
| |
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.
|
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;
|
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 ;
|
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);
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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();...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |