Thursday, 29 March 2018

Friend function in c++



FRIEND FUNCTION:

Access to the private and protected members of a class by non-member function is not allowed in normal. But it is possible to grant this access to non-member function.
A function which is declared as friend to a class have access to all its members i.e. private and protected as well as public.
To declare a function as friend, following syntax is used:
class class_name
             {      data members and functions;
               public:
                    friend ret_type function_name(arg_list);
                    member functions and variables;
              };
Sample Program:
#include<iostream>
using namespace std;
class sum
{
int a, b;
public:
friend int sumfrd(sum);                          // friend function
sum();                                                     //constructor
}
sum::sum()
{
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
}
int sumfrd(sum x)
{ return x.a+x.b;}

int main()
{   sum s;
     cout<<"Sum is: "<<sumfrd(s);
}


NOTE: friend is a keyword in C++

The circumstances in which friend functions are valuable:
  1. Friend functions make creation of some types of I/O functions easier.
  2. It is useful when we need to relate two classes in some sort.
  3. It is useful in overloading certain types of operators.

Data member and member function



DATA MEMBERS AND MEMBER FUNCTION IN C++

The variables declared in a class are called data members. These are also known as member variables.
Member functions are the functions declared within class. The member functions have the accessibility of every element of the class.



Restrictions applied to class members:
  1. A non-static member variable cannot have an initializer.
  2. No member can be an object of the class that is being declared.
  3. No member can be declared as auto, extern, or register.

Monday, 26 March 2018

ACCESS SPECIFIERS IN C++


 
Access specifiers in C++

Access specifiers, as the name suggests, specifies the access boundary or scope of a data member or function. In C++, there are three C++ keywords that act as access specifiers, these are:

a)private
b)public
c)protected

By default, the data members and functions are private to the class it is declared in. This means that are accessible only by the members of the class.
The data members and member functions declared public , are accessible by the objects, derived classes, friend class and friend function.
The data and functions declared protected are accessible by the class member functions and derived classes but not by objects or friend function(s) and classes.
  

Access specifiers in C++

Thursday, 22 March 2018

Classes in C++


Classes in C++


In C++, classes are created using class keyword. It declares and define a new type which is a combination of code and data.
The declaration of a class is similar to that of declaration of a structure.

A class not inheriting from any other class is declared as:
class class_name {                                                           
private data members and member functions
access specifier:
data and functions
access specifier:
data and functions
.
.
.
} object_list;
The object_list is optional. We can create objects of class in the main() function also.

Example:
class A
      {     int n;
        public:
            void get();
           void show();
       };
void A::get()           //accepting value of n
{     cout<<”Enter value of n:”;
       cin>>n;
}
void A::show()      // displaying value of n
{ cout<<”Value of n is:”<<n;
}
int main()
{      A a;             //Object of class A is created
       a.get();
     a.show();
}

OUTPUT:
Enter value of n:6
Value of n is:6

POLYMORPHISM IN C++

POLYMORPHISM IN C++ C++ achieves polymorphism through: i) Function overloading ii)Operator overloading By the...