C++ Basics and Functions
C++ Basics and Functions
in C++
C++ Friend function
• If a function is defined as a friend function in C++, then the protected
and private data of a class can be accessed using the function.
• By using the keyword friend compiler knows the given function is a
friend function.
• For accessing the data, the declaration of a friend function should be
done inside the body of a class starting with the keyword friend.
Declaration of friend function in C++
• class class_name
• {
• friend data_type function_name(argument/s); // syntax of friend function.
• };
• The function is not in the scope of the class to which it has been
declared as a friend.
• It cannot be called using the object as it is not in the scope of that
class.
• It can be invoked like a normal function without using the object.
• It cannot access the member names directly and has to use an object
name and dot membership operator with the member name.
• It can be declared either in the private or the public part.
C++ friend function Example
1. #include <iostream> 12. b.length += 10;
2. using namespace std; 13. return b.length;
3. class Box 14. }
4. { 15. int main()
5. private: 16. {
6. int length; 17. Box b;
7. public: 18. cout<<"Length of box: "<< printLen
8. friend int printLength(Box); gth(b)<<endl;
9. }; 19. return 0;
10. int printLength(Box b) 20. }
11. {
Output: 10
Simple example when the function is friendly to two classes
1. #include <iostream> 17. if(a.x<=b.y)
2. using namespace std; 18. std::cout << a.x << std::endl;
3. class B; // forward declarartion. 19. else
4. class A { 20. std::cout << b.y << std::endl; }
5. int x; 21. int main() {
6. public: 22. A a;
7. void setdata(int i) { 23. B b;
8. x=i; } 24. a.setdata(10);
9. friend void min(A,B); }; 25. b.setdata(20);
10. class B { 26. min(a,b);
11. int y; 27. return 0; }
12. public:
13. void setdata(int i) {
14. y=i; }
15. friend void min(A,B); };
16. void min(A a,B b) {
Output: 10
Important points about friend functions and classes:
• 1) Friends should be used only for limited purpose. too many functions or
external classes are declared as friends of a class with protected or private
data, it lessens the value of encapsulation of separate classes in object-
oriented programming.
2) Friendship is not mutual. If class A is a friend of B, then B doesn’t
become a friend of A automatically.
3) Friendship is not inherited
• A friend class can access both private and protected members of the
class in which it has been declared as friend.
• It is sometimes useful to allow a particular class to access private
members of other class
C++ Friend class Example
1. #include <iostream> 14. {
2. 15. cout<<"value of x is : "<<a.x;
3. using namespace std; 16. }
4. 17. };
5. class A 18. int main()
6. { 19. {
7. int x =5; 20. A a;
8. friend class B; // friend class. 21. B b;
9. }; 22. b.display(a);
10. class B 23. return 0;
11. { 24. }
12. public:
13. void display(A &a)
Output: value of x is : 5
Virtual Function in C++
• A C++ virtual function is a member function in the base class that you
redefine in a derived class. It is declared using the virtual keyword.
• It is used to tell the compiler to perform dynamic linkage or late
binding on the function.
• Basically, a virtual function is used in the base class in order to ensure
that the function is overridden. This especially applies to cases where
a pointer of base class points to an object of a derived class.A 'virtual'
is a keyword preceding the normal declaration of a function.
• When the function is made virtual, C++ determines which function is
to be invoked at the runtime based on the type of the object pointed
by the base class pointer.
Requirement of Virtual Function
1. #include <iostream> 16. void display()
2. using namespace std; 17. {
3. class A 18. std::cout << "Value of y is : " <<y<< std::endl;
4. { 19. }
5. int x=5; 20. };
6. public: 21. int main()
7. void display() 22. {
8. { 23. A *a;
9. std::cout << "Value of x is : " << x<<std::endl; 24. B b;
10. } 25. a = &b;
11. }; 26. a->display();
12. class B: public A 27. return 0;
13. { 28. }
14. int y = 10;
15. public:
Output: Value of x is : 5
C++ virtual function Example
1. #include <iostream> 13. {
2. { 14. cout << "Derived Class is invoked"<<endl;
3. public: 15. }
4. virtual void display() 16. };
5. { 17. int main()
6. cout << "Base class is invoked"<<endl; 18. {
7. } 19. A* a; //pointer of base class
8. }; 20. B b; //object of derived class
9. class B:public A 21. a = &b;
10. { 22. a->display(); //Late Binding occurs
11. public: 23. }
12. void display()