Differences Between Static Data And Objects Information Technology Essay

Published: November 30, 2015 Words: 1222

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 shop

{

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 static example::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) The main difference between friend function and member function are given below:-

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 preceeded with the keyword friend .

SYNTAX:-

class stu

{

……

public:

…..

friend void abc(); //declaration

};

EXAMPLE:-

#include<iostream.h>

#include<conio.h>

class cam

{

int m,n;

public:

void setvalue()

{

m=30; n=10;

}

friend int ret (cam d); //friend function declaration

};

int ret (cam d)

{

return int(d.m+d.n)/2; }

int main()

{

cam Z;

Z.setvalue();

cout<<"mean value=" <<ret(Z);

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 bike

{

Void horn(); //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>

#include<conio.h>

Class Fibo

{

Private :

int f,f1,fib;

Public :

Fibo ( ) // constructor

{

f=0;

f1=1;

fib=f+f1;

}

Fibo (fibo &ptr) // copy construtor

//COPY CONSTRUTOR takes reference to an object of same class as argument.

{

f=ptr.f;

f1=ptr.f1;

fib=prt.fib;

}

Void increment ( )

{

f=f1;

f1=fib;

fib=f+f1;

}

Void showdata ( )

{

Cout<<fib<<'/t';

}

};

Void main( )

{

Fibo number ;

Fibo num(&number);

For(int i=0;i<=15;++i)

{

Number.showdata();

Num.showdata();

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 AAA

{

char *name;

int a;

public:

AAA( )

{

a=0;

name=new char[a+1];

}

AAA(char *s)

{

a=strlen(s);

name=new char[a+1];

strcpy(name,s);

}

void showdata() { cout<<name;}

int main()

{

char *first="saba symbyol";

AAA name1(first),s1,s2;

s1.showdata();

}

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>

#include<conio.h>

class drink

{

public:

Food() {

cout << "drink's constructor" << endl; }

};

class Bar : public drink

{

public:

Bar() { cout << "Bar's constructor" << endl; }

};

int main()

{

// a good boy ;)

Bar bar;

}

The object bar is constructed in two stages: first, the Food constructor is invoked and then the Bar constructor is invoked. The output of the above program will be to indicate that Food'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 that can take arguments are called parameterized constructors.

EXAMPLE:-

Class ABC

{

Int m, n;

Public :

ABC (int x, int y); // parameterized constructor

};

ABC::ABC(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 XYZ

{ int m,n;

public:

XYZ( ) { m=0; n=0; } //consructor 1

XYZ(int a,int b) { m=a;n=b; } //constructor 2

XYZ(integer &i) { m=i.m; n=i.n; } //constructor 3

};

Statement

XYZ I1; //automatically invoke the 1st constructor

XYZ I2(10,20); //call the 2nd constructor

XYZ I3(I2); // we 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[memory] is allocated for member function when objects are created . only space for member variables are allocated separately 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 in order 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 function 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 we want dynamic binding. To get the effect of a virtual friend,

the friend function should call a hidden (usually protected:) virtual[20]

member function .