SIGNIFICANCE OF STATIC DATA MEMBERS:
Static members are those which are independent of classes' i.e. in case of static Functions objects are not required for allocation of memory. It is directly called by
Class name::object name
It is normally used to maintain values common to the entire class.
One copy of that member is created for the entire class and is shared by all the objects of that class.
It is visible only within the class, but its lifetime is the entire program.
Example:
#include<iostream.h>
class test
{
private:
static int counter;
public:
test()
{
counter++;
}
static void display()
{
cout<<"\n count:"<<counter;
}
};
int test::counter=0;
void main()
{
test t1;
t1.display();
test t2;
t2.display();
test t3;
t3.display();
getch();
}
SIGNIFICANCE OF STATIC DATA OBJECTS:
Static data objects not actually created it is static means it is independent of objects.
Q.2. Differentiate between the Friend Function and the Member Function.
Solution 2:
FRIEND FUNCTION:
Friend function provides user the opportunity to access the private members of the class from outside the class or from another class.
It can either be declared in the private part or public part of a class.
Example:
#include<iosream.h>
Class sample;
Class test
{
Private:
Int x;
Public:
Test()
{
X=50;
}
Friend int access(test,sample);
};
Class sample
{
Private:
Int y;
Public:
Sample()
{
Y=500;
}
Friend int access(test,sample);
};
Int access(test a,sample b)
{
Return (a.x+b.y);
}
Int main ()
{
Test obj1;
Sample obj2;
Int z=access (obj1,obj2);
Cout<< "the total is :";<<z;
Return 0;
}
MEMBER FUNCTION:
It can be defined either inside the class definition or outside the class definition but it cannot be access other class data members.
It incorporates a member function identity label in the header. This level tells the compiler which class the function belongs to.
Example:
#include<iostream.h>
Class person
{
Int age;
Float sal;
Public:
Void getage(void)
{
Cout<< "enter the age";
Cin>>age;
}
Void getsal(void)
{
Cout<< "enter the salary";
Cin>>sal;
}
Void show(void)
{
Cout<< "age is "<<age;
Cout<< "salary is"<<sal;
}
};
Void main()
{
Person obj;
Obj.getage();
Obj.getsal();
Obj.show();
getch();
}
Q.3. Discuss the use of the Copy Constructor, Give an example also.
Solution 3:
COPY CONSTRUCTOR:
It is used to declare and initialize an object from another object.
It helps to prevent problems that might occurs when one object is used to initialize another.
Example:
#include <iostream.h>
struct A
{
int i;
A() : i(10) { }
};
struct B
{
int j;
B() : j(20) {
cout << "Constructor B(), j = " << j << endl;
}
B(B& arg) : j(arg.j)
{
cout << "Copy constructor B(B&), j = " << j << endl;
}
B(const B&, int val = 30) : j(val)
{
cout << "Copy constructor B(const B&, int), j = " << j << endl;
}
};
struct C
{
C() { }
C(C&) { }
};
int main()
{
A a;
A a1(a);
B b;
const B b_const;
B b1(b);
B b2(b_const);
const C c_const;
C c1(c_const);
}
Q.4. Justify the Statement that "the Dynamic Constructor helps in utilizing the memory efficiently.
Solution 4:
The constructor used to assign the memory to object at the time of creation is known as dynamic constructor.
When the objects data member size is not the same, it will enable a program to allocate the right and of memory during execution for each object.
Example:
#include<iostream.h>
Class linkedlist
{
Private:
Char isnodeinto[20]';
Linkedlist*ipnextpointer;
Public:
Linkedlist();
};
Linkedlist::linkedlist()
{
Isnodeinto==newchar[20];
Ipnextpointer=null;
}
Void main()
Linkedlist linkedobj;
-------------
-------------
}
PART B
Q.1. Illustrate with the help of an example, the use of the Initialization List.
Solution 1:
The initialization list helps to initialise default values to the data members. This can be done using constructors. Constructors provide legal initial values to the members of the class.
Example:
Class super
{
Int x;
Int y;
Super(int m, int n)
{
x=m;
y=n;
}
Void display()
{
Cout<<"The values are "<<x<<y;
}
};
Int main()
{
Super ob1=super(4,5);
Ob1.display();
}
Q.2. . Discuss the need of the Parameterized Constructors, discuss the various functions of the constructors with their declaration in the class.
Solution 2:
When the objects data member size is not the same, it will enable a program to allocate the right and of memory during execution for each object.
In practice it is necessary to initialize the various data elements of different objects with different values when they are created so to achieve this objective by passing argument to the constructor function when the objects are created
It allows us to avoid having to make an additional function call simply to initialize one or more variable in an obect.
Various functions of constructor:
Class integer
{
Int m,n;
Public:
Integer(int x,int y) //parameterized constructor
};
Integer::integer(int x,int y)
{
M=x,n=y;
}
When a constructor has been parameterized, the object declaration statement such as
Integer int1;
may not work. So we must pass the initial value as argument to the constructor function when a object is declared by-
Calling the constructor explicitly
Calling the constructor implicitly
Integer int1=integer (0,100) //explicitly
Integer int1(0,100); //implicitly
The constructor function also defined as inline function.
A constructor can accept a references,so its own class as a parameter in such class ,it is called copy constructor.
It can be of any type except that of the class to which it is belongs.
Q.3. Is there any difference in allocation of memory to the members of the class and object.
Solution 3:
No there is no difference in allocation of memory to the members of the class and object because the main function are created and placed in the memory space only once when they are defined as a part of a class specification , since all the objects belonging to that class use the some memory function. No separate space is allocated for the member function when the objects are created. Only space for member variable is allocated separately for each object.
Q.4. Discuss the various benefits and limitations of the Friend Functions.
Solution 4:
Benefits of friend function:
Friend function is used to link two or more classes data members.
It can access the private members of the class from outside the class definition.
Friend function can be used to increase the versatility of overloaded operators.
Limitations:
The function declared must be prefixed by the keyword friends.
It can only be defined in the private or public part of the class.
A friend function generally requires object of the class as its argument.