Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-01-2010
Member
 
Join Date: Jan 2010
Posts: 22
Access the non-public members of the class:C++

Hi, I am studying in the Second year of the BscIT. I have C++ language in syllabus. I have the little bit knowledge about the C++. My teacher gave me an assignment on the functions that make the use of private members of the class. SoI want to know about how can make the use of non public members of the base class? Is there any function or method that allows me to do this. If any then what it is? If anyone knows about it then help me.
Reply me!
Reply With Quote
  #2  
Old 18-01-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
Friend funtion

Hello, I think the answer to your question is that you require Friend function. For accessing the non-public members of a class Friend functions are used . Whenever a data is declared as private inside a base class, then it is not accessible from outside the class. A class can only allow non-member functions and other classes to access its own private data, by making them as friend. A Friend function is a function or a member of another class. Friend function is also Global friend. I hope it is enough to know you how can we access the private members of the class outside it.

Last edited by Modifier : 18-01-2010 at 11:08 AM.
Reply With Quote
  #3  
Old 18-01-2010
Member
 
Join Date: May 2008
Posts: 2,383
C++ Functions

I guess it helps you to make your assignment:
Properties of Friend function:
1) Friend of the one class can be the member of some another class.
2) A friend is known as GLOBAL FRIEND.
3) Friend can access the non-public or protected members of the class in which they are declared as friend.
4) Friends do not get “this” pointer.
5) Friends can be friend of more than one class.
6) Friend can be declared anywhere in the class.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 18-01-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Program

This example shows you the use of friend function. Friend function abc is a member of class B and accesses the private data members p and q of class A.

#include <iostream>
using namespace std;

class A;
class B
{
public:
void print(A& y);
};

class A
{
int p,q;
friend void B::print(A& y);
public:
A() : p(1), q(2) { }
};

void B::print(A& y)
{
cout << "p is " << y.p << endl;
cout << "q is " << y.q << endl;
}

int main()
{
A yobj;
B sobj;
sobj.print(yobj);
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Access the non-public members of the class:C++"
Thread Thread Starter Forum Replies Last Post
Listing members of Group with >1500 members Umesh Thakur Windows Server Help 11 03-02-2012 05:29 AM
Display members of a group with more than 1500 members Simon G Windows Server Help 5 25-10-2011 12:35 PM
public, private, protected access modifiers in OOP Bol-Bacchan Software Development 5 09-03-2011 05:43 PM
What is static data members of class? Jaiwanti Software Development 3 20-11-2009 10:48 AM
Access remote directory with a public IP address Daljeet Software Development 4 19-12-2008 06:09 PM


All times are GMT +5.5. The time now is 08:15 AM.