You are on page 1of 7

Q4 (a) Bitwise And operator

(b) Explicit Typecasting


C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion. We have already seen two notations for explicit type conversion: functional and c-like casting:

short a=2000; int b; b = (int) a; b = int (a);

// c-like cast notation // functional notation

#include <iostream> using namespace std; void main() { int a; float b,c; cout << "Enter the value of a:"; cin >> a; cout << "Enter the value of b:"; cin >> b; c = float(a)+b; cout << "The value of c is:" << c; }

( c ) Array of pointers
The concept of array is very much bound to the one of pointer. In fact, the identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the address of the first element that it points to, so in fact they are the same concept. For example, supposing these two declarations:

1 int numbers [20]; 2 int * p;

The following assignment operation would be valid:

p = numbers;

After that, p and numbers would be equivalent and would have the same properties. The only difference is that we could change the value of pointer p by another one, whereas numberswill always point to the first of the 20 elements of type int with which it was defined. Therefore, unlike p, which is an ordinary pointer, numbers is an array, and an array can be considered a constant pointer. Therefore, the following allocation would not be valid:

numbers = p;

Because numbers is an array, so it operates as a constant pointer, and we cannot assign values to constants. Due to the characteristics of variables, all expressions that include pointers in the following example are perfectly

valid:

// more pointers #include <iostream> using namespace std; int main () { int numbers[5]; int * p; p = numbers; *p = 10; p++; *p = 20; p = &numbers[2]; *p = 30; p = numbers + 3; *p = 40; p = numbers; *(p+4) = 50; for (int n=0; n<5; n++) cout << numbers[n] << ", "; return 0; }

In the chapter about arrays we used brackets ([]) several times in order to specify the index of an element of the array to which we wanted to refer. Well, these bracket sign operators[] are also a dereference operator known as offset operator. They dereference the variable they follow just as * does, but they also add the number between brackets to the address being dereferenced. For example:

a[5] = 0; *(a+5) = 0;

// a [offset of 5] = 0 // pointed by (a+5) = 0

These two expressions are equivalent and valid both if a is a pointer or if a is an array.

( d ) Arithmetic if operator
Arithmetic if

Operator

Symbol

Form

Operation

Arithmetic if

?:

c?x:y

If c is nonzero (true) then evaluate x, otherwise evaluate y

The arithmetic if operator (?:) is also known as the conditional operator, and it is C++s only ternary operator (it takes 3 operands). The ?: operator provides a shorthand method for doing a particular type of if/else statement. If/else statements in the following form: if (condition) x = some value else x = some other value can be rewritten as: x = (condition) ? some value : some other value;

For example, to put the larger of values x and y in variable z, we could write this:

if (x > y) z = x; else z = y;

Or this: z = (x > y) ? x : y;

Program::
#include <iostream.h> int main() { int x, y, z; cout << "Enter two numbers.\n"; cout << "First: "; cin >> x; cout << "\nSecond: "; cin >> y; cout << "\n"; if (x > y) z = x; else z = y; cout << "z: " << z; cout << "\n"; z = (x > y) ? x : y;

cout << "z: " << z; cout << "\n"; return 0; } Output: Enter two numbers. First: 5 Second: 8 z: 8 z: 8

Ans-5
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C++ uses call by value to pass arguments. In general, this means that code within a function cannot alter the arguments used to call the function. Consider the function swap()definition as follows.

// function definition to swap the values. void swap(int x, int y) { int temp; temp = x; /* save the value of x */ x = y; /* put y into x */ y = temp; /* put x into y */ return; }
Now let us call the function swap() by passing actual values as in the following example:

#include <iostream> using namespace std; // function declaration void swap(int x, int y); int main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; // calling a function to swap the values. swap(a, b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; }
When the above code is put together in a file, compiled and executed, it produces following result:

Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :100 After swap, value of b :200
Which shows that there is no change in the values though they had been changed inside the function.

WAYS TO REMEDY THE PROBLEM

Call by Reference

A reference provides an alias- an alternate name- for the variable. While passing reference arguments, a reference to the variable in the calling program is passed. The above program can be modified using reference arguments in the following way: //This program swaps the values in the variable using function containing reference arguments #include<iostream.h> void swap(int &a, int &b); void main() { int a,b; cout<<"Enter two numbers "<<endl; cin>>a; cin>>b; swap(a, b); cout<<"In main "<<a<<" "<<b<<endl; } void swap(int &a, int &b) { int t; t = a; a = b; b = a; cout<<"In swap "<<a<<" "<<b<<endl; } Reference arguments are indicated by an ampersand (&) preceding the argument:

Ans-6
#include<iostream.h> #include<conio.h> #include<stdlib.h> template<class Type> class Queue { Type s[10]; int rear,front,n; public: Queue() { cout<<"\n\tEnter the Queue Size : ";

cin>>n; rear=front=n-1; } void insert(Type elt) { if((rear+1)%n!=front) { rear=(rear+1)%n; s[rear]=elt; } else cout<<"\n\tQueue is full.Can't insert "<<elt<<endl; } void remove() { if(front==rear) cout<<"\n\tQueue is empty.\n"; else { front=(front+1)%n; cout<<"\n\tRemoved elt : "<<s[front]; } } void que_operation(); void display(); }; template<class Type> void Queue<Type> :: display() { if(rear!=front) { cout<<"\n\t\tQueue Content :\n\n\t"; for(int i=(front+1)%n;;i=(i+1)%n) { cout<<s[i]<<"\t"; if(i==rear) break; } } else cout<<"\n\tEmpty Queue Can't Be Print\n"; } template<class Type> void Queue<Type> :: que_operation() { int choice=1,i; Type elt; while(choice>0 && choice<3) { cout<<"\n\n\t1.Insert\t2.Remove\tAny Key To Exit\n\tChoice : "; cin>>choice; switch(choice) { case 1 : //insert cout<<"\n\tEnter the Elt to insert : "; cin>>elt; insert(elt); display(); break; case 2 : //remove remove(); display();

break; } } } void main() { clrscr(); cout<<"\n\t\tQUEUE OPERATION USING TEMPLATE\n\n"; cout<<"\n\t INT\n"; Queue<int> que1; cout<<"\n\t FLOAT\n"; Queue<float> que2; int ch; while(1) { cout<<"\n\t\t\tQUEUE OPERATION \n\n"; cout<<"\t1.INT QUEUE\t2.FLOAT QUEUE\tAny Key To Exit\n\tChoice : "; cin>>ch; switch(ch) { case 1 : //perform queue operation on int queue que1.que_operation(); break; case 2 : //float que2.que_operation(); break; default : exit(0); } } }

You might also like