Answer the question
In order to leave comments, you need to log in
How to pass an array with data from one derived class to another, or how to make the program work?
class Product {
public:
int arrID[3]{1, 2, 3};
string arrP[3]{"ogurec", "pomidor", "redis"};
};
class Balances {
public:
int arrOstN[3]{160, 145, 200};
};
class Product_release:Balances {
public:
int arrOstK[3]{125, 150, 114};
int arrPlan[3]{200, 180, 170};
void func();
};
void Product_release::func() {
setlocale(LC_ALL, "Russian");
int arrAll[3];
for (int i = 0; i<3; i++){
arrAll[i] = arrOstN[i] + arrPlan[i] - arrOstK[i];
}
for (int i = 0; i<3; i++){
cout<<"Продукст "<<arrP[i]<<"имеет ID "<<arrID[i]<<"и объем реализации равен "<<arrAll[i]<<endl; // arrID and arrP was not declared
}
}
int main() {
setlocale(LC_ALL, "Russian");
int num, exit;
cout<<"Введите число:\n 1 - запись данных в файл;\n 2 - вывод содержимого файла на экран;\n 3 - выход. \n\n"<<endl;
cin>>num;
Product_release product_release;
switch(num) {
case 1: cout<<"Вы выбрали запись данных в файл!\n\n"<<endl; product_release.func(); break;
case 2: cout<<"Вы выбрали вывод содержимого файла на экран!\n\n"<<endl; break;
case 3: cout<<"Вы действительно хотите выйти?\nВведите:\n1 - для выхода;\n2 - для отмены.\n\n"<<endl;
cin>>exit;
switch(exit) {
case 1: exit; break;
case 2: main(); break;
default: cout<<"Вы ввели неверное число!"<<endl; main();
}
break;
default: cout<<"Вы ввели неверное число!"<<endl;
}
return 0;
}
cout<<"Продукст "<<arrP[i]<<"имеет ID "<<arrID[i]<<"и объем реализации равен "<<arrAll[i]<<endl;
to file.
Answer the question
In order to leave comments, you need to log in
Either pass the Product object to the function or do this:
( As I understand it, there are 2 base classes from which 3 should be inherited)
class Product_release:Balances,Product
In general, from the comments of @hlogeon it became clear that an object can be transferred to an array
$objArr = (array)$ob;
foreach ($objArr as $key => $value){
$name = $value->name;
$id = $value->id;
}
echo "$name";
echo "$id";
$xxxxxName = 'stdObName'; // сюда динамически присваиваем имя
$name = $ob->$xxxxxName->name;
$id = $ob->$xxxxxName->id;
You can get a list of public properties of an object like this:
$foo = new Foo();
$reflect = new ReflectionClass($foo);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
$names = array();
$ids = array();
foreach($props as $property){
$propName = $property->getName();
$names[] = $object->$propName->name;
$ids[] = $object->$propName->id;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question