Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code to use: my_int.cc C++ Const parameter modifiers, overloading operators, and

ID: 3821759 • Letter: C

Question

Code to use: my_int.cc

C++ Const parameter modifiers, overloading operators, and separate compilation 1 Introduction In this lab, you are going to get some practice using const modifiers, overloading operators as friend functions and in order to define a class so that it is an ADT you need to divide the program into separate files. By doing so the programmers who use the type do not have access to how values and operations are implemented. 1.1 Laboratory Preparation Create a directory called L inside your 2400 directory 2. const Parameter Modifier Purpose of this exercise is for you to experience the problems that you can encounter by inconsistent use of the const modifier parameter (pg,638). First copy the following program below: m int.cc Then read and understand the program. After that compile the program and check the error messages. Answer question #1a on the answer sheet. After correcting the problem recompile and run the program. Answer questions ib-ic on the answer sheet. You can find more information on this topic on pages 638 640 3. overloading operators overload the operator as a friend function to the unvoint class. Include the function declaration and the function definition. Also include the necessary statements in the main to test the function you have written. You will have to create two objects of nvainit type with values you choose. Then compare and see whether you're overloaded operator is working correctly. (Don't make any changes to the existing statements i the main function.. Don't forget to include the documentation for this function. Look at question 1d on the answer sheet for the point distribution. For more information, look at pages 644 646 in your text.

Explanation / Answer

1a & 1b)

Error Messages:

1)#include expects "FILENAME" or <FILENAME>

(need to include the header files).

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

2) in line 148 : we have to use "<<" operator with fout;

3) in line 153 : we have to use "input(cin)" overloader function instead of directly using "inp"

4) in line 173 : with static_cast conversion we have to explicitly give type to which we want to convert.

5) also implement "input()" and "get_int()" methods.

1c) Code goes as follows :

#include <iostream>
#include <stdbool.h>
#include <math.h>
using namespace std;

class my_int
{
public:
//*********************************************************
// Function: my_int
// Purpose: Constructor initializes the val to x
// Params:   x - the value for the val
// Calls:    none
// Uses:     none
//*********************************************************
my_int(int x);

//*********************************************************
// Function: my_int     
// Purpose: Constructor initializes the val to 0    
// Params:   none    
// Calls:    none                                  
// Uses:     none         
//*********************************************************
my_int();     

//*********************************************************
// Function: set
// Purpose: Sets the val to x
// Params:   x - the new value for the val
// Calls:    none
// Uses:     none
//*********************************************************
void set(int x);

//*********************************************************************
// Function: input
// Purpose: reads and stores a value from inp. if fin is a input
//           stream, then fin is already connected to a file.User enters
//           a value and ask the user to re-enter the data if the
//           user enters an incorrect value.
// Params:   inp -- the input stream
// Calls:    none
// Uses:     istream
//*********************************************************************
void input(istream& inp);

//*********************************************************************
// Function: output
// Purpose: display the val on fout. if fout is a output stream
//           then fout is already connected to a file
// Params:   fout -- the output stream
// Calls:    none
// Uses:     ostream
//*********************************************************************
void output(ostream& fout) const;

//*********************************************************
// Function: get_int
// Purpose: returns the val
// Params:   none
// Calls:    none
// Uses:     none
//*********************************************************
int get_int();

private:
int val; // Variable to hold value for class
};

//*********************************************************
// Function: is_prime
// Purpose: object num contains a valid positive value
//           returns true if num is prime; otherwise
//           returns false
// Params:   num - the value to be checked for prime
// Calls:    sqrt
// Uses:     cmath
//*********************************************************
bool is_prime(const my_int& num);


int main()
{
my_int value1;
   
value1.input(cin);

value1.output(cout);

if (is_prime(value1))
    cout<<" is a prime number ";
else
   cout<<" is not a prime number ";

return 0;
}


//*********************************************************
// Function: my_int
// Purpose: Constructor initializes the val to x
// Params:   x - the value for the val
// Calls:    none
// Uses:     none
//*********************************************************
my_int::my_int(int x)
{
   val = x;
}

//*********************************************************
// Function: my_int     
// Purpose: Constructor initializes the val to 0    
// Params:   none    
// Calls:    none                                  
// Uses:     none         
//*********************************************************
my_int::my_int()
{
val = 0;
}


//*********************************************************
// Function: set
// Purpose: Sets the val to x
// Params:   x - the new value for the val
// Calls:    none
// Uses:     none
//*********************************************************
void my_int::set(int x)
{
   val = x;
}


//*********************************************************************
// Function: output
// Purpose: display the val on fout. if fout is a output stream
//           then fout is already connected to a file
// Params:   fout -- the output stream
// Calls:    none
// Uses:     ostream
//*********************************************************************
void my_int::output(ostream& fout) const
{
fout<<" The value is equal to "<< val;
   while (val <= 1)
   {
    cout<<"Entered an Invalid value ";
    cout<<"Enter a positive value greater than 1 ";
    input(cin);
   }
}

      
//*********************************************************
// Function: is_prime
// Purpose: object num contains a valid positive value
//           returns true if num is prime; otherwise
//           returns false
// Params:   num - the value to be checked for prime
// Calls:    sqrt
// Uses:     cmath
//*********************************************************
bool is_prime(const my_int& num)
{
double limit;
int n;      //divisor
bool prime= true;

limit = sqrt(static_cast<int>(num.get_int()));

n = 2;

while (n <= limit && prime)
{
    if (num.get_int() % n == 0)
     prime = false;
    else
     n++;
   }
   return prime;
}

void my_int::input(istream& inp) {

    inp>>val;
}

int my_int::get_int() {
   input(cin);
   return val;  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote