Page 115 - C++
P. 115
in protected section of class new_counter, so new_counter member function of new_counter can access all
private, protected and public section.
Extra info on Inheritance
Single Inheritance:
Class Derived: visibility-mode Base
{ };
Multiple Inheritance:
Class Derived: public Base1, private Base2 (if no mode is given it is private by default)
{ };
Multilevel Inheritance:
Class derived1 : protected base
{ };
Class dervide2 : public derived1
{ };
Constructors in Inheritance: when an object OF DERIVED CLASS IS CREATED then constructor
of all base classes is called first and then of derived class constructor. But if base class constructor requires
argument in constructor it is the responsibility of derived class to send arguments for the base class.
class base
{ int x;
public:
base(int a)
{ x=a;}
};
class derived: public base
{ public:
derived(int h): base (h) // derived class constructor takes argument to pass to base
{ }
}
3 (a) Write a function TRANSFER(int A[], int B[], int Size) in C++ to copy the elements of array A into array B
in such a way that all the negative elements of A appear in the beginning of B, followed by all the positive
elements, followed by all the zeroes maintaining their respective orders in array A. For example:
If the contents of array A are:
7, -23, 3, 0, -8,-3,4, 0
The contents of array B should be
-23 , -8, -3, 7, 3, 4, 0
Ans: void transfer(int a[], int b[], int size)
{ int k=0;
for(int i=0;i<size;i++)
if(a[i]<0)
b[k++]=a[i];
for(i=0;i<size;i++)
if(a[i]>=0)
b[k++]=a[i];
}
(b) Each element of the array A[8][6] is stored using 4 bytes of memory. If the element A[2][4] is stored at
location 936, find the address of A[5][1]. Assume that the array is stored column-wise.
Ans; Column major order //write formula (it carries marks)
a[8][6] n=8 m=6
W=4
A[2][4]=b+ w*((2-0)+8*(4-0))