Sharp Tutorial
special functions in C++

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

introduction to c++

  • C++ Home
  • Introduction to C++
  • History features
  • Sample Programs
  • cout , cin , endl
  • Data type and Operators
  • if else in c++
  • loop in c++

Oops in C++

  • Object / Class
  • Constructor
  • Function & Constructor Overloading
  • Inheritance
  • Overriding

Advanced C++

  • Strings in C++
  • Exception Handling
  • Special Functions
  • C++ Programs

Friend Function in C++

Friend function is used to access the private data of a class .For this we need to declare the function as friend function

Program on friend function

 #include <iostream>  
using namespace std;  
class Box  
{  
    private:  
        int length;  
    public:  
        Box(): length(0) { }  
        friend int printLength(Box); //friend function  
};  
int printLength(Box b)  
{  
    b.length += 10;  
    return b.length;  
}  
int main()  
{  
    Box b;  
    cout<<"Length of box: "<< printLength(b)<<endl;  
    return 0;  
} 
 

Output of the above program is as follows

Virtual function in c++

 #include <iostream>  
using namespace std;  
class A  
{  
 public:  
 virtual void display()  
 {  
  cout << "Base class is invoked"<<endl;  
 }  
};  
class B:public A  
{  
 public:  
 void display()  
 {  
  cout << "Derived Class is invoked"<<endl;  
 }  
};  
int main()  
{  
 A* a;    //pointer of base class  
 B b;     //object of derived class  
 a = &b;  
 a->display();   //Late Binding occurs  
}   
 

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial