Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

hiding defination of functions

Posted on 2004-10-17
3
Medium Priority
?
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

715 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