How to access private member function using friend class object? - c++

In main, I want to access the display function. Here, in class B I declared class A as friend. So i thought that it is possible to access the private member functions.
But i dont know how to do that.
#include<stdio.h>
class A
{
public:
class B
{
public:
friend class A;
private:
void display()
{
printf("\nHi");
}
};
};
int main()
{
//here i wanna access display function.. is it possible?
return 1;
}

friend specifies what has access to private members. In your case, you want to access private members in the main function, so you should specify that it's friend:
class A
{
public:
class B
{
friend int main();
void display()
{
printf("\nHi");
}
};
};
int main()
{
// here you can access display function:
A::B object;
object.display();
}
Alternatively, if you want to make class A (and not anything else) a friend, then class A should access the display function. Any member of class A can do it:
class A
{
public:
class B
{
friend class A;
void display()
{
printf("\nHi");
}
};
// here you can access display function:
void access_display(B object)
{
object.display();
}
};
int main()
{
A object1;
A::B object2;
object1.access_display(object2);
}

Related

C++ Change private member of a class through only one other specific class?

How do I change the int 'a' inside class A but restrict the function that changes the int to ONLY class B? I do not even want A to be able to change it.
I have tried to create a friend function, but I get an error I included in the comments of the code. I then tried to just forward declare the function as a friend function in private and then actually creating the function in public, but the C class can access the public function. This despite me writing (in the private section) that the function is a friend function. Can I 'friend' a private value? I am unsure what to do here.
#include <stdio.h>
#include <iostream>
int main() {
// forward declaration
class B;
class C;
class A {
private:
int a; // private member I want to edit, but only via class B.
int a2; // I would like to NOT be able to access this in class B. I only want to access and modify int a from class B, no other variable. If possible.
// invalid use of non-static data member 'a'
friend void test(int new_value) {
a = 5;
}
friend B;
public:
};
class B {
private:
int b;
public:
change_a_value(A a_class, int new_int) {
a_class.test(new_int); // I want this to be possible.
}
};
class C {
private:
int c;
public:
change_a_value(A a_class, int new_int) {
a_class.test(new_int); // I want this to be impossible
}
};
return 0;
}
Add "void" before prototypes of change_a_value, and change the
friend B
to
friend class B
and suppress friend in
friend void test(int new_value) {
complete corrected program :
int main() {
// forward declaration
class B;
class C;
class A {
private:
int a; // private member I want to edit, but only via class B.
int a2; // I would like to NOT be able to access this in class B. I only want to access and modify int a from class B, no other variable. If possible.
void test(int new_value) {
a = new_value;
}
friend class B;
public:
};
class B {
private:
int b;
public:
void change_a_value(A a_class, int new_int) {
a_class.test(new_int); // I want this to be possible.
}
};
class C {
private:
int c;
public:
void change_a_value(A a_class, int new_int) {
a_class.test(new_int); // I want this to be impossible
}
};
return 0;
}
Compilation results are what was expected :
TestCpp2.cpp:9:14: error: ‘void main()::A::test(int)’ is private
void test(int new_value) {
^
TestCpp2.cpp:28:31: error: within this context
a_class.test(new_int); // I want this to be impossible
^
Makefile:510: recipe for target 'TestCpp2.o' failed
make: *** [TestCpp2.o] Error 1

Class members visibility C++

I need to make the initializer of a class invisible for the child classes, but visible for the main(). How can I do it? The only way I see is to make it PUBLIC, but it will be inherited to the child classes. Any ideas?
You can make it private and add main as a friend
class A {
private:
A() {}
public:
friend int main(void);
};
int main(void) {
// Your code
}
Making the initializer private (cannot access the initializer even in main), there are many design patterns you can follow for solving your problem. One of the design pattern is Singelton.
The example is given below:
#include <iostream>
using namespace std;
class A{
private:
static A *single_instance;
//private constructor, cannot be inherited
A(){
}
public:
//for getting access in main
static A *getInstance() {
if (single_instance==NULL){
single_instance = new A();
}
return single_instance;
}
void print() {
cout<<"I'm from A";
}
};
//initializing with NULL
A *A ::single_instance=NULL;
class B:A {
//not access to the constructor
//but access to getInstance()
};
int main()
{
//now you can access it in main
A *obj = obj->getInstance();
obj->print();
return 0;
}
Note that, this design makes sure, only one instance can be created from your class.

Passing instance of own class to another

Assuming I have these classes (question marks mark the question what I need to pass here):
class A
{
...
public:
void pass()
{
B ins;
ins.doSth(?????);
}
};
class B
{
...
public:
void doSth(const A &sth)
{
...
}
}
int main()
{
A te;
te.pass();
}
Can you pass an instance of your own class or is this just an example of a failed class structure on my side?
The current object in a member function is *this. You can pass that to another function.
You will have to consider how the classes depend on each other, and that one class cannot use the other class until the declaration is complete.
This would work though:
class A
{
//...
public:
void pass();
};
class B
{
//...
public:
void doSth(const A &sth)
{
//...
}
};
// Here both classes are completely declared
void A::pass()
{
B ins;
ins.doSth(*this);
}
int main()
{
A te;
te.pass();
}
Your class "contains" an instance of each other so you'll face an error of undeclared types. To solve this issue you need to use forward declaration.
And you'll face another problem:
If your methods doSth() and pass() are defined inlinlely then you'll face a problem: "using incomplete types". The workaround this is to implement these methods outside the class so that each object has been fully constructed before used.
The program may look like:
class A;
class B;
class A{
public:
void pass();
};
class B{
public:
void doSth(const A &sth){
}
};
void A::pass(){
B ins;
ins.doSth(*this);
}
int main(){
A te;
te.pass();
return 0;
}

Learning Classes C++ - Can't set values in an array of classes nested within another class.

I am super-new to classes and still wrapping my brain around how they work. Any help/advice/pointers-> are appreciated!
I have two classes. Within the second class is an array of the first class. I am trying to assign values to the private member variables contained in the array of the first class.
I get this error message when compiling:
hw2Test.cpp: In member function 'void bar::set(int)':
hw2Test.cpp:11:7: error: 'int foo::x' is private
int x;
^
hw2Test.cpp:34:12: error: within this context
foodoo[0].x = x;
^
Here is the code:
#include <iostream>
using namespace std;
class foo
{
public:
private:
int x;
};
class bar
{
public:
void set(int x);
private:
foo foodoo[1];
};
int main()
{
bar tar;
tar.set(1);
return 0;
}
void bar::set(int x)
{
foodoo[0].x = x;
}
foo::x is declared as private, so only methods of foo can access it. But you are trying to access x inside of a method of bar instead, which does not have access to foo's private members.
To give bar access, you need to either:
declare foo::x as public:
class foo
{
public:
int x;
};
void bar::set(int x)
{
foodoo[0].x = x;
}
declare a public setter:
class foo
{
public:
void set(int i);
private:
int x;
};
void foo::set(int i)
{
foodoo[0].x = i;
}
void bar::set(int x)
{
foodoo[0].set(x);
}
declare bar as a friend of foo:
class foo
{
public:
private:
int x;
friend class bar;
};
void bar::set(int x)
{
foodoo[0].x = x;
}

How to call a private function via friend function?

Hello I am trying to access a private member function is Gtest.
The code looks somewhat similar to this. So, how can I access static void Pri_fun?
using namespace std;
class test{
};
class abc{
public:
friend class test;
private:
static void Pri_fun()
{
cout << "private fun called \n";
}
};
int main()
{
abc ab;
test *abd;
abd->Pri_fun();
}
Since it's a static function, you should access it via the class name:
abc::Pri_fun();
You should make a caller function though, or call it from the friend class' constructor:
class test{
public:
void foo()
{
abc::Pri_fun();
}
};
or
class test{
public:
test()
{
abc::Pri_fun();
}
};