SlideShare a Scribd company logo
C++ Assignment
Please read all the requirements and the overloading requiremets.
Here is what i have so far,
#include
using namespace std;
class ComplexNumbers
{
private:
double real;
double imaginary;
public:
void input ();
void output();
void setReal ( int );
int getReal ();
void setImaginary ( int );
int getImaginary();
ComplexNumbers();
ComplexNumbers(int);
ComplexNumbers(int, int);
friend bool operator == (ComplexNumbers , ComplexNumbers);
friend ComplexNumbers operator + (ComplexNumbers , ComplexNumbers);
friend ComplexNumbers operator - (ComplexNumbers , ComplexNumbers);
friend ComplexNumbers operator * (ComplexNumbers , ComplexNumbers);
friend ComplexNumbers operator - (ComplexNumbers);
};
void ComplexNumbers::setReal(int x)
{
real=x;
}
void ComplexNumbers::setImaginary(int y)
{
imaginary=y;
}
int ComplexNumbers::getReal()
{
return real;
}
int ComplexNumbers::getImaginary()
{
return imaginary;
}
//default constructor
ComplexNumbers::ComplexNumbers()
{
real=0;
imaginary=0;
}
//single parameterized constructor
ComplexNumbers:: ComplexNumbers(int z)
{
real=z;
}
//two parameterized constructor
ComplexNumbers::ComplexNumbers(int r,int u)
{
real = r;
imaginary= u;
}
void ComplexNumbers::input ()
{
int aReal(0),bImaginary(0);
cout<<"enter real no"<>aReal;
cout<<"enter imaginary"<>bImaginary;
real = aReal;
imaginary = bImaginary;
}
/* int real,imaginary;
cout<<"enter real no"<>real;
cout<<"enter imaginary"<>imaginary;*/
void ComplexNumbers::output()
{
cout<<"the complex no is"<
Solution
Your program is well-written. Apart from overloading the binary and unary operators. I have
modified your code and changed "int" to "double" in Constructors, and some methods.
FYI. Accessors are nothing but "get" methods in a class
Mutators are overloaded operators +,-,*. Means these operators are mutated over your
"ComplexNumbers" class.
The following is the modified program:
#include
using namespace std;
class ComplexNumbers
{
private:
double real;
double imaginary;
public:
void input ();
void output();
void setReal ( double );
double getReal ();
void setImaginary ( double );
double getImaginary();
ComplexNumbers();
ComplexNumbers(double);
ComplexNumbers(double,double);
bool operator == (ComplexNumbers&);
ComplexNumbers operator + (ComplexNumbers&);
ComplexNumbers operator - (ComplexNumbers&);
ComplexNumbers operator * (ComplexNumbers&);
ComplexNumbers operator - ();//unary minus
};
void ComplexNumbers::setReal(double x)
{
real=x;
}
void ComplexNumbers::setImaginary(double y)
{
imaginary=y;
}
double ComplexNumbers::getReal()
{
return real;
}
double ComplexNumbers::getImaginary()
{
return imaginary;
}
//default constructor
ComplexNumbers::ComplexNumbers()
{
real=0;
imaginary=0;
}
//single parameterized constructor
ComplexNumbers:: ComplexNumbers(double z)
{
real=z;
}
//two parameterized constructor
ComplexNumbers::ComplexNumbers(double r,double u)
{
real = r;
imaginary= u;
}
void ComplexNumbers::input ()
{
int aReal(0),bImaginary(0);
cout<<"enter real no"<>aReal;
cout<<"enter imaginary"<>bImaginary;
real = aReal;
imaginary = bImaginary;
}
/* int real,imaginary;
cout<<"enter real no"<>real;
cout<<"enter imaginary"<>imaginary;*/
void ComplexNumbers::output()
{
cout<<"the complex no is: "<getReal() * i.getReal());
temp.setImaginary( this->getImaginary() * i.getImaginary());
return temp;
}
ComplexNumbers ComplexNumbers::operator + (ComplexNumbers &i)
{
ComplexNumbers temp;
temp.setReal(this->getReal() + i.getReal());
temp.setImaginary( this->getImaginary() + i.getImaginary());
return temp;
}
ComplexNumbers ComplexNumbers::operator - (ComplexNumbers &i)
{
ComplexNumbers temp;
temp.setReal(this->getReal() - i.getReal());
temp.setImaginary( this->getImaginary() - i.getImaginary());
return temp;
}
bool ComplexNumbers::operator == (ComplexNumbers &i)
{
return( (this->getReal() == i.getReal()) && (this->getImaginary()== i.getImaginary()));
}
ComplexNumbers ComplexNumbers::operator - ()
{
this->setReal(-1 * this->getReal());
this->setImaginary( -1 * this->getImaginary());
return *this;
}
int main()
{
ComplexNumbers x,y,a,b,c;
x.input();
y.input();
a= x+y;
b = x-y;
c = x*y;
a.output();
b.output();
c.output();
if (x==y)
{
cout<< "they are equal"<

More Related Content

PDF
C++ Programming - 11th Study
DOC
oop Lecture 6
DOC
Pads lab manual final
PDF
C++ manual Report Full
PPTX
Pointer to Member Function.pptx pointer in c++
PDF
Operator overloading in C++
PDF
P3
PDF
#include iostream #include deque #include stdio.h   scan.pdf
C++ Programming - 11th Study
oop Lecture 6
Pads lab manual final
C++ manual Report Full
Pointer to Member Function.pptx pointer in c++
Operator overloading in C++
P3
#include iostream #include deque #include stdio.h   scan.pdf

Similar to C++ AssignmentPlease read all the requirements and the overloading.pdf (20)

DOCX
Oop project
PPT
OBJECTS IN Object Oriented Programming .ppt
PPTX
Lecture05 operator overloading-and_exception_handling
PDF
I wrote the following change it to having a header, main and cpp fi.pdf
PDF
C++ normal assignments by maharshi_jd.pdf
DOCX
Oops lab manual
PDF
M11 operator overloading and type conversion
DOCX
EN3085 Assessed Coursework 1 1. Create a class Complex .docx
PPT
Lecture5
PPT
Lecture17
PDF
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
PPTX
1.2 matlab numerical data
PDF
Oop05 6
PPTX
C++ Programming Basics.pptx
DOC
Cs2312 OOPS LAB MANUAL
DOCX
Friend function by sathya.docx
PPT
Overloading
Oop project
OBJECTS IN Object Oriented Programming .ppt
Lecture05 operator overloading-and_exception_handling
I wrote the following change it to having a header, main and cpp fi.pdf
C++ normal assignments by maharshi_jd.pdf
Oops lab manual
M11 operator overloading and type conversion
EN3085 Assessed Coursework 1 1. Create a class Complex .docx
Lecture5
Lecture17
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
1.2 matlab numerical data
Oop05 6
C++ Programming Basics.pptx
Cs2312 OOPS LAB MANUAL
Friend function by sathya.docx
Overloading

More from lakshmijewellery (20)

PDF
Write a shell command to substitute the first Nick to John in report.pdf
PDF
Why did the European empires in the Americas have such an enormously.pdf
PDF
What were the unintended consequences of the Cuban Missile Crisis.pdf
PDF
What process occurs when bile is mixed with fats How does this proc.pdf
PDF
What is OBDC , Is it a program and what is the difference between .pdf
PDF
This Capital includes O A. mineral resources. O B. the money in one.pdf
PDF
What are the essential characteristics of cloud computingSolutio.pdf
PDF
We live in a very complex and culturally diverse society. When we br.pdf
PDF
What are the values of the following expressions, assuming that n is.pdf
PDF
True or FalseA budget variance is the difference between expected.pdf
PDF
This level of measurement includes continuous measures A. Nominal; .pdf
PDF
Throughout the middle ages European philosophers believed in…a sci.pdf
PDF
The term intangible assets is used in accounting to denotea. suc.pdf
PDF
The methyl-accepting chemotaxis proteins of bacteriaa) integrate m.pdf
PDF
The lagging strand is replicated with a series of Okazaki fragments .pdf
PDF
The files etcpasswd and etcshadow can be manually edited. Why wo.pdf
PDF
The dividends received deduction Must exceed the applicable percentag.pdf
PDF
The chordae tendinae of the AV valves are anchored to the ___ of the .pdf
PDF
Read the case Kenny An Effective Supervisor and in a 2-3 page p.pdf
PDF
Q4. How are automated tools used in the maintenance of information .pdf
Write a shell command to substitute the first Nick to John in report.pdf
Why did the European empires in the Americas have such an enormously.pdf
What were the unintended consequences of the Cuban Missile Crisis.pdf
What process occurs when bile is mixed with fats How does this proc.pdf
What is OBDC , Is it a program and what is the difference between .pdf
This Capital includes O A. mineral resources. O B. the money in one.pdf
What are the essential characteristics of cloud computingSolutio.pdf
We live in a very complex and culturally diverse society. When we br.pdf
What are the values of the following expressions, assuming that n is.pdf
True or FalseA budget variance is the difference between expected.pdf
This level of measurement includes continuous measures A. Nominal; .pdf
Throughout the middle ages European philosophers believed in…a sci.pdf
The term intangible assets is used in accounting to denotea. suc.pdf
The methyl-accepting chemotaxis proteins of bacteriaa) integrate m.pdf
The lagging strand is replicated with a series of Okazaki fragments .pdf
The files etcpasswd and etcshadow can be manually edited. Why wo.pdf
The dividends received deduction Must exceed the applicable percentag.pdf
The chordae tendinae of the AV valves are anchored to the ___ of the .pdf
Read the case Kenny An Effective Supervisor and in a 2-3 page p.pdf
Q4. How are automated tools used in the maintenance of information .pdf

Recently uploaded (20)

PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Presentation on HIE in infants and its manifestations
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
A systematic review of self-coping strategies used by university students to ...
Supply Chain Operations Speaking Notes -ICLT Program
Chinmaya Tiranga quiz Grand Finale.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
01-Introduction-to-Information-Management.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Types and Its function , kingdom of life
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
human mycosis Human fungal infections are called human mycosis..pptx
VCE English Exam - Section C Student Revision Booklet
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
GDM (1) (1).pptx small presentation for students
Presentation on HIE in infants and its manifestations
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharma ospi slides which help in ospi learning
Anesthesia in Laparoscopic Surgery in India
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS

C++ AssignmentPlease read all the requirements and the overloading.pdf

  • 1. C++ Assignment Please read all the requirements and the overloading requiremets. Here is what i have so far, #include using namespace std; class ComplexNumbers { private: double real; double imaginary; public: void input (); void output(); void setReal ( int ); int getReal (); void setImaginary ( int ); int getImaginary(); ComplexNumbers(); ComplexNumbers(int); ComplexNumbers(int, int); friend bool operator == (ComplexNumbers , ComplexNumbers); friend ComplexNumbers operator + (ComplexNumbers , ComplexNumbers); friend ComplexNumbers operator - (ComplexNumbers , ComplexNumbers); friend ComplexNumbers operator * (ComplexNumbers , ComplexNumbers); friend ComplexNumbers operator - (ComplexNumbers); }; void ComplexNumbers::setReal(int x) { real=x; } void ComplexNumbers::setImaginary(int y) { imaginary=y;
  • 2. } int ComplexNumbers::getReal() { return real; } int ComplexNumbers::getImaginary() { return imaginary; } //default constructor ComplexNumbers::ComplexNumbers() { real=0; imaginary=0; } //single parameterized constructor ComplexNumbers:: ComplexNumbers(int z) { real=z; } //two parameterized constructor ComplexNumbers::ComplexNumbers(int r,int u) { real = r; imaginary= u; } void ComplexNumbers::input () { int aReal(0),bImaginary(0); cout<<"enter real no"<>aReal; cout<<"enter imaginary"<>bImaginary;
  • 3. real = aReal; imaginary = bImaginary; } /* int real,imaginary; cout<<"enter real no"<>real; cout<<"enter imaginary"<>imaginary;*/ void ComplexNumbers::output() { cout<<"the complex no is"< Solution Your program is well-written. Apart from overloading the binary and unary operators. I have modified your code and changed "int" to "double" in Constructors, and some methods. FYI. Accessors are nothing but "get" methods in a class Mutators are overloaded operators +,-,*. Means these operators are mutated over your "ComplexNumbers" class. The following is the modified program: #include using namespace std; class ComplexNumbers { private: double real; double imaginary; public: void input (); void output(); void setReal ( double ); double getReal (); void setImaginary ( double ); double getImaginary(); ComplexNumbers(); ComplexNumbers(double); ComplexNumbers(double,double);
  • 4. bool operator == (ComplexNumbers&); ComplexNumbers operator + (ComplexNumbers&); ComplexNumbers operator - (ComplexNumbers&); ComplexNumbers operator * (ComplexNumbers&); ComplexNumbers operator - ();//unary minus }; void ComplexNumbers::setReal(double x) { real=x; } void ComplexNumbers::setImaginary(double y) { imaginary=y; } double ComplexNumbers::getReal() { return real; } double ComplexNumbers::getImaginary() { return imaginary; } //default constructor ComplexNumbers::ComplexNumbers() { real=0; imaginary=0; } //single parameterized constructor
  • 5. ComplexNumbers:: ComplexNumbers(double z) { real=z; } //two parameterized constructor ComplexNumbers::ComplexNumbers(double r,double u) { real = r; imaginary= u; } void ComplexNumbers::input () { int aReal(0),bImaginary(0); cout<<"enter real no"<>aReal; cout<<"enter imaginary"<>bImaginary; real = aReal; imaginary = bImaginary; } /* int real,imaginary; cout<<"enter real no"<>real; cout<<"enter imaginary"<>imaginary;*/ void ComplexNumbers::output() { cout<<"the complex no is: "<getReal() * i.getReal()); temp.setImaginary( this->getImaginary() * i.getImaginary()); return temp; }
  • 6. ComplexNumbers ComplexNumbers::operator + (ComplexNumbers &i) { ComplexNumbers temp; temp.setReal(this->getReal() + i.getReal()); temp.setImaginary( this->getImaginary() + i.getImaginary()); return temp; } ComplexNumbers ComplexNumbers::operator - (ComplexNumbers &i) { ComplexNumbers temp; temp.setReal(this->getReal() - i.getReal()); temp.setImaginary( this->getImaginary() - i.getImaginary()); return temp; } bool ComplexNumbers::operator == (ComplexNumbers &i) { return( (this->getReal() == i.getReal()) && (this->getImaginary()== i.getImaginary())); } ComplexNumbers ComplexNumbers::operator - () { this->setReal(-1 * this->getReal()); this->setImaginary( -1 * this->getImaginary()); return *this; } int main() { ComplexNumbers x,y,a,b,c; x.input(); y.input(); a= x+y; b = x-y;
  • 7. c = x*y; a.output(); b.output(); c.output(); if (x==y) { cout<< "they are equal"<