+ 2

Whats wrong in this program? How to fix the code to get output 10

#include<iostream> using namespace std; class base { public: int a=10; }; class child: public base { public: a=20; child(){cout<<a;} }; int main() { child o; return 0; }

29th Aug 2017, 4:40 PM
Asif Akhtar
Asif Akhtar - avatar
4 Answers
+ 6
#include<iostream> using namespace std; class base { public: int a=10; }; class child: base { public: // int a=20; // This creates a class variable for the child class. you must still declare its type (int) child() { //a=20; //if the variable above doesn't exist then this will access the a from the base class and set its value to 20 cout<<a; // This will output the value of a // if both lines above are commented out it will output 10 from the base class } }; int main() { child o; return 0; }
29th Aug 2017, 5:37 PM
ChaoticDawg
ChaoticDawg - avatar
0
thanks
30th Aug 2017, 11:30 AM
Asif Akhtar
Asif Akhtar - avatar
0
it's executing an showing out put 20
30th Aug 2017, 11:31 AM
Asif Akhtar
Asif Akhtar - avatar
0
the main thing is to declare it data type
30th Aug 2017, 11:40 AM
Asif Akhtar
Asif Akhtar - avatar