C++ Programming - OOPS Concepts - Discussion

Discussion :: OOPS Concepts - General Questions (Q.No.4)

4. 

Which of the following is not the member of class?

[A]. Static function
[B]. Friend function
[C]. Const function
[D]. Virtual function

Answer: Option B

Explanation:

No answer description available for this question.

Amruta said: (May 11, 2012)  
In a class, the private data can be accessed only by the public member function of that class. c++ provides a mechanism in which a non member can have access to the private member of a class. This is achieved by the non_member function "friend" to the class, whose private data can be accessed.

Smarak said: (May 12, 2012)  
What is const function?

Srikanth said: (May 23, 2012)  
A const function is one, which doesn't allow any modification or change its instance variable's data (member data).

Selvakumar said: (Jul 9, 2012)  
What is virtual function ?

Kavita said: (Jul 12, 2012)  
I agree with Amruta and virtual function is used for dynamic binding. If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases, class will invoke the base function.

Maha said: (Jul 19, 2012)  
Why we are using virtual function in dynamic binding ?

Mohamed Rafeek said: (Jul 31, 2012)  
In a class, the private data can be accessed only by the public member function of that class. C++ provides a mechanism in which a non member can have access to the private member of a class. This is achieved by the non_member function "friend" to the class, whose private data can be accessed.

Yogesh said: (Aug 16, 2012)  
Virtual function can be used in derived class.

Sandesh said: (Aug 17, 2012)  
A const member function cannot modify any of the data members of its class. This is used by programmers to prevent accidental modification of data members.

One more thing is that const memb function can modify only those data members that are declared as mutable.

Shruti Ganesh said: (Aug 22, 2012)  
A class can have different types of MEMBER functions or methods. These can be static, virtual or constant.

These are functions of the class and belong to the class. However, a friend function is one such type of function which can access the private data members of a class and is not a MEMBER function.

Why do we need them? 'cause there are times when you need to access private data members with an external function. Using too many friend functions disintegrates the privacy of the object. They should be used only when necessary.

Abhishek Porwal said: (Aug 23, 2012)  
Private data member can acess out side the class with the help of friend function. (i.e) we can access the private data member of the base class in drived class so we use friend function.

Yogeshwar Bargal said: (Oct 6, 2012)  
I dont know what is cont class?

Shashi said: (Mar 17, 2013)  
A friend function is that function of the class which can access the private and public members of the class but it is not the member of the class. It is declared inside the class either in private or public.

Vani said: (Apr 25, 2013)  
What is virtual funtion?

Vidya V Nair said: (May 1, 2013)  
Virtual function means the function defined in a base class implemented by the child class and it is called at the run time.

Manisha said: (May 18, 2013)  
A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function's declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.

Base class pointer can point to derived class object. In this case, using base class pointer if we call some function which is in both classes, then base class function is invoked. But if we want to invoke derived class function using base class pointer, it can be achieved by defining the function as virtual in base class, this is how virtual functions support runtime polymorphism.

Consider following program code:

Class A
{
int a;
public:
A()
{
a = 1;
}
virtual void show()
{
cout <<a;
}
};

Class B: public A
{
int b;
public:
B()
{
b = 2;
}
virtual void show()
{
cout <<b;
}
};

int main()
{
A *pA;
B oB;
pA = &oB;
pA->show();
return 0;
}

Output is 2 since pA points to object of B and show() is virtual in base class A.

Saai said: (Sep 29, 2013)  
Can you explain the main function?

Shakti said: (Nov 22, 2013)  
What is the mean of 'pA->show() ;'. And how its work ?

Anjali said: (Mar 3, 2014)  
#include <iostream>

using namespace std;

class Box
{
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};

// Member function definition
void Box::setWidth( double wid )
{
width = wid;
}

// Note: printWidth() is not a member function of any class.
void printWidth( Box box )
{
/* Because setWidth() is a friend of Box, it can
directly access any member of this class */
cout << "Width of box : " << box.width <<endl;
}

// Main function for the program
int main( )
{
Box box;

// set box width without member function
box.setWidth(10.0);

// Use friend function to print the wdith.
printWidth( box );

return 0;
}

Datta Bachate said: (Mar 8, 2014)  
In a class, the private data can be accessed only by the public member function of that class. C++ provides a mechanism in which a non member can have access to the private member of a class. This is achieved by the non_member function "friend" to the class, whose private data can be accessed.

Ankita said: (Jun 24, 2014)  
What is dynamic binding, static function, const function, virtual function?

Karthika said: (Sep 21, 2014)  
What is dynamic binding, static function, const function, virtual function?

Ashish Ranjan Shadangi said: (Feb 10, 2015)  
pA = &oB; means pA contains the address of oB. And pA->show will definitely show the output as 2.

So what is the requirement of show as virtual function?

Pooja said: (Mar 30, 2015)  
What is the friend function?

Raj said: (May 4, 2015)  
@Pooja.

Friend function can access the private & protected data of the parent class!

Normally, Other class couldn't able to access the private & protected data of a class.

Mahesh27 said: (May 27, 2015)  
What is this pointer?

Saddam Hossain Mondal said: (Aug 2, 2015)  
A function that has access to the private members of a class but is not itself a member of the class. An entire class can be a friend function of another class. A function that although not a member of a class is able to access the private members of that class.

Sabitha.R said: (Aug 27, 2015)  
Friend function is used in only public class.

N.Sivaraman said: (Aug 29, 2015)  
What is meant by Static function?

Thazaar said: (Sep 11, 2015)  
Friend function is used to access the private data member of a class. So it is not a member of a class.

Jeswanth Kumaar.C.S.G said: (Oct 3, 2015)  
Because we don't have friend function in C++. We have only friend function and friend class in C++.

Abhishek Chaudhary said: (Oct 14, 2015)  
If friend function will be the member function of the class then it can access private data member of the class without using friend function.

Mych5 said: (Oct 22, 2015)  
What is static function and where they are applicable?

Priyanka Patil said: (Feb 26, 2016)  
What is friend function?

Chetan said: (Aug 16, 2016)  
A friend is a function, i.e. It's not member of the class but has access to private and protected members of the class.

Ahmad said: (Sep 26, 2016)  
Without Virtual function can we dynamic binding?

Muhammad Abdul Khaliq said: (Jul 28, 2017)  
Static Function Can Be The Past of Class?

Nandita Pandey said: (Aug 10, 2018)  
Since friend functions are used by the methods that want to access the private data members of a class. They are only declared in the class and defined outside the class hence, they are not the members of a class.

Piyush said: (Sep 9, 2019)  
Yes, correct @Nandita. Thanks.

Swastik said: (Nov 23, 2019)  
You declare function as a friend inside class but it is not considered as member function of the class.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.