Writing a class that has an array with variables and all sub-classes have values for same array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Writing a class that has an array with variables and all sub-classes have values for same array

I am still learning c++ and I couldn't exactly think up the right title for this. But, I want to create a class with an array or function with 3 variables and then create sub-classes that just give those variables a value (with every sub-class having different values).

9th Nov 2019, 2:33 PM
adel
2 Answers
+ 1
adel I show you how to do with variables, you try it with array. #include <iostream> using namespace std; class A{ public: int x = 1; int y = 2; int z = 3; }; class B:public A{ public: int x = 8; int y = 9; int z = 10; }; int main() { A a; B b; cout << a.y << endl; cout << b.y << endl; return 0; } Output is 2 and 9.
9th Nov 2019, 4:16 PM
Avinesh
Avinesh - avatar
+ 1
study inheritance
9th Nov 2019, 3:18 PM
Bahhaⵣ
Bahhaⵣ - avatar