Blog Archive

Computer Instructor C++ Friend Function & Friend class

 

 C++  Class:14

 Friend Function

     Friend Class

 

Friend Functions and Friend Class

Friend Function:

A friend function can access the private and protected data of a class. We declare a friend function using the friend keyword inside the body of the class.

class className {

    ... .. ...

    friend returnType functionName(arguments);

    ... .. ...

}


Similar to friend class, this function can access the private and protected members of another class. A global function can also be declared as friend as shown in the example below:

 

Friend Function Example

#include <iostream>
using namespace std;
class XYZ {
private:
   int num=100;
   char ch='Z';
 
   friend void disp(XYZ obj);
};
void disp(XYZ obj){
   cout<<obj.num<<endl; 
   cout<<obj.ch<<endl;
}
int main() {
   XYZ obj;
   disp(obj);
   return 0;
}

Output:

100
Z

1. The Friend Functions are used to access the member variables of the class indirectly.

2. It is a not a member function of a class. It is     a friend function of the class.

3. We define the friend function outside the class.

4. it can be declared either in public or private.

5. It has object as an argument.

6. It can access the member variables with the use of the objects of that class.

 

Friend Class:


A friend class is a class that can access the private and protected members of a class in which it is declared as friend. This is needed when we want to allow a particular class to access the private and protected members of a class.

 

we can also use a friend Class in C++ using the friend keyword. For example,

class ClassB;

 

class ClassA {

   // ClassB is a friend class of ClassA

   friend class ClassB;

   ... .. ...

}

 

class ClassB {

   ... .. ...

}

When a class is declared a friend class, all the member functions of the friend class become friend functions.

Since ClassB is a friend class, we can access all members of ClassA from inside ClassB.

However, we cannot access members of ClassB from inside ClassA. It is because friend relation in C++ is only granted, not taken.

 

Friend Class Example

 

#include <iostream>

using namespace std;

class classA {

private:

   char ch='A';

   int num = 11;

public:

   /* This statement would make class classB

    * a friend class of classA, this means that

    * classB can access the private and protected

    * members of classA class.

    */

   friend class classB;

};

class classB {

public:

   void disp(classA objA){

      cout<<objA.ch<<endl;

      cout<<objA.num<<endl;

   }

};

int main() {

   classA objA;

   classB objB;

   objB.disp(objA);

   return 0;

}

 

Output:

A
11



 

 

 

Data Structure - Expression Parsing ( Infix Notation, Prefix Notation, Postfix Notation)

 

Data Structure - Expression Parsing

 


 

The way to write arithmetic expression is known as a notation. An arithmetic expression can be written in three different but equivalent notations, i.e., without changing the essence or output of an expression. These notations are −

  • Infix Notation
  • Prefix (Polish) Notation
  • Postfix (Reverse-Polish) Notation

Infix Notation

We write expression in infix notation, e.g. a - b + c, where operators are used in-between operands. It is easy for us humans to read, write, and speak in infix notation but the same does not go well with computing devices. An algorithm to process infix notation could be difficult and costly in terms of time and space consumption.

Prefix Notation

In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish Notation.

Postfix Notation

This notation style is known as Reversed Polish Notation. In this notation style, the operator is postfixed to the operands i.e., the operator is written after the operands. For example, ab+. This is equivalent to its infix notation a + b.

 

 

Precedence

When an operand is in between two different operators, which operator will take the operand first, is decided by the precedence of an operator over others. For example −

As multiplication operation has precedence over addition, b * c will be evaluated first. A table of operator precedence is provided later.

Associativity

Associativity describes the rule where operators with the same precedence appear in an expression. For example, in expression a + b − c, both + and – have the same precedence, then which part of the expression will be evaluated first, is determined by associativity of those operators. Here, both + and − are left associative, so the expression will be evaluated as (a + b) − c.

Precedence and associativity determines the order of evaluation of an expression. Following is an operator precedence and associativity table (highest to lowest) −


The above table shows the default behavior of operators. At any point of time in expression evaluation, the order can be altered by using parenthesis. For example −

In a + b*c, the expression part b*c will be evaluated first, with multiplication as precedence over addition. We here use parenthesis for a + b to be evaluated first, like (a + b)*c.

Convert infix to Postfix

Example :1

A+B

Step 1

Expression string : A

Operator Stack : +

Remaining Expression : B

Step 2

Expression string : AB

Operator Stack : +

Postfix  Expression : AB+

Example :2

((A+B)-C*(D/E))+F

Step 1

Expression string :

Operator Stack : ((

Remaining Expression : A+B)-C*(D/E))+F

Step 2

Expression string : A

Operator Stack : ((

Remaining Expression : +B)-C*(D/E))+F

Step 3

Expression string : A

Operator Stack : ((+

Remaining Expression : B)-C*(D/E))+F

Step 4

Expression string : AB

Operator Stack : ((+

Remaining Expression : )-C*(D/E))+F

Step 5

Expression string : AB+

Operator Stack : (

Remaining Expression : -C*(D/E))+F

Step 6

Expression string : AB+

Operator Stack : (-

Remaining Expression : C*(D/E))+F

Step 7

Expression string : AB+C

Operator Stack : (-

Remaining Expression : *(D/E))+F

Step 8

Expression string : AB+C

Operator Stack : (-*

Remaining Expression : (D/E))+F

Step 9

Expression string : AB+C

Operator Stack : (-*(

Remaining Expression : D/E))+F

Step 10

Expression string : AB+CD

Operator Stack : (-*(

Remaining Expression : /E))+F

Step 11

Expression string : AB+CD

Operator Stack : (-*(/

Remaining Expression : E))+F

Step 12

Expression string : AB+CDE

Operator Stack : (-*(/

Remaining Expression : ))+F

Step 13

Expression string : AB+CDE/*-

Operator Stack :

Remaining Expression : +F

Step 14

Expression string : AB+CDE/*-

Operator Stack : +

Remaining Expression : F

Step 15

Expression string : AB+CDE/*-F

Operator Stack : +

Remaining Expression :

Step 16

Expression string : AB+CDE/*-F+

Operator Stack :

Postfix Expression : AB+CDE/*-F+

 


 

 

 

 

PG -TRB COMPUTER INSTRUCTOR C plus plus Class : 4 switch do while

  C++ switch The C++ switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C++. sw...