Link to home
Start Free TrialLog in
Avatar of fatimao
fatimao

asked on

hiding defination of functions

hi,

i have started programing in c++ ( visual studios and borlandc). i want to ask that can i hide function definations. i mean is there any way to hide user defined functions, so that we can use them by only calling their name.


regards
fa
ASKER CERTIFIED SOLUTION
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Essentially rstaveley has given the answer here.  Windows DLLs are in essence pre-compiled source files that can be linked with just like the linker links with the object files at run time.  DLLs allow you to safely distribute your code for others to use without fear of having the source ripped.
Avatar of pankajtiwary
pankajtiwary

Hi fatimao,

Yes, this is the power of modular programming. The is actually done in the industry to make flexible and extensible software. Check the example out.

// file: myhdr.h

#ifndef _MY_HDR_H
#define _MY_HDR_h

#include "myhdr.h"

double sum_of_squares( int n ) ;

#endif

// file: myhdr.cc

#include "myhdr.h"

double sum_of_squares ( int n ) {
    double sum = 0 ;
    for ( int i = 0 ; i < n ; i++ )
        sum = sum + ( i * i ) ;
    return sum ;
}

// file: main.cc

#include <iostream>

using namespace std ;

#include "myhdr.h"

int main ( ) {
    int n ;
    cout << "Enter n" << endl ;
    cin >> n ;
    cout << "Sum of squares is " << sum_of_sqaures(n) << endl ;
    return 0 ;
}

Lets say, you have writte the files myhdr.h and myhdr.cc. You will compile the myhdr.cc file and handover the object file to the user. The user is writing the main.cc who will use the call to your function sum_of_squares(int). He will include your header file ( to know the usage of the function ), write his main, compile it and just link it with your object file myhdr.o.
Now assume, after one month you came to know that you actually don't need to loop till that number but you can directly use one formulae to determine the sum of squares. So, you change your myhdr.cc to:

// file: myhdr.cc

#include "myhdr.h"

double sum_of_squares ( int n ) {
    double sum = 0 ;
    sum =  ( n * (n +1) * ( ( 2 * n ) +1) ) / 6 ;
    return sum ;
}

You just need to give the new object file myhdr.o to the user and he just has to link your application man.cc with your new myhdr.o

The good thing about this approach is people know only that much which is important to the. Here the user of your library does not really need to know how you have implemented the sum_of_squares() and his program will run perfectly fine.


Cheers!