Why it's showing errors | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why it's showing errors

Please can anyone explain to me the logic behind the code that i have mentioned below. In the below code it's not showing errors if i include the lines =====> :B(i) on class C and :A(i) on class B constructors. Why it's not showing errors after i included these lines on the code kindly explain to me any of you guys. https://code.sololearn.com/cToqBELWKjah/?ref=app

13th Mar 2023, 3:15 PM
Kabilan K
Kabilan K - avatar
1 Answer
+ 6
All of the constructors for A, B and C in this code require an input parameter (i). When you instantiate C in main, automatically it must also instantiate its superclass - B. The B constructor needs an argument to pass into its parameter i. So the syntax, C(int i): B(i) {, tells the compiler to pass i as the argument to construct B. In turn, B automatically must call its superclass constructor A and pass an argument. The syntax, B(int i): A(i), passes i as the argument for A's constructor. The code would work without adding the extra syntax if A and B both were given another constructor that requires no argument. A() { cout<<"just plain A()"<<endl; } And B() { cout<<"just plain B()"<<endl; }
13th Mar 2023, 4:56 PM
Brian
Brian - avatar