#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.
cList[5] = cl[5];does not copy/assign the entire array, only one element (which in this case is also one past the size ofcl, invoking undefined behavior) - UnholySheep