Question

12.24 HW2: Practice with Classes Objectives: Become familiar with developing a C++ class, to include developing:...

12.24 HW2: Practice with Classes

Objectives:

  • Become familiar with developing a C++ class, to include developing:
    • A constructor
    • An multiplication operator
    • A friend function (input operator)
  • Introduce various C and C++ functions for parsing user input

Assignment Details

This assignment consists of 6 parts.

Part 1: Using an IDE of your own choosing (Visual Studio is recommended), create a new project and copy the starter code

  • ComplexNumber.h
  • ComplexNumber.cpp and
  • TestComplexNumber.cpp

to this new project for developing and testing your solution.

Part 2: Complete the specification in ComplexNumber.h by adding function prototypes for:

  • A ComplexNumber constructor which takes as a parameter a single C-sytle string (i.e., const char *)

  • A multiplication operator as a member function which takes as a parameter a single constant reference to a ComplexNumber and returns a ComplexNumber

  • An input operator as a friend function which takes two input parameters

    • A reference to an istream and
    • A reference to a ComplexNumber and

    Initializes the ComplexNumber to value given in the istream and returns the modified istream

Part 3: Write implementations for the three new functions specified in Part 2. Your functions should adhere to the following requirements:

  • Your constructor initializes a ComplexNumber object using a C-string with one of the following forms:
    1. [+|-]realNumber
    2. [+|-][realNumber]i
    3. [+|-]realNumber(+|-)[realNumber]i
    • Please note:
      • | stands for OR,
      • [] stands for OPTIONALLY,
      • () stands for EITHER, and
      • realNumber stands for any valid real number literal, including numbers in signed or unsigned scientific notation.
    • For full credit your constructor must be able to read complex numbers matching any of the three forms.
    • For simplicity, your solution is not required to reject ill-formed complex numbers (i.e., numbers which do not match the specification above)
  • Your multiplication operator must be a member function using * as the operator and correctly multiply the this object and the ComplexNumber parameter and return the resulting ComplexNumber according to the definition for ComplexNumber multiplication from Wolfram.
  • Your input operator must be a friend function using >> as the operator and allow initialization of the ComplexNumber parameter using the input stream (e.g., typically cin).

These three functions are the only new functions you are required to add to ComplexNumber.cpp. While it is not necessary, you may add as many additional non-member or non-friend functions as you want to complete your solution.

Part 4: Test your implementations from Parts 2 and 3 using TestComplexNumber.cpp and various valid complex numbers.

Example Inputs and Results:

     
      Testing the string constructor with "1.23e-1+4.5i" results in the value 0.123+4.5i
     
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The contents of the file "ComplexNumber.h" is given below:-

//----------------------------------------------------------------------------------------------------------------

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/*
* File: ComplexNumber.h
* Author: vipul
*
* Created on 29 January, 2020, 8:14 PM
*/

#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H

#include <iostream>

using namespace std;

class ComplexNumber {
public:
ComplexNumber();
ComplexNumber(const char * str);
ComplexNumber(const ComplexNumber& orig);
ComplexNumber operator*(const ComplexNumber& c);
friend istream& operator>>(istream& din,ComplexNumber& c);
private:
double real,img;
};

#endif /* COMPLEXNUMBER_H */


//--------------------------------------------------------------------------------------

The contents of the file "ComplexNumber.cpp" are given below:-

//-------------------------------------------------------------------------------------------------

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/*
* File: ComplexNumber.cpp
* Author: vipul
*
* Created on 29 January, 2020, 8:14 PM
*/

#include <iostream>
#include <string.h>
#include <math.h>

#include "ComplexNumber.h"

using namespace std;

ComplexNumber::ComplexNumber() {
this->real = 0;
this->img = 0;
}

ComplexNumber::ComplexNumber(const char * str) {
  
double x,y;
char i;
if (std::cin>>x >>y>>i && i=='i')
{
this->real = x;
this->img = y;
}
else
{
std::cout<< "Invalid String!"<<endl;
exit(1);
}
}

ComplexNumber::ComplexNumber(const ComplexNumber& orig) {
this->real = orig.real;
this->img = orig.img;
}

ComplexNumber ComplexNumber::operator*(const ComplexNumber& c)
{
ComplexNumber temp;
temp.real = (this->real)*(c.real) - (this->img)*(c.img);
temp.img = (this->real)*(c.img) + (this->img)*(c.real);
return temp;
}

istream& operator>>(istream& din,ComplexNumber& c)
{
din>>c.real;
din>>c.img;
return din;
}

//-------------------------------------------------------------------------------------------------

The contents of the file "TestComplexNumber.cpp" are given below:-

//----------------------------------------------------------------------------------------------------

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/*
* File: main.cpp
* Author: vipul
*
* Created on 29 January, 2020, 8:12 PM
*/

#include <iostream>
#include <cstdlib>

#include "ComplexNumber.h"

using namespace std;

/*
*
*/
int main(int argc, char** argv) {
cout<<"Hello!"<<endl;
ComplexNumber c1("-10+15i");
  
return 0;
}

//----------------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
12.24 HW2: Practice with Classes Objectives: Become familiar with developing a C++ class, to include developing:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • need the code in .c format #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> struct _cplx double re,...

    need the code in .c format #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> struct _cplx double re, im; // the real and imaginary parts of a complex number }; typedef struct _cplx Complex; // Initializes a complex number from two real numbers Complex CmplxInit(double re, double im) Complex z = { re, im }; return z; // Prints a complex number to the screen void CmplxPrint(Complex z) // Not printing a newline allows this to be printed in the middle of...

  • C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...

    C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...

  • For this lab you will be creating a class representing the shape square. A Square is...

    For this lab you will be creating a class representing the shape square. A Square is a special case of a Rectangle where both sides have the same length. Rectangle has been provided for your use in this lab. A Rectangle has a height and a width as member variables, two constructors, two member functions, area() and perimeter(), and lastly, an input and output operator. Your Square class should publicly inherit from Rectangle. You will need to update the Constructors...

  • c++ 2) Complex Class A complex number is of the form a+ bi where a and...

    c++ 2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...

  • Please help with this assignment. Thanks. 1. Write a C++ program containing a class Invoice and...

    Please help with this assignment. Thanks. 1. Write a C++ program containing a class Invoice and a driver program called invoiceDriver.cpp. The class Invoice is used in a hardware store to represent an invoice for an item sold at the store. An invoice class should include the following: A part number of type string A part description of type string A quantity of the item being purchased of type int A price per item of type int A class constructor...

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT