INLINE FUNCTIONS
If a function is INLINE, then the compiler places the copy of the functions code in
place of the function call.
And this can speed up the program execution.
SYNTAX OF INLINE():
inline return_type function_name(params)
//CODE
EXAMPLE: Program to implement INLINE FUNCTION
#include<iostream.h>
Using namespace std;
Inline int fun(int a,int b)
Return a+b;
Main(){.
Int value=fun(34,67); //calling
Cout<<value;
ADVANTAGES OF USING INLINE FUNCTION
1. Avoid repetition of codes.
2. Increase program readability.
3. Divide a complex program into many simpler problems.
4. Reduces chances of error.
5. Making modifying a program becomes easier.
SOLUTION: #include <iostream>
using namespace std;
inline int Max(int x, int y)
return (x > y)? x : y;
// Main function for the program
int main()
cout << "Max (30,10): " << Max(30,10) << endl;
cout << "Max (0,100): " << Max(0,100) << endl;
cout << "Max (200,1010): " << Max(200,1010) << endl;
return 0;
SOLUTION: #include <iostream>
using namespace std;
inline void displayNum(int num) {
cout << num << endl;
int main() {
// first function call
displayNum(15);
// second function call
DisplayNum(18);
// third function call
displayNum(66);
return 0;
Friend function: It is a function that is declared as a friend of a class not as a
member of a class instead of that it can access private and protected member of a class.
SYNTAX: Friend return-type function name(class & reference);
ADVANTAGES OF USING FRIEND FUNCTION:
1. We can able to access the other class members in our class if we use friend function.
2. We can access the members without inheriting the class.
EXmple: program to implement the friend function
1. #include<iostream.h>
2. Using namespace std;
3. Class Ankush
4. {
5. Private:
6. Int money=10;
7. Friend void Rohit( Ankush, Ankit); //common friend that access the private property
8. };
9. Class Ankit
10. {
11. Private:
12. Int money=20;
13. Friend void Rohit(Ankit,Ankush); //common friend
14. };
15. Void Rohit(Ankit r1,Ankush r2)
16. {
17. Cout<<”sum”<<[Link]+[Link]; //passing reference for accessing the money
that borrow from ankit and ankush
18. Int main()
19. {
20. Ankush obj1; //create object of Ankush class
21. Ankit obj2; //create object of Ankit class
22. Rohit (obj2,obj1); //common friend can take both objects,obj1->r1 and obj2->r2
23. Return0;
24. }
MACROS: It is a preprocessor directive that defines a name or a function like macro
that can be used throughout the program .
It replace the name of macro to value of macro.
Macro is defined using the ‘#define ’ preprocessor directive.
SYNTAX:
#define macro _name macro _value;
EXAMPLE : WAP to implement macros/understand the concept of macros.(AREA OF A
CIRCLE)
1. #include<iostream.h>
2. Using namespace std;
3. #define PI 3.14 //object macro
4. #define square(r) ((r)*(r)) //function name
5. main(){
6. Int r;
7. Cout<<”enter a radius:”;
8. Cin>>r; //r=2
9. Double area=PI*square(r);
10. Cout<<”Area of circle:”<<area;
11. }
VIRTUAL FUNCTIONS:
Virtual means existing in appearance but not in reality.
Virtual functions means function() existing in class but cannot be used.
It is declared in BASE CLASS with keyword ‘VIRTUAL’ and redefined in DERIVED CLASS.
SYNTAX: virtual return_type function_name(argument)
//BODY OF FUNCTIONS
NEED FOR VIRTUAL FUNCTION: The vital reason for having a virtual function is to implement
a different functionality in the derived class.
PURE VIRTUAL FUNCTIONS: They are virtual functions which have no
definition. They start with virtual keyword and ends with equal to zero.
A pure virtual function is a function that has the Notation=0 in the declaration of that
function(a function has no body).
SYNTAX: Virtual return_type fun-name()=0;
Ex-Virtual void get()=0;
EXAMPLE: pure virtual function
#include<iostream.h>
Using namespace std;
Class A
Public:
Virtual void show()=0;
Void display()
Cout<<”Hello:”;
};
Class B:public A
Public:
Void show()
{
Cout<<”Hii:”
};
Void main()
[Link];
[Link]()
Program: with no virtual keyword
class Animal {
public:
void sound() {
cout << "Animal sound\n";
}
};
class Dog : public Animal {
public:
void sound() {
cout << "Dog barks\n";
}
};
int main() {
Animal* a; // Declare a pointer to the base class (Animal)
Dog d; // Create an object of the derived class (Dog)
a = &d; // Point the base class pointer to the Dog object
a->sound(); // Call the sound() function using the pointer. Since sound() is not virtual, this
calls Animal's version
return 0;
}
OUTPUT: Animal Sound
Program with virtual keyword
class Animal {
public:
virtual void sound() {
cout << "Animal sound\n";
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks\n";
}
};
int main() {
Animal* a;//pointer points to object
Dog d; //object
a = &d; //hold address or value of animal class
a->sound(); // Outputs: Dog barks
return 0;
}
OUTPUT:Dog barks
Now it works! Because sound() is virtual, the call uses the actual object's function and not
just the pointer type.
NOTES BY, MS.
SABA NEHAL
Assistant
Professor
KIT,
ROOMA