0% found this document useful (0 votes)
30 views

C++ Basics and Functions

C++ Basics : Overview, Program structure, namespace, identifiers, variables, constants, enum, operators, typecasting, control structures C++ Functions : Simple functions, Call and Return by reference, Inline functions, Macro Vs. Inline functions, Overloading of functions, default arguments, friend functions, virtual functions

Uploaded by

Anusha Maurya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

C++ Basics and Functions

C++ Basics : Overview, Program structure, namespace, identifiers, variables, constants, enum, operators, typecasting, control structures C++ Functions : Simple functions, Call and Return by reference, Inline functions, Macro Vs. Inline functions, Overloading of functions, default arguments, friend functions, virtual functions

Uploaded by

Anusha Maurya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Friend and Virtual 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 can be defined anywhere in the program like a normal


C++ function.
• The function definition does not use either the keyword friend or
scope resolution operator.
Characteristics of a 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

• 4) The concept of friends is not there in Java.


C++ Friend class

• 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()

Output: Derived Class is invoked


Pure Virtual Function
• A virtual function is not used for performing any task. It only serves as a
placeholder.
• When the function has no definition, such function is known as "do-
nothing" function.
• The "do-nothing" function is known as a pure virtual function. A pure
virtual function is a function declared in the base class that has no
definition relative to the base class.
• A class containing the pure virtual function cannot be used to declare the
objects of its own, such classes are known as abstract base classes.
• The main objective of the base class is to provide the traits to the derived
classes and to create the base pointer used for achieving the runtime
polymorphism.
• Pure virtual function can be defined as: virtual void display() = 0;
Pure Virtual Function Example
• #include <iostream> 13. }
1. using namespace std; 14. };
2. class Base 15. int main()
3. { 16. {
4. public: 17. Base *bptr;
5. virtual void show() = 0; 18. //Base b;
6. }; 19. Derived d;
7. class Derived : public Base 20. bptr = &d;
8. { 21. bptr->show();
9. public: 22. return 0;
10. void show() 23. }
11. {
12. std::cout << "Derived class is derived from th
e base class." << std::endl;

Output: Derived class is derived from the base class

You might also like