0
votes
#include <iostream>
#include <string>
#include "coin.h"
#include "purse.h"
//include necessary files

using namespace std;
/*
 Object Composition:  An object can comprise of many other objects; eg handphone has battery, sim card, sd card, etc
  To program object composition; we declare a data member (variable) that is of other Class type;
 */

int main(){
    //   i.Create an array of Coin 5 objects
        Coin a(5),b(10),c(20),d(50),e(100);
        Coin coinsArray[5]; 
        coinsArray[0] =a;
        coinsArray[1] =b;
        coinsArray[2] =c; 
        coinsArray[3] =d; 
        coinsArray[4] =e; 



    //ii.Create an instance of Purse object with the array in i)
        Purse purse = Purse(coinsArray ,"John");

    //iii.Compute and display the total value stored in the Purse object in ii).
    cin.ignore();
}


#include <iostream>
#include <string>
#include "coin.h"
#include "purse.h"
using namespace std;

//write the definition for the Purse class

Purse::Purse(Coin cl[5], string o)
{
    cList[5] = cl[5];
    owner = o;
}

    string Purse::getOwner()
    {
    return owner;
    }

    void Purse::setOwner(string o)
    {
      owner =o;
    }

    void Purse::displayCoins()
    {
       cout<< "Display";
    }


    int Purse::computeTotal()
    {
     int total = 6;

         return total;
        }


#pragma once
#include <iostream>
#include <string>
#include "coin.h"
using namespace std;
/* Object Composition:  An object can comprise of many other objects; eg handphone has battery, sim card, sd card, etc
  To program object composition; we declare a data member (variable) that is of other Class type;
  Purse contains Coin objects - this is object composition
 */
class Purse
{
  private:
    Coin cList[5];
    string owner;
  public:
    Purse(Coin [] ,string="");
    string getOwner();
    void setOwner(string o);
    void displayCoins();
    int computeTotal();



};

Hi, I have created 5 Coin array objects to put into Purse object but I keep getting the unhandled exception. Have anyone encountered this error? The error is when i instantiate Purse.

First-chance exception at 0x5240ad7a (msvcp100d.dll) in CoinProject.exe: 0xC0000005: Access violation reading location 0xccccccd0. Unhandled exception at 0x5240ad7a (msvcp100d.dll) in CoinProject.exe: 0xC0000005: Access violation reading location 0xccccccd0.

1
Careful, C style arrays have other semantics than the rest of the objects (in your case, they decay, and those pointers can be assigned as usual) - Rakete1111
cList[5] = cl[5]; does not copy/assign the entire array, only one element (which in this case is also one past the size of cl, invoking undefined behavior) - UnholySheep
Don't use C arrays. Use std::vector or std::array. - user2672107
Do you stick your coins in an array and than that array into your purse? I put my coins directly into my purse. - user2672107
Purse purse = Purse(coinsArray ,"John"); @manni66 This is what the instructor taught us. - gonggong

1 Answers

1
votes

The constructor of the Purse does not do what you intend to do:

Purse::Purse(Coin cl[5], string o)
{
    cList[5] = cl[5];
    owner = o;
}

What's the problem ?

The array cl is not passed by value but by reference. You define here that the array has a size of 5 elements.

The expression cList[5] = cl[5] says that the item number 5 of the array cl (so the 6th element of this array of 5 elements) is to be copied in the item number 5 of the array cList (so also the 6th element of an array of 5 elements).

This is undefined behavior: you are not allowed to access the 6th element of cl and cList arrays which have each only 5 elements indexed from 0 to 4. Depending on your compiler, this can for example lead to a crash of your programme, or to memory corruption, or any kind of other weird things.

How to solve it ?

If you've already learned about loops, use a loop to copy each element of the array. If not, copy the elements one by one (starting at 0).

P.S.: And tell your teacher that you have heard that vectors are much better than arrays (i.e. they can be copied in a single instruction).