#include <iostream>
usingnamespace std;
class a {
protected: int a=5;
public:
a(){cout<<"a c"<<endl;}
};
class b {
protected: int b=6;
public:
b(){cout<<"b c"<<endl;}
};
class d :protected a,protected b
{
public:
void display()
{cout<<a<<b<<endl;}
d(){cout<<"d c"<<endl;}
};
int main()
{d m;
m.display();return 0;}
errors:
/root/newtest/main.cpp|4|error: field ‘int a::a’ with same name as class [-fpermissive]|
/root/newtest/main.cpp|9|error: field ‘int b::b’ with same name as class [-fpermissive]|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
When I write a class I like to use this for the variable names: "m_variableName". Where the "m_" says the this is a member variable of a class. This way if you try to use this kind of variable outside of a class member function it will help understand the compiler error.
Your code would read much better with the proper indentation and a couple of blank lines. In class a, d and main the {}s are hard to find. In your code for main I almost missed finding them. The following code makes the code much easier to read and follow.
#include <iostream>
usingnamespace std; // <--- Best not to use
class a
{
protected: int m_a = 5;
public:
a() { cout << "a c" << endl; }
};
class b
{
protected: int m_b = 6;
public:
b() { cout << "b c" << endl; }
};
class d :protected a, protected b
{
public:
void display()
{
cout << a << b << endl;
}
d() { cout << "d c" << endl; }
};
int main()
{
d m;
m.display();
return 0;
}