Solved

hiding defination of functions

Posted on 2004-10-17
3
279 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now