Results 1 to 5 of 5

Thread: Explain how to define function outside the class in C++?

  1. #1
    Join Date
    Dec 2010
    Posts
    11

    Explain how to define function outside the class in C++?

    I am having a problem in one of the C++ terms. I don’t understand the term scope resolution and how one can define the function outside the class. I don’t have any idea about this term as when taught in my classes I was absent which is causing problem for me to understand the rest terms as they are interlinked. So, anybody having any knowledge than please let me know without fail.

  2. #2
    Join Date
    May 2009
    Posts
    539

    Re:Explain how to define function outside the class in C++?

    This is another way of defining the function. In this case the function declaration and function definition are written separately. So the class name and Scope resolution operator is used to tell the compiler the scope of the member functions. Scope resolution operator is represented by the :: ,which indicate the scope of the function.
    The general syntax is
    return type class name :: function_name(argument list)
    {
    //function body
    }

  3. #3
    Join Date
    Apr 2009
    Posts
    569

    Re: Explain how to define function outside the class in C++?

    The type of member function argument must exactly match with the type declared in the class definition. Scope resolution operator (: is used along with the class name, It identifies the function as a member of particular class. Without scope resolution operator the function will be ordinary function. You must not forget that the argument should match to the class which is been declared. So, these are the point which one should notice while defining the function outside the class.

  4. #4
    Join Date
    May 2009
    Posts
    543

    Re: Explain how to define function outside the class in C++?

    Code:
    For example
    class Box
    {
     private :   int l,h,w;
    
    public :
    void get_data();
    void display();
    };
    void  Box ::display()
    {
    cout<<”the length of box is<<l;
    cout<<”the height of box is<<h;
    cout<<”the width of box is<<w; 
    }
    void Box ::get_data()
    {
    cout<<”enter the length width and height of box”;
    cin>>l>>h>>w;
    }
    In this example we have calculated the length, height and width of the box. Important thing to notice is that we have defined the function outside class in C++. This is the perfect example of Scope resolution.

  5. #5
    Join Date
    May 2009
    Posts
    529

    Re: Explain how to define function outside the class in C++?

    Many of them get confused when said that define the function outside the class. Well, you must know that defining the function outside the class and scope resolution is same. I have seen that many of them getting confused between this terms. I don’t think that there is any reason to get confused in it but still one should be aware of it.

Similar Threads

  1. Define Attributes of a java class
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 29-03-2010, 08:58 AM
  2. How can I return different class in one function?
    By Madaleno in forum Software Development
    Replies: 4
    Last Post: 10-02-2010, 09:45 PM
  3. Explain the System.Exception class of C#
    By Jesus2 in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 03:51 AM
  4. Explain #define preprocessor directive
    By Javiier in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 10:12 PM
  5. explain class in java
    By GlassFish in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 09:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,629,887.29297 seconds with 16 queries