protected access specifier
-
Can anyone tell me how can I access the x in the following statement: FirstClass obj3(200, 300); Here Y = 200, X = 300; X is initialized by calling the constructor BaseClass(x) FirstClass inherits BaseClass with protected access specifier.
#include
using namespace std;class BaseClass
{
private:
int x;
public:
BaseClass()
{
cout << "\n BaseClass(default) called." << endl;
}
BaseClass(int x)
{
cout << "\n BaseClass(parameters) called." << endl;
this->x = x;
}
int getX()
{
return x;
}
};class FirstClass:protected BaseClass
{
private:
int y;
public:
FirstClass(int y, int x):BaseClass(x)
{
cout << "\n FirstClass(parameters) called." << endl;
this->y = y;
}
int getY()
{
return y;
}
};int main()
{
BaseClass obj1;
BaseClass obj2(100);
cout << "\n X = " << obj2.getX() << endl;
FirstClass obj3(200, 300);
cout << "\n Y = " << obj3.getY() << endl;
return 0;
} -
Can anyone tell me how can I access the x in the following statement: FirstClass obj3(200, 300); Here Y = 200, X = 300; X is initialized by calling the constructor BaseClass(x) FirstClass inherits BaseClass with protected access specifier.
#include
using namespace std;class BaseClass
{
private:
int x;
public:
BaseClass()
{
cout << "\n BaseClass(default) called." << endl;
}
BaseClass(int x)
{
cout << "\n BaseClass(parameters) called." << endl;
this->x = x;
}
int getX()
{
return x;
}
};class FirstClass:protected BaseClass
{
private:
int y;
public:
FirstClass(int y, int x):BaseClass(x)
{
cout << "\n FirstClass(parameters) called." << endl;
this->y = y;
}
int getY()
{
return y;
}
};int main()
{
BaseClass obj1;
BaseClass obj2(100);
cout << "\n X = " << obj2.getX() << endl;
FirstClass obj3(200, 300);
cout << "\n Y = " << obj3.getY() << endl;
return 0;
} -
I know the protected access specifier. In my code I need a function into FirstClass() to call function getX() from BaseClass, and I tried but I did not succeed.
You have been given the answer to this twice already in your original post two days ago[^]. You are trying to access an uninitialised variable in an object (obj1). Build your program and step through the code with the debugger and you will be able to see exactly what happens.
-
Can anyone tell me how can I access the x in the following statement: FirstClass obj3(200, 300); Here Y = 200, X = 300; X is initialized by calling the constructor BaseClass(x) FirstClass inherits BaseClass with protected access specifier.
#include
using namespace std;class BaseClass
{
private:
int x;
public:
BaseClass()
{
cout << "\n BaseClass(default) called." << endl;
}
BaseClass(int x)
{
cout << "\n BaseClass(parameters) called." << endl;
this->x = x;
}
int getX()
{
return x;
}
};class FirstClass:protected BaseClass
{
private:
int y;
public:
FirstClass(int y, int x):BaseClass(x)
{
cout << "\n FirstClass(parameters) called." << endl;
this->y = y;
}
int getY()
{
return y;
}
};int main()
{
BaseClass obj1;
BaseClass obj2(100);
cout << "\n X = " << obj2.getX() << endl;
FirstClass obj3(200, 300);
cout << "\n Y = " << obj3.getY() << endl;
return 0;
}kinderu wrote:
Can anyone tell me how can I access the x in the following statement: FirstClass obj3(200, 300);
Taken at face value, this really makes no sense. The member variable
x
belongs to theBaseClass
class. So, are you wanting to accessx
in the context ofobj3
, or from withinBaseClass
,FirstClass
, ormain()
? As it stands right now, your code does indeed assignx
a value, but then you have no code to print it (like you do fory
)."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
kinderu wrote:
Can anyone tell me how can I access the x in the following statement: FirstClass obj3(200, 300);
Taken at face value, this really makes no sense. The member variable
x
belongs to theBaseClass
class. So, are you wanting to accessx
in the context ofobj3
, or from withinBaseClass
,FirstClass
, ormain()
? As it stands right now, your code does indeed assignx
a value, but then you have no code to print it (like you do fory
)."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
this code is what I want to achieve
#include using namespace std;
class BaseClass
{
private:
int x;
public:
BaseClass()
{
cout << "\n BaseClass(default) called." << endl;
}
BaseClass(int x)
{
cout << "\n BaseClass(parameter) called." << endl;
this->x = x;
}
int getX()
{
return x;
}
};
class FirstClass:protected BaseClass
{
private:
int y;
public:
FirstClass(int y, int x):BaseClass(x)
{
cout << "\n FirstClass(parameter) called." << endl;
this->y = y;
}
int getY()
{
return y;
}
int getXX()
{
return getX();
}
};int main()
{
BaseClass obj1;
BaseClass obj2(100);
cout << "\n X = " << obj2.getX() << endl;
FirstClass obj3(200, 300);
cout << "\n Y = " << obj3.getY() << endl;
cout << "\n X = " << obj3.getXX() << endl;
return 0;
} -
this code is what I want to achieve
#include using namespace std;
class BaseClass
{
private:
int x;
public:
BaseClass()
{
cout << "\n BaseClass(default) called." << endl;
}
BaseClass(int x)
{
cout << "\n BaseClass(parameter) called." << endl;
this->x = x;
}
int getX()
{
return x;
}
};
class FirstClass:protected BaseClass
{
private:
int y;
public:
FirstClass(int y, int x):BaseClass(x)
{
cout << "\n FirstClass(parameter) called." << endl;
this->y = y;
}
int getY()
{
return y;
}
int getXX()
{
return getX();
}
};int main()
{
BaseClass obj1;
BaseClass obj2(100);
cout << "\n X = " << obj2.getX() << endl;
FirstClass obj3(200, 300);
cout << "\n Y = " << obj3.getY() << endl;
cout << "\n X = " << obj3.getXX() << endl;
return 0;
}So what's the problem?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
this code is what I want to achieve
#include using namespace std;
class BaseClass
{
private:
int x;
public:
BaseClass()
{
cout << "\n BaseClass(default) called." << endl;
}
BaseClass(int x)
{
cout << "\n BaseClass(parameter) called." << endl;
this->x = x;
}
int getX()
{
return x;
}
};
class FirstClass:protected BaseClass
{
private:
int y;
public:
FirstClass(int y, int x):BaseClass(x)
{
cout << "\n FirstClass(parameter) called." << endl;
this->y = y;
}
int getY()
{
return y;
}
int getXX()
{
return getX();
}
};int main()
{
BaseClass obj1;
BaseClass obj2(100);
cout << "\n X = " << obj2.getX() << endl;
FirstClass obj3(200, 300);
cout << "\n Y = " << obj3.getY() << endl;
cout << "\n X = " << obj3.getXX() << endl;
return 0;
} -
When I run the code shows: BaseClass(default) called. BaseClass(parameter) called. X = 100 BaseClass(parameter) called. FirstClass(parameter) called. Y = 200 X = 300 wich is ok