Question

In: Computer Science

C++ code Derive a new subclass of MyClass, name it "MySubClass" and define a private "int"...

C++ code

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.

Take Away: When to and how to call super class constructors/methods

Here are source codes of Driver.cpp file, MyClass.h, and MyClass.cpp

CopyAssignTest.cpp file

#include

#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.h file

#pragma once

#include

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

};

MyClass.cpp file

#include "MyClass.h"

#include

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;

}

Solutions

Expert Solution

Please find the requested updated program below. Also including the screenshot of sample output.

Please provide your feedback
Thanks and Happy learning!

//MyClass.h file

#pragma once
#include
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
};

class MySubClass : public MyClass
{
private:
    int subClassData;
public:
    // default constructor
    MySubClass(){}

    //Copy constructor. Invoke the base class copy constructor
    MySubClass(const MySubClass& orig);

    //Assignment operator
    MySubClass& operator=(const MySubClass& rhs);

    void setSubClassData(int data);

    int getSubClassData() const;
};

//Main file

#include <iostream>
#include <string>
#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;

    //Test cases for the subclass
    // create object m1 using default constructor
    MySubClass ms1;

    // update data members
    ms1.setD(3.14159);
    ms1.setI(42);
    ms1.setS("This is subclass test");
    ms1.setIp(7);
    ms1.setSubClassData(303);

    cout << "ms1 values:" << endl;
    cout << '\t' << ms1.getD() << ", " << ms1.getI() << ", " << ms1.getS()
        << ", " << ms1.getIp() << ", "<< ms1.getSubClassData() << endl;

    // create object ms2 from ms1 using copy constructor
    MySubClass ms2(ms1);
    cout << "ms2 values:" << endl;
    cout << '\t' << ms2.getD() << ", " << ms2.getI() << ", " << ms2.getS()
        << ", " << ms2.getIp() << ", " << ms2.getSubClassData() << endl;

    // create object ms3 from ms1 using assignment operator
    MySubClass ms3 = ms1;
    cout << "ms3 values:" << endl;
    cout << '\t' << ms3.getD() << ", " << ms3.getI() << ", " << ms3.getS()
        << ", " << ms3.getIp() << ", " << ms3.getSubClassData() << endl;

    return 0;
}

//MyClass.cpp file

#include "MyClass.h"
#include
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;
}

//MySubclass methods

//Copy constructor. Invoke the base class copy constructor
MySubClass::MySubClass(const MySubClass& orig) : MyClass(orig), subClassData(orig.subClassData)
{}

MySubClass& MySubClass::operator=(const MySubClass& rhs)
{
    if (&rhs != this)
    {
        //Invoke the base class assignment operator
        MyClass::operator=(rhs);
        subClassData = rhs.subClassData;
    }

    return *this;
}

void MySubClass::setSubClassData(int data)
{
    subClassData = data;
}

int MySubClass::getSubClassData() const
{
    return subClassData;
}

Related Solutions

What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
//Question1: What is the name of the following lines in the data type class? private int...
//Question1: What is the name of the following lines in the data type class? private int intVariable; private float floatVariable; private String stringVariable; //Question2: What is the name of the following lines in the data type class? public DataTypeClass_Smith() { intVariable = 0; floatVariable = 0.0; stringVariable = “aString”; } //Question3: What is the name of the following lines in the data type class? public DataTypeClass_Smith( int intVar, float floatVar, String stringVar) { intVariable = intVar; floatVariable = floatVar; stringVariable...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This...
How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This function inserts a vehicle into the specified file. • Determine the RBN using the driver's hash function. • Use readRec to read the record at that RBN. • If that location doesn't exist or the record at that location has a szVehicleId[0] == '\0': o Write this new vehicle record at that location using writeRec. • If that record exists and that vehicle's szVehicleId...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT