Exercise 1: This exercise is designed to present a class that has both a copy constructor and an assignment operator and

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Exercise 1: This exercise is designed to present a class that has both a copy constructor and an assignment operator and

Post by answerhappygod »

Exercise 1 This Exercise Is Designed To Present A Class That Has Both A Copy Constructor And An Assignment Operator And 1
Exercise 1 This Exercise Is Designed To Present A Class That Has Both A Copy Constructor And An Assignment Operator And 1 (476.96 KiB) Viewed 32 times
Code Below: PLEASE ANSWER IN C++
//CopyAssignTest.cpp
#include <iostream>
#include "MyClass.h"
using namespace std;
int main(int argc, char** argv)
{
// create object m1 using default constructor
MyClass m1;
// update data members
m1.setD(3.14159);
m1.setI(42);
m1.setS("This is a test");
m1.setIp(7);
cout << "m1 values:" << endl;
cout << '\t' << m1.getD() << ", " <<
m1.getI() << ", " << m1.getS()
<< ", " << m1.getIp() << endl;
// create object m2 from m1 using copy constructor
MyClass m2(m1);
cout << "m2 values:" << endl;
cout << '\t' << m2.getD() << ", " <<
m2.getI() << ", " << m2.getS()
<< ", " << m2.getIp() << endl;
// create object m3 from m1 using assignment operator
MyClass m3 = m1;
cout << "m3 values:" << endl;
cout << '\t' << m3.getD() << ", " <<
m3.getI() << ", " << m3.getS()
<< ", " << m3.getIp() << endl;
// update m2's data
m2.setD(1.7172);
m2.setI(100);
m2.setS("This is a NEW test");
m2.setIp(8);
// copy m2 to m1
m1 = m2;
cout << "m1 values:" << endl;
cout << '\t' << m1.getD() << ", " <<
m1.getI() << ", " << m1.getS()
<< ", " << m1.getIp() << endl;
// only update m2's data IP which is using dynamically allocated
memory
m2.setIp(23);
cout << "m1 values:" << endl;
cout << '\t' << m1.getD() << ", " <<
m1.getI() << ", " << m1.getS()
<< ", " << m1.getIp() << endl;
cout << "m2 values; last should be different:" <<
endl;
cout << '\t' << m2.getD() << ", " <<
m2.getI() << ", " << m2.getS()
<< ", " << m2.getIp() << endl;
return 0;
}
------------------------------------------------------------------------------------------
//MyClass.cpp
#include "MyClass.h"
#include <cassert>
using namespace std;
MyClass::MyClass() : i(0), d(0.0)
{
ip = new int;
*ip = 0;
}
MyClass::MyClass(const MyClass& orig) : ip(nullptr)
{
// Note that this is a new object; no dynamic memory has been
allocated
copy(orig);
}
MyClass& MyClass::operator=(const MyClass& rhs)
{
// we have seen this before: a = a is a legal assignment, and
shouldn't do anything
if (this != &rhs) {
copy(rhs);
}
return *this;
}
MyClass::~MyClass()
{
clear();
}
void MyClass::setI(int newI)
{
i = newI;
}
void MyClass::setD(double newD)
{
d = newD;
}
void MyClass::setS(string newS)
{
s = newS;
}
void MyClass::setIp(int newIp)
{
*ip = newIp;
}
int MyClass::getI() const
{
return i;
}
double MyClass::getD() const
{
return d;
}
string MyClass::getS() const
{
return s;
}
int MyClass::getIp() const
{
return *ip;
}
void MyClass::copy(const MyClass& other)
{
i = other.i;
d = other.d;
s = other.s;
// assert(ip == nullptr);
ip = new int;
*ip = *(other.ip);
}
void MyClass::clear()
{
i = 0;
d = 0.0;
s = "";
assert(ip != nullptr);
*ip = 0;
delete ip;
ip = nullptr;
}
------------------------------------------------------------------------------------------
//MyClass.h
#pragma once
#include <string>
using namespace std;
class MyClass
{
public:
// default constructor
MyClass();
// copy constructor
MyClass(const MyClass& orig);
// destructor
~MyClass(void);
// assignment operator
MyClass& operator=(const MyClass& rhs);
// setters
void setI(int newI);
void setD(double newD);
void setS(string newS);
void setIp(int newIp);
// getters
int getI(void) const;
double getD(void) const;
string getS(void) const;
int getIp(void) const;
private:
// a couple useful utility functions
void copy(const MyClass& other);
void clear(void);
// an assortment of test data members
int i; // primitive
double d; // primitive
string s; // object
int* ip; // primitive, pointer
};
Exercise 1: This exercise is designed to present a class that has both a copy constructor and an assignment operator and to illustrate the difference between deep and shallow copies. Please download and compile the code in Lab4/Exercise 1 (MyClass.h, MyClass.cpp, and CopyAssignTest.cpp). Then do the following (30 minutes coding): 1. Question: Look at the code; do you think that MyClass::operator=() is doing a deep or shallow copy? Put your answer in AnswersToLab4.txt. O Take Away: Know the difference between deep and shallow copy. 2. Question & Coding: There is a memory leak in this code. Find it and fix it. (Hint: use valgrind to check if you're not sure) 3. Coding: Is MyClass::setlp() implemented correctly? What exactly does it do? Fix any issues. Hint: Well- encapsulated code behaves correctly regardless of what the rest of the program does. What would happen if ip was not set before calling setlp()? 4. Coding: Fix all occurrences of issues with the same problem as we found in setlp() above. 5. Coding: Derive a new subclass of MyClass, name it "MySubClass" and define a private "int" type data member named "subClassData" in this new class. What do you need to do is to ensure that the correct copy constructor and assignment operators are called. Verify that your new class works correctly. O Take Away: When to and how to call super class constructor/methods
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply