Solved

hiding defination of functions

Posted on 2004-10-17
3
312 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 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

Technology Partners: 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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
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.

733 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