0% found this document useful (0 votes)
69 views35 pages

C++ Advanced Concepts Guide

Friend functions and classes allow non-member functions and classes to access private and protected members of another class. A friend function is declared with the friend specifier in a class, and a friend class is declared as a friend in the class definition. Friend functions and classes do not have to be declared in the same file as the class.

Uploaded by

Thanga Prakash
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views35 pages

C++ Advanced Concepts Guide

Friend functions and classes allow non-member functions and classes to access private and protected members of another class. A friend function is declared with the friend specifier in a class, and a friend class is declared as a friend in the class definition. Friend functions and classes do not have to be declared in the same file as the class.

Uploaded by

Thanga Prakash
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

FRIEND IN C++

FRIEND
A friend of a class X is a function or class that is
not a member of X, but is granted the same
access to X as the members of X.

Functions declared with the friend specifier in a


class member list are called friend functions of
that class.
Classes declared with the friend specifier
in the member list of another class are
called friend classes of that class.

A class Y must be defined before any


member of Y can be declared a friend of
another class.
In the following example, the friend
function print is a member of class Y and
accesses the private data members a and b of
class X.

#include <iostream>
using namespace std;
class X;
class Y
{
public:
void print(X& x);
};
class X
{
int a, b;
friend void Y::print(X& x);
public:
X() : a(1), b(2) { }
};
void Y::print(X& x) {
cout << "a is " << x.a << endl;
cout << "b is " << x.b << endl;
}
int main()
{
X xobj;
Y yobj;
yobj.print(xobj);
}
The following is the output of the above
example:

a is 1
b is 2

You can declare an entire class as a friend.


Suppose class F is a friend of class A. This
means that every member function and
static data member definition of class F has
access to class A.
You must use an elaborated type specifier when you
declare a class as a friend. The following example
demonstrates this:
class F;
class G;
class X
{
friend class F;
friend G;
};
The compiler will warn you that the friend declaration
of G must be an elaborated class name.
You cannot define a class in a friend declaration.
For example, the compiler will not allow the
following:

class F;
class X
{
friend class F { };
};
VIRTUAL FUNCTION IN C++
C++ Virtual Function - Reasons:

   The most prominent reason why a C++ virtual


function will be used is to have a
different functionality in the derived class.
For example a Create function in a
class Window may have to create a window with
white background.

But a class called CommandButton derived


or inherited from Window, may have to use a gray
background and write a caption on the center.

The Create function for CommandButton now
should have a functionality different from the one
at the class called Window.
C++ Virtual function - Example:

   This article assumes a base class named


window with a virtual member function named
create.The derived class name will be
CommandButton,with our over ridden function
create.
class Window // Base class for C++ virtual
function example
     {
       public:
          virtual void Create() // virtual function for
C++ virtual function example
          {
               cout <<"Base class Window"<<endl;
          }
     };
     class CommandButton : public Window
     {
       public:
          void Create()
          {
              cout<<"Derived class Command Button -
Overridden C++ virtual function"<<endl;
          }
     };
INLINE FUNCTION IN C++
Inline Functions:
To have faster execution of functions we use
this...
1) Faster access
2) when function is called the definition is stored
in buffer. so control dont go out of function.
3) looping cannot be given
4) recursion is not possible
5) similar to macros
6) Debugging is not possible when compared to
macros.
main()
{
int a,b;
clrscr();
cout<<"enter 2 numbers";
cin>>a>>b;
if(compare(a,b)==0)
cout<<"Equal"<<endl;
else
cout<<"Not Equal"<<endl;
getch();
}
inline int compare(int a,int b)
{
if(a==b)
return 0;
else
return 1;
}
THIS KEYWORD IN C++
The this keyword is a pointer to the current
object.

All non-static member functions of a class


have access to this pointer.

It is a pointer to the object itself. You can


use it e.g. in a member function to point
member variables and member functions.
Or you can pass a pointer to the object inside
the class to some external (or internal) 
target. 

It holds the address of the object.


It is a keyword.
Used to invoke the object.
class one
{
public:
void check()
{
cout<<this<<endl;
}
};
void main()
{
one a,b,c,d;
a.check();
b.check();
c.check();
d.check();
getch();
}
MACROS IN C++
It defines a kind of function which, used in an
actual piece of code, looks exactly like any other
function call:

double yout,xin=3; 
yout = SquareOf(xin);

The problem is that SquareOf only pretends to


be a function call, while it is really something
completely different.
The formal syntax of a macro is:

#define name(dummy1[,dummy2]
[,...]) tokenstring

The symbols dummy1, dummy2, ... are


called dummy arguments (as usual, square
brackets indicate optional items).
There are a few additional rules such as that
the macro can extend over several lines,
provided one uses a backslash to indicate
line continuation ,

#define ThirdPowerOf(dummy_argument) \ 
         dummy_argument \ 
        *dummy_argument \ 
        *dummy_argument
SCOPE RESOLUTION
OPERATOR IN C++
The scope resolution operator (::) in C++ is
used to define the already declared member
functions (in the header file with the .hpp or
the .h extension) of a particular class.

In the .cpp file one can define the usual


global functions or the member functions of
the class.
To differentiate between the normal functions and
the member functions of the class, one needs to
use the scope resolution operator (::) in between
the class name and the member function name
i.e. ship::foo() where ship is a class and foo() is a
member function of the class ship.
The other uses of the resolution operator is to
resolve the scope of a variable when the
same identifier is used to represent a global
variable, a local variable, and members of one
or more class(es). If the resolution operator is
placed between the class name and the data
member belonging to the class then the data
name belonging to the particular class is
referenced.
If the resolution operator is placed in front of the
variable name then the global variable is
referenced. When no resolution operator is placed
then the local variable is referenced.
#include <iostream> 
using namespace std; 
int n = 12; // A global variable 
int main()
{
int n = 13; // A local variable
cout << ::n << endl; // Print the global variable: 12

cout << n << endl; // Print the local variable: 13


}
THANK Y
O U

You might also like