Class receipt c++


Joined
Nov 7, 2020
Messages
3
Reaction score
0
Hello can someone help with this please

Write a receipt class.
Class to contain as data members
• The name of the store
• Array of prices of purchased goods, and member functions:

• Constructors

• get () and set () - for prices to work with individual elements of the array

• show () - displays a list of prices, the goods to be named product # 1, product # 2, etc.

• total () - determines the total price of the goods

• remove () - removes a product from the list (according to its index)

• discount () - reduces the price of goods by a certain percentage (the index of the goods in the array and the percentage are submitted as parameters)

• other member functions if desired

In the main function, illustrate how to work with the methods of the receipt class.
 
Last edited by a moderator:
Ad

Advertisements

Joined
Nov 7, 2020
Messages
3
Reaction score
0
Where is your main method?
That is the only think i have, but it's hard for me to call them in main


#include <iostream>
#include <string>
using namespace std;

class Receipt {
private:
string storeName;
double purchasedGoods {2};
double price[purchasedGoods];

public:

Receipt(string storeName, double[] purchasedGoods) {
this->storeName = storeName;
this->purchasedGoods = purchasedGoods;
}

double getpurchasedGood(int i) {
return purchasedGoods;
}

void setpurchasedGood(double price, int i) {
purchasedGoods = price;
}

void show() {

string str = "";
for (int i = 0; i < purchasedGoods.length; i++) {
str += "product #" + i + " " + purchasedGoods + "\n";
}

cout<<str<<endl;
}

double total() {
double total = 0;
for (int i = 0; i < purchasedGoods.length; i++) {
total += purchasedGoods;
}
return total;
}

void remove(int index) {
double[] newList = new double[purchasedGoods.length - 1];
int g = 0;
for (int i = 0; i < purchasedGoods.length; i++) {
g++;
if (!(i == index)) {
newList[g] += purchasedGoods;
} else {
g--;
}
}
}

void discount(int index, int percentage) {
purchasedGoods[index] = purchasedGoods[index] * ((double) percentage / (double) 100);
}


int main(){

Receipt stock ("Sunny Market","bread","olive oil",1.20,2.80);















}
 
Ad

Advertisements

Joined
Apr 25, 2017
Messages
169
Reaction score
23
In init main, constructor expect to have 5 parameters, but you only pass two, that is wrong.

Shouldn't it be something like this?

C++:
class Receipt {
private:
    string storeName;
    string itemName;
    string secondItemName;
    double price;
    double secondItemPrice;
    double purchasedGoods{2};
    double price[purchasedGoods];

public:
    Receipt(string storeName, string itemName, string secondItemName, double price, double secondItemPrice) { // need to have 5 too
        this->storeName = storeName;
        this->itemName = itemName;
        this->secondItemName = secondItemName;
        this->price = price;
        this->secondItemPrice = secondItemPrice;
    }
};

int main(void) {
    Receipt stock("Sunny Market", "bread", "olive oil", 1.20, 2.80); // 5 parameters
}

Anyway, I not familiar with C++, but found this useful links. I hope it will helped.
 
Last edited:
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top