C++ Programming - OOPS Concepts - Discussion

Discussion Forum : OOPS Concepts - General Questions (Q.No. 4)
4.
Which of the following is not the member of class?
Static function
Friend function
Const function
Virtual function
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
42 comments Page 2 of 5.

Abhishek porwal said:   1 decade ago
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:   1 decade ago
I dont know what is cont class?

Shashi said:   1 decade ago
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:   1 decade ago
What is virtual funtion?

Vidya v nair said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
Can you explain the main function?

Shakti said:   1 decade ago
What is the mean of 'pA->show() ;'. And how its work ?

Anjali said:   1 decade ago
#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:   1 decade ago
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.


Post your comments here:

Your comments will be displayed after verification.