0

a class

hello i have Question : if we enter 100 for "s" and 1 for "j" and 1 for "k" and 1 for "o" and 1 for "i" we must have p and a as p = 2 a =2 for output so why i get worng answer. try this code and see. thanks for help. //////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> using namespace std; class priceofcompany { public: void swelling() { cout << " Percentage of inflation next year " << endl; cin >> s; } void a4() { long int j, k; cout << " Price per packet of A4 paper " << endl; cin >> j; cout << " Number of packages produced " << endl; cin >> k; a = j*k; } void pencil() { long int o, i; cout << "The price of each pencil " << endl; cin >> o; cout << " The number of pencil productions " << endl; cin >> i; p = o*i; } void price() { p = p + p*s; a = a + a*s; cout << p << endl <<a; } private: double s, a, p; }; int main() { priceofcompany m, n, w, v; m.swelling(); n.a4(); w.pencil(); v.price(); cin.get(); cin.get(); return 0; }

4th Feb 2019, 5:07 PM
Shayan Saboori
Shayan Saboori - avatar
1 ответ
+ 6
Hello there, the first thing that I noticed here is that: * You created 4 instances of `priceofcompany` class * You ask for inflation percentage on object m * You ask for A4 papers details on object n * You ask for pencils details on object w * And outputs the adjusted price from object v. You can't have a valid output while the information are separated in between those objects, you need to use one object where you call the proper method to ask for inflation percentage (swelling), paper production (a4), and pencil production (pencil) details, then from the same object you generate output (price), try this and you may have a good output : ) You are welcome to ask further.
4th Feb 2019, 5:36 PM
Ipang