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...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
//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;...
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
[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:" <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT