C++ Class

1,460 views
Skip to first unread message

Medosz

unread,
Mar 25, 2019, 9:50:33 AM3/25/19
to ISO C++ Standard - Discussion
I am trying to write a program but it is not working. I am a beginner :)
Thank you for your help :)


Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An invoice should include 6 data members a part number (type string), part description (type string), a quantity of the item beeing purchased (type int), a price per item (type int) a value-added tax (VAT) rate as a decimal (type double) and discount rate as decimal (type double) Your class should have a constructor that initializes the 6 data members. The constructor should initialise the first four data members with values frpm parameters and the last two data members to default values of 0.20 per cent and zero respectively. Provide a set and get functions for each data member. In addition provide a member function named getInvoiceAmount that calculates the invoice amount (i.e multiples the quantity by the price per item and applies the tax and discounts amouns) then returns the amount. Have the set data members perform validity checks on their parameters - if value is not positive it should be left unchanged. Write a driver program to demonstrate Invoice's capabilities.

I wrote this, but it is not working as it should work:


Header:

#include <string>

class Invoice {
public:
Invoice(std::string partNumber, std::string partDescription, int purchasedQuantity, int itemPrice)
  : number(partNumber), description(partDescription),
  quantity(purchasedQuantity), price(itemPrice)
  {
   trate = 0.20;
   drate = 0.0;
  }

void setNumber(std::string partNumber) {
number = partNumber;
}
std::string getNumber() const {
return number;
}
void setDescription(std::string partDescription) {
description = partDescription;
}
std::string getDescription() const {
return description;
}
void setQuantity(int purchasedQuantity) {
quantity = purchasedQuantity;
}
int getQuantity() const {
return quantity;
}
void setPrice(int itemPrice) {
price = itemPrice;
}
int getPrice() const {
return price;
}
double getTrate() const {
return trate;
}
double getDrate() const {
return drate;
}
void setInvoiceAmount(double iamount) {
iamount = ((quantity*price)*trate) + (quantity*price - drate);
}
double getInvoiceAmount() const {
return iamount;
}

private:

std::string number;
std::string description;
int quantity;
int price;
double trate;
double drate;
};


Main:

#include <iostream>
#include <string>
#include "Header.h"

using namespace std;

int main() {
Invoice invoice1{ "NO134 56", "PCIe SSD", 3, 130, 0.50, 0 };

cout << "Purchased part number: " << invoice1.getNumber() << " description: " << invoice1.getDescription() << " quantity: "
<< invoice1.getQuantity() << " price per item: " << invoice1.getPrice() << " tax VAT: " << invoice1.getTrate() << " discount rate: "
<< invoice1.getDrate();
cout << "\nThe invoice amount is: " << invoice1.getInvoiceAmount() << endl;

}

Daniel Krügler

unread,
Mar 25, 2019, 10:15:01 AM3/25/19
to std-dis...@isocpp.org
Am Mo., 25. März 2019 um 14:50 Uhr schrieb Medosz <demefe...@gmail.com>:
>
> I am trying to write a program but it is not working. I am a beginner :)
> Thank you for your help :)

This newsgroup seems to be the wrong candidate for your question. The
topics within this group are questions related to the Standard C++
language or Standard C++ Library. Your question looks more like a
homework question to me.

- Daniel

Ferenc Deme

unread,
Mar 25, 2019, 10:27:00 AM3/25/19
to std-dis...@isocpp.org
It is kind of homework but it is not. I bought a c++ book and I am learning it by myself without help. Unfortunately I can't get any help. That is why I am looking for a forum where may somebody can help me

--

---
You received this message because you are subscribed to a topic in the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/a/isocpp.org/d/topic/std-discussion/tR0Hg3dxJGs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.

Cassio Neri

unread,
Mar 25, 2019, 11:40:42 AM3/25/19
to ISO C++ Standard - Discussion
Unfortunately, this is not the right forum. You might be luckier at Stack Overflow.

Good luck,
Cassio.

Tony V E

unread,
Mar 25, 2019, 2:53:50 PM3/25/19
to Ferenc Deme
There's a discord server - include_cpp




Sent from my BlackBerry portable Babbage Device
From: Ferenc Deme
Sent: Monday, March 25, 2019 10:27 AM
Subject: Re: [std-discussion] C++ Class

You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
Reply all
Reply to author
Forward
0 new messages