006 Using the Assignment Operator

Instructions

In this exercise, you will write a program that uses the assignment operator = to change the value of an initialized variable as well as assign the value of one variable to another. 

Begin by declaring and initializing the integer variable num1 to the value of 13 

Now declare and initialize the integer variable num2  to the value 0 .

Use the assignment operator to change the value of num1 to 5 . 

Now use the assignment operator to assign the value of  num1  to  num2.

You can find my solution by clicking on the solution.txt file on the left pane. But please make sure you give it a go yourself first, and only check the solution if you really get stuck.

Test(s)

Test 1

#include "gtest/gtest.h"
#include "helpers/iohelper.h"
#include 

void assignment_operator();

namespace {

class Evaluate : public ::testing::Test {};


TEST_F(Evaluate, ExampleTest) {
    std::pair output = CAPTURE_OUTPUT(assignment_operator());
    std::string stdout = output.first;
    EXPECT_EQ("5 5", stdout) << "num1 and num2 should both be 5";

}

}  // namespace

Solution(s)

Solution 1

#include 
#include 
using namespace std;

void assignment_operator() {
    
    //----WRITE YOUR CODE BELOW THIS LINE----
    
    int num1 {13};
    int num2 {0};
    
    num1 = 5;
    num2 = num1;
   
   //----WRITE YOUR CODE ABOVE THIS LINE----
   //----DO NOT MODIFY THE CODE BELOW THIS LINE----
   
   cout << num1 << " " << num2;
}

Solution 2

//