Download - c++ Advance Template
-
8/12/2019 c++ Advance Template
1/25
C++ ADVANCE TEMPLATE
-
8/12/2019 c++ Advance Template
2/25
C++ Friend Functions
In this C++ tutorials, you will learn about friend functions, need for friend
function, how to define and use friend function and few important points
regarding friend function, explained with example.
Need for Friend Function:
As discussed in the earlier sections on access specifiers, when a data is
declared as private inside a class, then it is not accessible from outside theclass. A function that is not a member or an external class will not be able to
access the private data. A programmer may have a situation where he or she
would need to access private data from non-member functions and external
classes. For handling such cases, the concept of Friend functions is a useful
tool.
-
8/12/2019 c++ Advance Template
3/25
What is a Friend Function?
A friend function is used for accessing the non-public members of a class. A
class can allow non-member functions and other classes to access its own
private data, by making them friends. Thus, a friend function is an ordinary
function or a member of another class.
How to define and use Friend Function in C++:
The friend function is written as any other normal function, except the function
declaration of these functions is preceded with the keyword friend. The friend
function must have the class to which it is declared as friend passed to it inargument.
-
8/12/2019 c++ Advance Template
4/25
Some important points to note while using friend functions in C++:
The keyword friend is placed only in the function declaration of the friend
function and not in the function definition.
.
It is possible to declare a function as friend in any number of classes..
When a class is declared as a friend, the friend class has access to the private
data of the class that made this a friend.
.
A friend function, even though it is not a member function, would have the
rights to access the private members of the class..
It is possible to declare the friend function as either private or public.
.
The function can be invoked without the use of an object. The friend function
has its argument as objects, seen in example below.
-
8/12/2019 c++ Advance Template
5/25
#include
class exforsys
{private:
int a,b;
public:
void test(){
a=100;
b=200;
}friend int compute(exforsys
e1)//Friend Function Declaration with keyword friend and with the object of class
exforsys to which it is friend passed to it
};
-
8/12/2019 c++ Advance Template
6/25
int compute(exforsys e1)
{
//Friend Function Definition which
has access to private datareturn int(e1.a+e2.b)-5;
}
main(){
exforsys e;
e.test();
cout
-
8/12/2019 c++ Advance Template
7/25
The result is:295
The function compute() is a non-member function of the class exforsys. In order
to make this function have access to the private data a and b of class exforsys , it
is created as a friend function for the class exforsys. As a first step, the function
compute() is declared as friend in the class exforsys as:
friend int compute (exforsys e1)
The keyword friend is placed before the function. The function definition
is written as a normal function and thus, the function has access to the
private data a and b of the class exforsys. It is declared as friend insidethe class, the private data values a and b are added, 5 is subtracted from
the result, giving 295 as the result. This is returned by the function and
thus the output is displayed as shown above.
-
8/12/2019 c++ Advance Template
8/25
C++ Pure Virtual Function and Virtual Base Class
What is Pure Virtual Function:
Pure Virtual Function is a Virtual function with no body.
Declaration of Pure Virtual Function:
Since pure virtual function has no body, the programmer must addthe notation =0 for declaration of the pure virtual function in the
base class.
General Syntax of Pure Virtual Function takes the form
class classname //This denotes the base class of C++ virtual function{
public:
virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in
C++
};
-
8/12/2019 c++ Advance Template
9/25
To understand the declaration and usage of Pure Virtual Function, refer to
this example:
class Exforsys
{
public:
virtual void example()=0; //Denotes pure virtual
Function Definition
};
class Exf1:public Exforsys
{
public:
void example(){
cout
-
8/12/2019 c++ Advance Template
10/25
class Exf2:public Exforsys
{
public:
void example(){
coutexample();
}
-
8/12/2019 c++ Advance Template
11/25
Since the above example has no body, the pure virtual function example()
is declared with notation =0 in the base class Exforsys. The two derived
class named Exf1 and Exf2 are derived from the base class Exforsys. The
pure virtual function example() takes up new definition. In the main function,
a list of pointers is defined to the base class.
Two objects named e1 and e2 are defined for derived classes Exf1 and
Exf2. The address of the objects e1 and e2 are stored in the array
pointers which are then used for accessing the pure virtual function
example() belonging to both the derived class EXf1 and EXf2 and thus,the output is as in the above example.
The programmer must clearly understand the concept of pure virtual
functions having no body in the base class and the notation =0 isindependent of value assignment. The notation =0 simply indicates the
Virtual function is a pure virtual function as it has no body.
-
8/12/2019 c++ Advance Template
12/25
Some programmers might want to remove this pure virtual function from the
base class as it has no body but this would result in an error. Without the
declaration of the pure virtual function in the base class, accessing statements
of the pure virtual function such as, arra[0]->example() and arra[1]->example()
would result in an error. The pointers should point to the base class Exforsys.Special care must be taken not to remove the statement of declaration of the
pure virtual function in the base class.
-
8/12/2019 c++ Advance Template
13/25
In the above example, there are two derived classes Exf1 and Exf2 from the base
class Exforsys. As shown in the above diagram, the Training class is derived from
both of the derived classes Exf1 and Exf2. In this scenario, if a user has a member
function in the class Training where the user wants to access the data or member
functions of the class Exforsys it would result in error if it is performed like this:
-
8/12/2019 c++ Advance Template
14/25
class Exforsys
{
protected:
int x;};
class Exf1:public Exforsys
{ };
class Exf2:public Exforsys
{ };
class Training:public Exf1,public
Exf2
{public:
int example()
{
return x;
}};
-
8/12/2019 c++ Advance Template
15/25
F E l
-
8/12/2019 c++ Advance Template
16/25
class Exforsys
{
protected:
int x;
;
class Exf1:virtual public Exforsys
{ };
class Exf2:virtual public Exforsys
{ };
class Training:public Exf1,public
Exf2{
public:
int example()
{
return x;
}
For Example:
-
8/12/2019 c++ Advance Template
17/25
UnionsA unionis a user-defined data or class type that, at any given time, contains
only one object from its list of members (although that object can be an arrayor a class type).
union [tag] { member-list } [declarators];
[union] tag declarators;
-
8/12/2019 c++ Advance Template
18/25
Parameters
tag
Thetype name given to the union.member-list
List of the types of data the union can contain. See Remarks.
declarators
Declarator list specifying the names of the union.
Declaring a Union
Begin the declaration of a union with the union keyword, and enclose
the member list in curly braces:
#i l d tdi h
-
8/12/2019 c++ Advance Template
19/25
#include
int main()
{
union data
{char a;
int x;
float f;} myData;
int mode = 1;
myData.a = 'A';printf("Here is the Data:\n%c\n%i\n%.3f\n", myData.a, myData.x, myData.f
);
myData.x = 42;
mode = 2;
printf("Here is the Data:\n%c\n%i\n%.3f\n", myData.a, myData.x, myData.f
);
myData.f = 101.357;
mode = 3;
printf("Here is the Data:\n%c\n%i\n%.3f\n", myData.a, myData.x, myData.f);
-
8/12/2019 c++ Advance Template
20/25
if( mode == 1 )
printf("The char is being used\n");
else if( mode == 2 )
printf("The int is being used\n");else if( mode == 3 )
printf("The float is being used\n");
return 0;
Summary
Union allows same storage to be referenced in
different ways
Only one way is valid at any given time
-
8/12/2019 c++ Advance Template
21/25
Usage
access individual bytes of larger type
variable format input records (coded records)
sharing an area to save storage usageunions not used nearly as much as structures
-
8/12/2019 c++ Advance Template
22/25
enum in C++
enum(enumeration) is a user-defined type consisting of a set of
enumerators( enumerator --- named integer constant)
-
8/12/2019 c++ Advance Template
23/25
nue_acomany{Audi, BMW, Cadillac, Ford, Jaguar,
Lexus Maybach, RollsRoyce, Saab
};
-
8/12/2019 c++ Advance Template
24/25
e_acompany my_car_brand;
my_car_brand = RollsRoyce;//... if(my_car_brand == Ford)
cout
-
8/12/2019 c++ Advance Template
25/25