There is a code:

#include "stdafx.h" #include <iostream> #include <math.h> #include <string> using namespace std; class Auto { public: double inc; double Auto1(double a, double b, string val) { inc = b - a; cout << "Прибыль: " << inc << val << endl; return inc; } }; class BMW : public Auto { public: void BMW1() { cout << "Престиж: " << inc <<" престижей"<< endl; } }; int main() { double a, b,inc; string val; setlocale(LC_ALL, "rus"); cout << "Введите затраты на производство одного автомобиля: " << endl; cin >> a; cout << "Введите стоимость покупки: " << endl; cin >> b; cout << "Введите валюту: " << endl; cin >> val; Auto s1; s1.Auto1(a, b, val); BMW s2; s2.BMW1(); system("pause"); return 0; } 

How to inherit the inc variable in the BMW class? Explain, please. Thank you in advance!

  • The variable has already been inherited. What more do you want from it? - user194374
  • it was not inherited, the class of BMW displays transcendental number, which definitely should not output - JaredBay
  • 2
    In fact, it would be nice to define constructors ... And then, inc you have a variable that is not static, so changing it in s1 , in s2 you won’t get it. Classes themselves do not bear any load, you simply tied them with "strings", just to be ... - Harry

1 answer 1

I will try to use telepathy, most likely the author wanted something like this:

 #include <iostream> #include <math.h> #include <string> using namespace std; class Auto { public: double inc; double Auto1(double a, double b, string val) { inc = b - a; cout << "Прибыль: " << inc << val << endl; return inc; } }; class BMW : public Auto { public: void BMW1() { cout << "Престиж: " << inc <<" престижей"<< endl; } }; int main() { double a, b,inc; string val; setlocale(LC_ALL, "rus"); cout << "Введите затраты на производство одного автомобиля: " << endl; cin >> a; cout << "Введите стоимость покупки: " << endl; cin >> b; cout << "Введите валюту: " << endl; cin >> val; BMW s2; s2.Auto1(a, b, val); s2.BMW1(); system("pause"); return 0; } 
  • understood my mistake, thank you! and - JaredBay