Ans) Static data members are data and functions that are associated with the class itself, rather than with the objects of the class. A static data member is a single, shared object accessible to all objects of its class type. A data member is made static by prefixing the data member declaration within the class body with the keyword static or prefixing the function declaration with the keyword static
A static function cannot access any other member variables of the object. One does not need to create a class object for accessing a static member or a static function; a static function can be implemented as
Class shoppingcart
Private:
Static int total items; //static data member
Public:
Static int get total items(); //static member function
};
Static objects:-
Static objects are the objects of the static class member.
Example:
Class static example
{
Int data;
Static int staticvar; //static variable declared
Public:
//definition of member function
};
Int staticexample::staticvar=0; //static variable initialized to 0
Here, unlike member variables only one copy of static variable exists in the memory for all objects of that class. Thus, all objects share one copy of static variable in memory.
Q.2. Differentiate between the Friend Function and the Member Function .
Ans) A friend function is used for accessing the non public/private members of a class.The friend function is written as any other normal function, except the function declaration of these functions is preceded with the keyword friend .
SYNTAX:-
class ABC
{
……
public:
…..
friend void xyz(); //declaration
};
EXAMPLE:-
include<iostream.h>
class sample
{
int a,b;
public:
void setvalue()
{
a=10; b=20;
}
friend int mean (sample s); //friend fn declaration
};
int mean (sample s)
{
return int(s.a+s.b)/2; }
int main()
{
sample X;
X.setvalue();
cout<<"mean value=" <<mean(X);
return 0;
}
MEMBER FUNCTION:- Basically member functions are declared inside class body. The function declaration introduces the function in the class and the function definition contains the function code.
EXAMPLE:-
#include<iostream.h>
#include<conio.h>
Class car
{
Void honk(); //member function declared inside class
{
Cout<<"BEEP BEEP!!";
}
};
Q.3. Discuss the use of the Copy Constructor, Give an example also.
Ans) A copy constructor is used to declare and initialize object of another class.
Copy constructor is always used when the compiler has to create a temporary object of a class. The copy constructor is used in the following situations:-
=>the initialization of an object by another object of the same class.
=>return of object as a function value.
=> starting the object as by the value parameters of a function.
EXAMPLE:-
//Fibonacci series using copy constructor
#include<iostream.h
Class fibonacci {
Private :
int f0,f1,fib;
Public :
Fiboancci () // constructor
{
f0=0;
f1=1;
fib=f0+f1;
}
Fibonacci (fibonacci &ptr) // copy construtor
//CC takes reference to an object of same class
as argument
{
f0=ptr.f0;
f1=ptr.f1;
fib=prt.fib;
}
Void increment ( )
{
f0=f1;
f1=fib;
fib=f0+f1;
}
Void display ( )
{
Cout<<fib<<'/t';
}
};
Void main( )
{
Fibonacci number ;
Fibonacci num(&number);
For(int i=0;i<=15;++i)
{
Number.display();
Num.display();
Number.increment();
Num.increment();
}
}
Q.4. Justify the Statement that "the Dynamic Constructor helps in utilizing the
memory efficiently.
Ans) Dynamic constructor are used to allocate memory while creating objects. This will enable the system to allocate the right amount of memory for each object when the object are not of same size ,thus resulting in saving the memory. Allocation of memory to objects at the time of their construction is known as dynamic construction of objects.
EXAMPLE:-
class String
{
char *name;
int length;
public:
String()
{
length=0;
name=new char[length+1];
}
String(char *s)
{
length=strlen(s);
name=new char[length+1];
strcpy(name,s);
}
void display() { cout<<name;}
int main()
{
char *first="Joseph";
String name1(first),s1,s2;
s1.display();
}
PART B
Q.1. Illustrate with the help of an example, the use of the Initialization List.
Ans) In C++, whenever an object of a class is created, its constructor is called. But that is not all--its parent class constructor is called, as are the constructors for all objects that belong to the class. By default, the constructors invoked are the default ("no-argument") constructors. Moreover, all of these constructors are called before the class's own constructor is called.
#include <iostream.h>
class Foo
{
public:
Foo() { cout << "Foo's constructor" << endl; }
};
class Bar : public Foo
{
public:
Bar() { cout << "Bar's constructor" << endl; }
};
int main()
{
// a lovely elephant ;)
Bar bar;
}
The object bar is constructed in two stages: first, the Foo constructor is invoked and then the Bar constructor is invoked. The output of the above program will be to indicate that Foo's constructor is called first, followed by Bar's constructor.
Q.2. Discuss the need of the Parameterized Constructors, discuss the various functions of the constructors with their declaration in the class .
ANS)Basically parameterized constructors initializes the data members of all the objects.it does so by passing arguments to the constructor function when the objects are created.The constructors tht can take arguments are called parameterized constructors.
EXAMPLE:-
Class integer
{
Int m, n;
Public :
Integer (int x, int y); // parameterized constructor
};
Integer::integer(int x,int y)
{
m=x;
n=y;
}
The various function of constructor is:-
To assign values to the objects data members
Establishing invariant of the class
Leaving objects in valid state
Initializing immutable objects
MULTIPLE CONSRUTORS IN A CLASS
Class integer
{ int m,n;
public:
integer() { m=0; n=0; } //consructor 1
integer(int a,int b) { m=a;n=b; } //constructor 2
integer(integer &i) { m=i.m; n=i.n; } //constructor 3
};
Statement
integer I1; //automatically invoke the 1st constructor
integer I2(10,20); //call the 2nd constructor
integer I3(I2); // wud invoke 3rd constructor which copies the value of I2 into I3
Q.3. Is there any difference in allocation of memory to the members of the class and
object.
Ans) Memory space for objects are allocated when they are declared and not when class is specified.the member function of a class are created and placed in the memory space only once when they are defined as a part of class specification. Since all objects belonging to the same class use the same member function, no separate space is allocated for member function when objects are created . only space for member variables are allocated seperaately for each object.
Q.4. Discuss the various benefits and limitations of the Friend Functions.
ANS)benifits of friend functions includes:
We use friend function inorder to access the non public members of a class,
It is not in scope of class to which it has been declared as friend
Since it is not in scope of class, it can not be called using object of that class
It is invoked like normal function without the help of any object.
The friend func tion has object of class as its arguments.
Unlike member functions, it cannot access data members directly and has to use object name with dot operator
LIMITATIONS OF FRIEND FUNCTIONS:-
disadvantage of friend functions is that they require an extra line
of code when you want dynamic binding. To get the effect of a virtual friend,
the friend function should call a hidden (usually protected:) virtual[20]
member function