+ 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; }
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;
}
0
thanks
0
it's executing an showing out put 20
0
the main thing is to declare it data type