Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Query with relation to member function and object in Class

This is a discussion on Query with relation to member function and object in Class within the C and C++ forums, part of the Software Development category; Is it possible to create member function in a class without using or without creating an object? If so in what scenarios can one achieve this also tell me if ...

Go Back   Geeks Talk > Software Development > C and C++
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 07-12-2006
Contributing Member
 
Join Date: May 2006
Posts: 79
Thanks: 0
Thanked 6 Times in 5 Posts
fred is on a distinguished road
Query with relation to member function and object in Class

Is it possible to create member function in a class without using or without creating an object? If so in what scenarios can one achieve this also tell me if this concept is possible is this used in real time applications.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-05-2006
Junior Member
 
Join Date: Dec 2006
Location: Hyderabad
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
harshamailid is on a distinguished road
Re: Query with relation to member function and object in Class

Hi Everyone,

This is Sriharsha congratulations for the three winners

---Harsha
Reply With Quote
  #3 (permalink)  
Old 12-06-2006
Expert Member
 
Join Date: Sep 2006
Location: Bengalooru (Formerly called Bangalore), India
Posts: 486
Thanks: 2
Thanked 29 Times in 28 Posts
kalayama is on a distinguished road
Re: Query with relation to member function and object in Class

I fail to understand how Harsha'a post came here... I could see he's new here... Kindly can eny of the moderators delete/move that post which is irrelevant for the discussion?
Reply With Quote
  #4 (permalink)  
Old 12-06-2006
Expert Member
 
Join Date: Sep 2006
Location: Bengalooru (Formerly called Bangalore), India
Posts: 486
Thanks: 2
Thanked 29 Times in 28 Posts
kalayama is on a distinguished road
Re: Query with relation to member function and object in Class

Yes. You can use static funstions for this purpose. Static functions should be used without having an instance of the class. Here's an example.
Quote:
#include <stream.h>

static const int MAX_STACK = 100;

class Stack
{
private:
static int stack[MAX_STACK];
static int top;

public:
static void push (int item) { stack[top++] = item; }
static int pop () { return stack[--top]; }
static int is_empty () { return top == 0; }
static int is_full () { return top >= MAX_STACK; }
};

main ()
{
for (srandom (time (0L)); !Stack::is_full (); Stack:: push (random ()))
;

while (!Stack::is_empty ())
cout << Stack:: pop() << "\n";
}
Cheers,
Kalayama
Reply With Quote
  #5 (permalink)  
Old 12-19-2006
Contributing Member
 
Join Date: May 2006
Posts: 79
Thanks: 0
Thanked 6 Times in 5 Posts
fred is on a distinguished road
Re: Query with relation to member function and object in Class

Kalayama thanks for you reply. I understood that static function is used for the query I asked.But could you tell me why we need such concept or in other words in what scenarios would we use these.
Reply With Quote
  #6 (permalink)  
Old 12-20-2006
Expert Member
 
Join Date: Sep 2006
Location: Bengalooru (Formerly called Bangalore), India
Posts: 486
Thanks: 2
Thanked 29 Times in 28 Posts
kalayama is on a distinguished road
Re: Query with relation to member function and object in Class

Well, I personally have used this feature to handle exception in multi-threaded programs.

There are a number of other possible usages for static functions. (I remember answering the same question in Geek interview, may be in Q&A or FAQ section. Search for it, might have written more there)

PS: Too little time mate, will post a detailed reply soon
__________________
[COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -4. The time now is 02:29 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved