The Significance Of The Static Data Members Information Technology Essay

Published: November 30, 2015 Words: 2140

Once you define a static data member, it exists even though no objects of the static data members class exist. In the above example, no objects of class X exist even though the static data member X::i has been defined.

Static data members of a class in namespace scope have external linkage. The initializer for a static data member is in the scope of the class declaring the member.

A static data member can be of any type except for void or void qualified with const or volatile. You cannot declare a static data member as mutable. You can only have one definition of a static member in a program. Unnamed classes, classes contained within unnamed classes, and local classes cannot have static data members.

Static data members and their initializers can access other static private and protected members of their class.

Classes can contain static member data and member functions. When a data member is declared as static, only one copy of the data is maintained for all objects of the class. Static data members are not part of objects of a given class type; they are separate objects. As a result, the declaration of a static data member is not considered a definition. The data member is declared in class scope, but definition is performed at file scope.

For the static member to exist, it is not necessary that any objects of the class type exist. Static members can also be accessed using the member-selection (. and ->) operators. Static data members are subject to class-member access rules, so private access to static data members is allowed only for class-member functions and friends. These rules are described in Member-Access Control. The exception is that static data members must be defined in file scope regardless of their access restrictions. If the data member is to be explicitly initialized, an initializer must be provided with the definition.

Example of a program: - To declare the members of the class as static.

#include<iostream.h>

#include<conio.h>

class rectangle

{

private:

int l,b;

static int counter; NOTE THAT HERE A

public: DATA MEMBER HAS

void getvalues(); BEEN DECLARED AS

void showarea(); STATIC.

void fcount()

{

Counter++;

Cout<<count;

}

};

Void rectangle :: getvalues()

{

cout<<"enter length";

cin>>l;

cout<<"enter breadth";

cin>>b;

}

Void rectangle :: showarea()

{

Cout<<"area is"<< l*b

}

Int rectangle :: count=0;

void main()

{

clrscr();

rectangle r1;

r1.getvalues();

r1.showarea();

getch();

}

Output:

Enter length: 10

Enter breadth: 10

Area: 100

ANS2:- Difference between friend function and member functions are:-

Friend Function: A friend function is used for accessing the non-public members of a class. A class can allow non-member functions and other classes to access its own private data, by making them friends. Thus, a friend function is an ordinary function or a member of another class.

A friend function is used in object-oriented programming to allow access to private or protected data in a class from outside the class. Normally a function which is not a member of a class cannot access such information; neither can an external class. Occasionally such access will be advantageous for the programmer; under these circumstances, the function or external class can be declared as a friend of the class using the friend keyword. The function or external class will then have access to all information - public, private, or protected - within the class.

Member function - is normally referred to a C++Style method declared/defined inside of a C++ class. The scope for such member functions is the class. They are not accessible outside the class and are only accessible thru an object/instance of such a class.

A friend function is not a member of a class. But it can access the private data members of the class like a member function of the class.

A member function being a member of the class can easily access the private members of the class indirectly.

A friend function cannot be called using the object of the class.

A member function is called using the object of the class.

Difference based upon a program

#include<iostream.h>

#include<conio.h>

class square

{

private:

int a;

public:

friend void getvalues();

void showarea();

};

Void getvalues(square ob1) //defining a friend function

{

cout<<"enter the side of square";

cin>>ob1.a;

}

Void rectangle :: showarea() //defining a member function

{

Cout<<"area is"<< a*a

}

void main()

{

clrscr();

square ob1;

getvalues(ob1);

ob1.showarea();

getch();

}

Output:

Enter side of a square: 10

Area: 100

NOW IN THE ABOVE PROGAM GETVALUES() IS A FRIEND FUNCTION & SHOWVALUES() IS A MEMBER FUNCTION & BOTH THESE FUNCTIONS ARE DEFINED DIFFERENTLY OUTSIDE THE CLASS.

Ans 3) Definition of a copy constructor.

Constructor which initializes its object member variables with values from another object of the same class during declaration. If you don't implement one in your class then compiler implements one for you.

Copy constructors are called in following cases:

When a function returns an object of that class by value

When the object of that class is passed by value as an argument to a function

When you construct an object based on another object of the same class

When compiler generates a temporary object

E.g. OF A PROGRAM IN WHICH THE CONTENTSS OF OBJECT A COPIED TO OBJECT B , OBJECT C.

#include<iostream.h>

#include<conio.h>

Class cl

{

Private:

Int id;

Public:

cl() // default constructor

{

val=0 ;}

cl (int a)

{ val=a; }

Cl ( cl &v)// COPY CONSTRUCTOR

{

val=val.v;

}

Void show()

{

Cout<<"value is"<<val;

}

};

Void main()

{

Cl A(50); //here object A is created

Cl B= A; //here contents of A are copied to B, copy constructor called

Cl C(A); //here contents of A are copied to C, copy constructor called

Cout<<"id of A is"<<A.show();

Cout<<"id of B is"<<B.show();

Cout<<"id of C is"<<C.show();

Getch();

}

Output:

Id of A: 50

Id of B: 50

Id of C: 50

ANS4 :) The Dynamic Constructor helps in utilizing the memory efficiently. This can be proved in this manner.

The constructors can be used to allocate memory while creating objects. This will enable a system to allocate the right amount of memory foe each object when the object size is not of the same, thus preventing the wastage of memory & hence resulting in the saving of the memory. Allocation of memory to object at the time of creation of objects is known as dynamic constructors. The operator used to save memory at run time is known as new operator.

This is proved with help of a program:-

#include<iostream.h>

#include<conio.h>

#include<string.h>

Class str // class name is str

{

Private:

Char *nam; // data members

Int len; // data members

Public:

Str () // default constructor called

{ len=0;

Nam=new char[len+1];

}

Str(char *n);

Nam=new [len+1]; // use of dynamic constructor

Strcpy (nam, n);

}

Void show(void)

{

Cout<<nam<<"\n";

Void join(str &x,str &y);

};

Void str :: join(str &x,str &y)

{

len=x.len + y.len;

Nam=new char[len+1]; //again use of dynamic constructor

Strcpy(nam , a.nam);

Strcpy(nam , b.nam);

}

Void main()

{

Char * one="hello";

Str nam1(one),nam2("how"),nam3("areu"),ob1,ob2; //objects created

Ob1.join(nam1,nam2);

Ob2.join(ob1,nam3);

Nam1.show();

Nam2.show();

Nam3.show();

Ob1.show();

Ob2.show();

Getch();

}

Output:

Hello

How

Areu

Hello how areu

Part-B

Ans1:- The use of initialization list is:-

If we have a parent class that needs to take arguments to its constructor then the role of initialization lists come into play. An initialization list immediately follows the constructor's signature, separated by a colon:

Using Initialization Lists to Initialize Fields

In addition to letting you pick which constructor of the parent class gets called, the initialization list also lets you specify which constructor gets called for the objects that are fields of the class.

If you have a field that has no default constructor (or a parent class with no default constructor), you must specify which constructor you wish to use.

Before the body of the constructor is run, all of the constructors for its parent class and then for its fields are invoked. By default, the no-argument constructors are invoked. Initialization lists allow you to choose which constructor is called and what arguments that constructor receives.

If you have a reference or a const field, or if one of the classes used does not have a default constructor, you must use an initialization list

Example

#include<iostream.h>

#include<conio.h>

class square

{

private:

int a;

public:

void getvalues()

{

cout<<"enter the side of square";

cin>>a;

}

Square (int x): a (x) // parameterized constructor with initialised list

{ }

void showarea()

{

Cout<<"area is"<< l*l

}

};

void main()

{

clrscr();

square r1;

square r1(10); // parameterized constructor called

r1.getvalues();

r1.showarea();

getch();

}

Output:

Enter side of square:10

Area:100

Ans2:-

Need of parameterized constructors:-

The main need of parameterized constructors is to initialize the values of arguments because these constructors are called parameterized because of this very reason.

The parameterized constructors can pass value into the constructors who have different parameters in the constructor functions.

These constructors always pass initial values or stored variables, as arguments into the constructor function when the object is created.

Functions of constructor:-

They are declared in the public specifier & are invoked automatically when objects are created. A constructor enables an object to initialize itself when the objects are created. This is known as automatic initialization of objects. Constructors are also having default arguments

Constructors are also having the feature of function overloading means one function can be used in many interfaces. Therefore we can create multiple constructors.

A c++ program requires a return type to work errorless but using constructors we need not to give return type before a constructor because they don't return any values.

E.g of a program giving the basic idea of declaring a constructor.

#include<iostream.h>

#inxlude<conio.h>

Class cube

{

Private:

Int a ,b ,c;

Public:

Cube ( int , int , int );

Void showvalues();

};

Cube :: cube( int x, int y, int z) // declaring a constructor

{

a=x; b=y; c=z;

}

Void cube:: showvalues()

{

Int area=a*b*c;

Cout<<"area is"<<area;

}

Void main()

{

Cube c1(2,3,4); // constructor is called

C1.showvalues();

Getch();

}

Output: x=2 y=3 z=4

Area is 24

Ans3

No there is not any difference in the allocation of memory to the members of the class and object.

Because objects are called the instances of the classes and as soon as the object is created in the program, memory is allocated to the data members of the class and the member functions of the class & the data is stored inside some empty memory buffer.

So there is no logical difference between allocation of memory to data members of the class and the objects of the class

Ans 4:- Benefits of using Friend function are:-

A friend function is a special function in oops which in spite of not being member functions of a class has a privilege for accessing the private and protected data of a class.

A friend function is a non member function of a class that is declared as a friend using the keyword "friend" inside the class. By declaring a function as a friend, all the access permissions are given to the function.

As, when a data is declared as private inside a class, then it is not accessible from outside the class. A function that is not a member or an external class will not be able to access the private data. A programmer may have a situation where he or she would need to access private data from non-member functions and external classes. For handling such cases, the concept of Friend functions is a useful tool.

Limitations of using friend functions in C++:

The keyword friend is placed only in the function declaration of the friend function and not in the function definition. .

It is possible to declare a function as friend in any number of classes.

When a class is declared as a friend, the friend class has access to the private data of the class that made this a friend. A friend function, even though it is not a member function, would have the rights to access the private members of the class.

it should not be defined in name of class nor scope resolution operator is used in its definition even the keyword friend is also not used while defining friend function.

Disadvantage of using a friend function is;-

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 member function.

The procedure of using friend function should be used with caution. If too many functions or external classes are declared as friends of a class with protected or private data, necessary data security may be compromised, as well as the encapsulation of separate classes in object-oriented programming.