Disclaimer: This essay is provided as an example of work produced by students studying towards a information technology degree, it is not illustrative of the work produced by our in-house experts. Click here for sample essays written by our professional writers.

Any opinions, findings, conclusions or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of UKEssays.com.

Analysing C Plus Plus Static Functions Information Technology Essay

Paper Type: Free Essay Subject: Information Technology
Wordcount: 1691 words Published: 1st Jan 2015

Reference this

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

class abc

{

private:

static int count;

public:

abc()

{

count=10;

}

void display() const

{

cout<}

};

void main()

{

abc a1,a2,a3;

a1.display();

a2.display();

a3.display();

}

//program without errors

# include

#include

class abc

{

private:

static int count;

public:

abc();

{

count=0;

}

void displayconst();

{

cout<}

};

void main()

{

abc a1,a2,a3;

a1.display();

a2.display();

a3.display();

getch();

}

b) # include

class example

{

private:

int data;

public:

example();

void display()const;

};

example::example()

{

data=0;

}

example::display()

{

cout<Cin>>x;

Cout<<"enter the imaginary part of a cmplex number;

Cin>>y;

}

Void complex::showdata()

{

Cout<<"the real part is:"<}

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

#inclde

Class invoke

{ int a;

Public:

Invoke(); //constructor declared

~invoke(); //destructor declared

Void display();

};

Invoke::invoke()

{

a=0; //default constructor

}

Void invoke::display()

{

cout<}

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";

}

 

Cite This Work

To export a reference to this article please select a referencing stye below:

Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.

Related Services

View all

DMCA / Removal Request

If you are the original writer of this essay and no longer wish to have your work published on UKEssays.com then please:

Related Services

Our academic writing and marking services can help you!

Prices from

£124

Approximate costs for:

  • Undergraduate 2:2
  • 1000 words
  • 7 day delivery

Order an Essay

Related Lectures

Study for free with our range of university lecture notes!

Academic Knowledge Logo

Freelance Writing Jobs

Looking for a flexible role?
Do you have a 2:1 degree or higher?

Apply Today!