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?
Discussion:
42 comments Page 1 of 5.
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.
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.
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;
}
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;
}
Shruti Ganesh said:
1 decade ago
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.
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.
Amruta 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.
Mohamed Rafeek 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.
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.
Sandesh said:
1 decade ago
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.
One more thing is that const memb function can modify only those data members that are declared as mutable.
Saddam Hossain Mondal said:
1 decade ago
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.
Kavita said:
1 decade ago
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.
Nandita Pandey said:
7 years ago
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.
(6)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers