-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduct.cpp
More file actions
34 lines (26 loc) · 806 Bytes
/
Copy pathproduct.cpp
File metadata and controls
34 lines (26 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "product.h"
#include <sstream>
#include <iostream>
#include <iomanip>
Product::Product(std::string name, double price, double cost)
: _name{name}, _price{price}, _cost{cost} { }
Product::Product(std::istream& ist) {
std::getline(ist, _name);
ist >> _price; ist.ignore();
ist >> _cost; ist.ignore();
}
void Product::save(std::ostream& ost) {
ost << _name << std::endl;
ost << _cost << std::endl;
ost << _price << std::endl;
}
std::string Product::name() {return _name;}
double Product::profit() {return _price-_cost;}
double Product::price(){
return _price;
}
std::string Product::to_string() {
std::ostringstream oss;
oss << _name << std::fixed << std::setprecision(2) << " ($" << _price << " / $" << _cost << ')';
return oss.str();
}