Additional Features Of C++ | Manipulators, Namespace, Reference, Structure, Arguments, Function | With Example In C++ - LBM4

Recent Post

Thursday 19 September 2019

Additional Features Of C++ | Manipulators, Namespace, Reference, Structure, Arguments, Function | With Example In C++

Additional Features of C++

The additional features of C++/Object Oriented Programming are manipulators, namespace, reference, structure, arguments, and function which are discussed below with some programing examples.
C++

Manipulator

Manipulators are operators that are used to format the data display. The most commonly used manipulators are endl and setw. The endl manipulator, when used in an output statement, causes a linefeed to be inserted. It has same effect as using the newline character "\n" in C. For example
….
cout<< "LODGING" <<lexp<<endl;
…..
If we assume that value of variable lexp is 2000  then output appears is
LODGING 2000
It is not the ideal output. setw(n) manipulator eliminate this problem by specifying filed width. The value is right justified within field. For example
……
cout<<setw(11)<< "LODGING" <<setw(8)<<lexp<<endl;
……
output of this section is
LODGING   2000

Namespace

The namespace feature in C++ allow us to specify a scope with a name i.e. namespace is a named scope. The namespace feature helps us to reduce the problem of polluting the namespace. Namespace is defined as follows.
namespace nsn
{
int item;
void showitem(int it)
{
cout<<it;
}
}
The elements from the namespace are used as
cout<<nsn::item;

Pass By Reference

When an address of the variable/object is passed, the function works directly on the actual variable/object used in the call. This means that any changes made to the variable/object inside the function will reflect in actual variable/object. For example

return_type function_name(datatype &, datatype &)

Return By Reference

The primary reason of returning the value by reference is to use a function call on the leftside of the equal sign. For example

max(a,b)=-1

Here the function should return reference to variables, but not the values, then the function call will yield a reference to either a or b depending on their values.

Structure 

The value of structure variable can be passed as a parameter to a function. For example
struct stu
{
...
};
void show(stu);

Overloaded Function

Overloaded function appears to perform different operation depending on the kind of data sent to it. It performs one operation on one kind of data but another operation on a different kind. The reason behind using the overloaded function is because of its convenience to use the same function name for different operation.

void convert(); //takes no argument
void convert(int n); //takes one argument of type int
void convert(float,int); //takes two argument of type float and int

It uses the number of arguments, and their data types, to distinguish one function from another.

Inline Function

We know that function save memory space but take some extra time. If the functions are short, we may put in the function directly in the line with the code in the calling program. But the trouble with repeatedly inserting the same code is that you lose the benefits of the program organization and clarity that come with using function. If the function is very short, the instructions necessary to call it may take up as much space as the instructions within the function body, so that there is not only a time penalty but a space penalty as well. The solution to this is the inline function. This kind of function is written like a normal function in the source file but complies into inline code instead of into a function. Beside of this the source file remains well organized and easy to read, since the functions are shown as a separate entity. Functions that are very short, say one or two statements are candidates to be inlined.

inline(keyword) float(retutn type) convert(int n)
i.e
inline float convert(int n)

Here all we need is the keyword inline in the function definition.

Default Argument

The function can be called without specifying all its arguments. This won't work on just any function that is the function declaration must provide default values for those arguments that can be missed.
Let us take an example

void dearg(char ='?',int=25); //declaration with default arguments
int main()
{
dearg();
dearg('<');
dearg('>',30);
return 0;
}
void dearg(char ch,int n)
{
...
}
Here the function dearg( ) takes two arguments. It is called three times from main( ) program. The first time it is called with no arguments, second time with one argument and third time with two. The first and second provides default arguments, which will be used if the calling function doesn't supply them. The default argument follows an equal sign, which is placed directly after name. It is also possible to use variable name. If one argument is missing it assumed to the last argument as we show in second.

Examples:

WAP to set a structure to hold a date (mm,dd and yy), assign values to the members of the structure and print out the values in the format 11/28/2004 by function. Pass the structure to the function
#include<iostream>
using namespace std;
struct date
{
int year;
int month;
int day;
};
void data( date a)
{
    cout<<a.day<<"\\"<<a.month<<"\\"<<a.year;
}
main()
{
struct date a;
  cout<<"Enter day";
  cin>>a.day;
  cout<<"Enter month";
  cin>>a.month;
  cout<<"enter year";
  cin>>a.year;
   data(a);
return 0;
}

Define two namespaces: Square and Cube. In both the namespaces, define an integer variable named "num" and a function named "fun". The "fun" function in "Square" namespace, should return the square of an integer passed as an argument while the "fun" function in "Cube" namespace, should return the cube of an integer passed as an argument. In the main function, set the integer variables "num" of both the namespaces with different values. Then, compute and print the cube of the integer variable "num" of the "Square" namespace using the "fun" function of the "Cube" namespace and the square of the integer variable "num" of the "Cube" namespace using the "fun" function of the "Square" namespace.
#include<iostream>
using namespace std;
namespace square
{
    int num;
    int fun(int)
    {
     return num*num;
    }
}
namespace cube
{
    int num;
    int fun(int)
    {
      return num*num*num;
    }
}
int main()
{
   cout<<"Enter number to find square and cube";
   cin>>square::num;
   cout<<"Enter number to find square and cube";
   cin>>cube::num;
   cout<<cube::fun(cube::num)<<endl;
   cout<<square::fun(square::num);
   return 0;
}

Write a function that passes two temperatures by reference and sets the larger of the two numbers to 100 by using return by reference.
#include<iostream>
using namespace std;
int &temperature(int &temp1,int &temp2)
{
    if(temp1>temp2) return temp1;
    else return temp2;
}
main()
{
    int temp1,temp2,a;
    cout<<"Enter temperatures";
    cin>>temp1>>temp2;
    temperature(temp1,temp2)=100;
    cout<<temperature(temp1,temp2);
    return 0;
}

Assume that employee will have to pay 10 percent income tax to the government. Ask user to enter the employee salary. Use inline function to display the net payment to the employee by the company.

#include<iostream>
using namespace std;
float salary(float x=18000,float y=12)
{
    return x+(x*y/100);
}
main()
{
    cout<<"chief e.officer="<<salary(35000,9)<<endl;
    cout<<"infor.officer="<<salary(25000,10)<<endl;
    cout<<"system ana.="<<salary(24000)<<endl;
    cout<<"programmer="<<salary();
    return 0;
}

1 comment:

  1. Draw – The second deal of the cards, after might have} determined which cards to carry and which to discard. We all have our own rituals and beliefs in terms of|in relation to} the art of the sport, however sometimes these can fog your thinking. This is especially the case in terms of|in relation to} strategies. It’s essential not to fall into 바카라 사이트 the entice purchasing for} into any myths in terms of|in relation to} the power of strategies. We’ve outlined a number of the} most pervasive myths below so you can to|you probably can} steer clear. Though a lot of the above in all probability sounds like widespread sense, it really can’t be stressed sufficient how a lot these four simple steps can improve your overall video poker experience.

    ReplyDelete