I have a class which declares some static variables:
#include <iostream>
class A
{
private:
static int x;
public:
static int y;
static getX(){ return A::x;}
static setX(int z){ A::x = z;}
};
int A::x = 0;
int A::y = 0;
Now class A can be accessed by anyone/anywhere and its member variables can be manipulate. How can I allow only one another class that will have access to class A static variables / methods?
class B
{
public:
void showX(){A::setX(9) ; std::cout << A::getX() << std::endl;}
void showY(){A::y = 8; std::cout << A::y << std::endl;}
};
int main()
{
B b1;
b1.showX();
b1.showY();
}
Define A inside B as private class.
#include <iostream>
class B
{
class A
{
private:
static int x;
public:
static int y;
static int getX() {
return A::x;
}
static void setX(int z) {
A::x = z;
}
};
public:
void showX() {
A::setX(9) ;
std::cout << A::getX() << std::endl;
}
void showY() {
A::y = 8;
std::cout << A::y << std::endl;
}
};
int B::A::x = 0;
int B::A::y = 0;
int main()
{
B b1;
b1.showX();
b1.showY();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With