Solved

hiding defination of functions

Posted on 2004-10-17
3
299 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:fatimao
3 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 50 total points
ID: 12333126
You might have three files:

main.cpp
--------8<--------
#include <iostream>
#include "myfuncs.h"

int main()
{
        std::cout << "Hello, ";
        say_name();
}
--------8<--------

myfuncs.h
--------8<--------
void say_name();
--------8<--------

myfuncs.cpp
--------8<--------
#include <iostream>
#include "myfuncs.h"

void say_name()
{
        std::cout << "world\n";
}
--------8<--------

The myfuncs.h is a header file, used for definitions. Here you define a void function, which takes no parameters. The code itself is implemented in myfuncs.cpp.

You compile main.cpp and myfuncs.cpp to make object files main.obj and myfuncs.obj. These are linked with system libraries by the linker and you are in business.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12333378
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.
0
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12334380
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!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question