Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



What is the difference between Local class and global class in C++?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 03-01-2011
Member
 
Join Date: Dec 2010
Posts: 16
What is the difference between Local class and global class in C++?
  

Hey Guys, well I am confused between the terms of object oriented programming. If I am not wrong then C++ is also an object oriented programming language. I am having problem in Local class and Global class. Can anybody let me know what are the differences between Local class and global class in C++? So, if anyone having any clue or information about this query then please let me know.

Reply With Quote
  #2  
Old 03-01-2011
Member
 
Join Date: May 2009
Posts: 615
Re: What is the difference between Local class and global class in C++?

Well, taking about Local class then a class can be declared or defined inside a function or a block .such classes are known as Local class. When a class is declared within a function then it is known only to that function and unknown outside of it. Local classes have following restrictions.
1) All member function of local class should be defined within the class declaration.
2) The function in which the class is defined cannot access private member of the class directly.
3) The local class cannot use local variables of the functions in which it is declared.
Reply With Quote
  #3  
Old 03-01-2011
Member
 
Join Date: May 2009
Posts: 523
Re: What is the difference between Local class and global class in C++?

Code:
.>     For example
#include<iostream.h>  
main()
{
    class  sample
       {
           int n;
        public:
           void get_num();
               {
                   cout<<”give no”;
                    cin>>n;
                }
           void display()
               { 
                 cout<<”the no. is “<<n;
                }
}
sample s;
s.get_num();
s.display();
}
This is an example of local class where in you will find that how class is defined or declared inside the function. You can notice that if a local class is declared to the function then it will only be available to that function only, it won’t available outside the function. This is the specialty of local class where in it is accessible only to the function not outside the function.
Reply With Quote
  #4  
Old 03-01-2011
Member
 
Join Date: May 2009
Posts: 532
Re: What is the difference between Local class and global class in C++?

When a class is defined outside of any function then such classes are known as Global class. All the functions in the program can access global classes. It is completely opposite to local class. In local class it is accessible only to the function where as in global class it is accessible outside the function in the program. One should note that the in global class it is declared or defined outside the class not inside the class, if it is defined inside the class then it will be assumed as neither local class nor global class.
Reply With Quote
  #5  
Old 03-01-2011
Member
 
Join Date: May 2009
Posts: 501
Re: What is the difference between Local class and global class in C++?

Code:
For example

class Box
{
 private :   int l,h,w;

public :

void get_data()
{
cout<<”enter the length width and height of box”;
cin>>l>>h>>w;
}

void display()
{
cout<<”the length of box is<<l;
cout<<”the height of box is<<h;
cout<<”the width of box is<<w; 
}
};

main ()
{
Box b1,b2;
b1.get_data();
b2.get_data();
b1.display();
b2.display();
}
This is an example of Global class. Over here you will find that global class is defined outside the function where as previously in local class it was defined in the function itself. This is only the major difference between the local and global class. I think you might be now cleared with the concept of global class and local class.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is the difference between Local class and global class in C++?"
Thread Thread Starter Forum Replies Last Post
Upgrade Sony Ericsson Xperia Play Micro SD card from class 2 8GB to class 4 32GB Aaryan2011 Portable Devices 8 08-10-2011 11:06 PM
Debug class and Trace class Difference Amaresh Software Development 5 22-01-2010 09:00 AM
Difference among concrete class and abstract class Roxy_jacob Software Development 4 07-12-2009 12:22 PM
What is the Difference between Class and Object? RupaliP Software Development 5 28-02-2009 06:03 PM
Good news for CBSE CLASS X & CLASS IX - visit learnnext surya_expert Education Career and Job Discussions 0 13-12-2008 11:09 AM


All times are GMT +5.5. The time now is 05:10 PM.