Analysing C Plus Plus Static Functions Information Technology Essay
Static member functions have a class scope and they do not have access to the ‘this’ pointer of the class. When a member is declared as static, a static member of class, it has only one data for the entire class even though there are many objects created for the class. The main usage of static function is when the programmer wants to have a function which is accessible even when the class is not instantiated.
The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope. For example:
Class X
{ public:
Static int i;
};
Int X::i=0; //definition outside class declaration
Once you define a static data member, it exists even though no objects of the static data member’s class exist. In the above example, no objects of class X exist even though the static data member X::i has been defined.
Yes, a class can have static data members as well as static functions in a same program.
A non-static member function has a hidden argument called the this-pointer which points to the data of the specific instance of the class.
Static member functions can be called without reference to a specific instance of the class so it is completely what instance if any you mean. You do this by treating them as functions in a namespace with the same name as your class.
Here’s an illustrative example:
class CPerson
{
private:
int m_Age;
static int m_NumPeople = 0;
public:
void SetAge( int Age );
CPerson( void );
~CPerson( void );
static int GetAge( void ){ return this->m_Age }; // Wrong, this-> is ambiguous for static functions.
static int GetPeople( void ){ return m_NumPeople}; // Correct, people count is static.
};
There is only one integer m_NumPeople, it behaves like a global variable but the compiler will only let member functions access it since it is private.
Ans2) Static Data Members (C++)
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. (For more information, see Static Member Functions.)
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. These static members have external linkage.
Only one copy of a static data member of a class exists; it is shared with all objects of that class.
Static data members of a class in namespace scope have external linkage. Static data members follow the usual class access rules, except that they can be initialized in file scope. Static data members and their initializers can access other static private and protected members of their class. 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.
The declaration of a static data member in the member list of a class is not a definition. The definition of a static data member is equivalent to an external variable definition.
Ans3) a) //program with errors
# include<iostream.h>
class abc
{
private:
static int count;
public:
abc()
{
count=10;
}
void display() const
{
cout<<endl<<“count=” <<count;
}
};
void main()
{
abc a1,a2,a3;
a1.display();
a2.display();
a3.display();
}
//program without errors
# include<iostream.h>
#include<conio.h>
class abc
{
private:
static int count;
public:
abc();
{
count=0;
}
void displayconst();
{
cout<<endl<<“count=” <<count;
}
};
void main()
{
abc a1,a2,a3;
a1.display();
a2.display();
a3.display();
getch();
}
b) # include<iostream.h>
class example
{
private:
int data;
public:
example();
void display()const;
};
example::example()
{
data=0;
}
example::display()
{
cout<<endl<< “count=” <<count;
}
void main()
{
example d;
d.display();
}
//without errors
# include<iostream.h>
class example
{
private:
int data;
public:
example();
void displayconst();
};
example::example()
{
data=0;
}
example::display()
{
cout<<endl<< “count=” <<data;
}
void main()
{
example d;
d.display();
getch();
}
Q4) WAP that defines a class complex (defining a complex number).Write a friend function that takes two argument of class complex and returns a complex number?
Ans4)
#include<iostream.h>
#include<conio.h>
Class complex
{ int x,y;
Public:
Void getdata();
Void showdata():
};
Void complex::getdata();
{
cout<<“enter the real part of a complex number”;
Cin>>x;
Cout<<“enter the imaginary part of a cmplex number;
Cin>>y;
}
Void complex::showdata()
{
Cout<<“the real part is:”<<x<<” & the imaginary part is:”<<y;
}
Void main();
{clrscr();
Complex a;
a.getdata();
a.showdata();
getch();
}
Ans5) Static Initialization and Dynamic Initialization
C++ distinguishes between two initialization types for objects with static storage duration (global objects are an example of objects having static storage). Static initialization consists of either zero-initialization or initialization with a constant expression; any other initialization is dynamic initialization. These two types roughly correspond to compile-time initialization and runtime initialization. For example:
int x = func();
int main()
{
}
The global variable x has static storage. Therefore, it’s initialized to 0 at the static initialization phase (this is the default value of objects with static duration). The subsequent dynamic initialization phase initializes x with the value returned from the function func(). Note that func() must be invoked for that purpose – an operation that can only take place only at runtime.
Thus, global objects might be initialized twice: once by static initialization, during which their memory storage is initialized to binary zeroes, and afterwards, they are dynamically initialized by their constructors.
Q6) Distinguish between the following two statements
Time T2(T1);
Time T2=T1;
Ans6)in the above statement, T1 has been passed as an argument to T2, whereas in second statement the value of T2 has been intialised equal to the value of T1. Hence there is a big difference between the two staterments……
Time T2(T1) means that T1 is being called implicitly whereas,
Time T2=T1 means that T1 is called explicitely……
Q7) WAP to show the order of invocation of the constructor and destructor.
Ans7)
#inclde<iostream.h>
#inclde<conio.h>
Class invoke
{ int a;
Public:
Invoke(); //constructor declared
~invoke(); //destructor declared
Void display();
};
Invoke::invoke()
{
a=0; //default constructor
}
Void invoke::display()
{
cout<<endl<<“a=”<<a;
}
Invoke::~invoke()
{
}
Void main()
{
clrscr();
Invoke d;
d.diplay();
getch();
}
Ans8) The constructor and destructor function attributes provide the ability to write a function that initializes data or releases storage that is used implicitly during program execution. A function to which the constructor function attribute has been applied is called automatically before execution enters main. Similarly, a function to which the destructor attribute has been applied is called automatically after calling exit or upon completion of main.
When the constructor or destructor function is called automatically, the return value of the function is ignored, and any parameters of the function are undefined.
The constructor and destructor function attributes provide the ability to write a function that initializes data or releases storage that is used implicitly during program execution. A function to which the constructor function attribute has been applied is called automatically before execution enters main. Similarly, a function to which the destructor attribute has been applied is called automatically after calling exit or upon completion of main.
When the constructor or destructor function is called automatically, the return value of the function is ignored, and any parameters of the function are undefined.
class stack {
int stck[SIZE];
int topOfStack;
public:
stack(); // constructor
~stack(); // destructor
void push(int i);
int pop();
};
// constructor
stack::stack(){
topOfStack = 0;
cout << “Stack Initializedn”;
}
// destructor
stack::~stack(){
cout << “Stack Destroyedn”;