ideas to write a good interface

hi all ,

This is a question that I doing for fun , in the book Data Structures using C++
, The question is to to implement those functions that are already implemented in the standard c library.
(a) strlen (b) strcmp (c) strcmp (d) strchr
I have already write those things . And now I looking ideas for proper and very clean interface . I use a helder file , Question5.h .

1
2
3
4
5
6
7
8
9
namespace question5
{

	int  strlen ( const char * ); // this will output the length of the string via  integer.....
	bool strcmp ( const char * , const char * ) ; // This will compare two strings. If they are same this will return 1 otherwise 0 
	char * strcat ( char * , const char * ) ; // this copies the string second parameater to the second one 
	char * strchr ( char * , int ) ; // locate the first occurence of aa string character.
		
}


and it's implementation ,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "Question5.h"


int question5::strlen( const char * input)
{
	int size = 0 ;
	while ( * input != 0 )
	{
		input++ ;
		size++ ;
	}
	
	return size ;

}


bool question5::strcmp ( const char * str1 , const char *str2  )
{
	while ( *str1 != 0 ) {
		if ( * str1 != *str2 )
			return 0 ;
		str1++ ;
		str2++ ;
	}

	return 1;
}



char * question5::strcat ( char * str1 , const char * str2)
{
	char * str1_original = str1 ;
	
	// just we are overwriting that memory.....
	while ( *str2 != 0 )
	{
		*str1 = *str2 ;
		str1++ ;
		str2++ ;
	}
	// just append that at last
	//str1++ ;
	*str1 = 0 ;


	return str1_original  ;
}


char * question5::strchr ( char *  str , int ch) 
{
	// convert the integer to the char
	char ch1 = (char) ch ;

	// then find for that...
	while ( *str != ch1 ) 
	{
		
		if ( *str == 0 )
				return (char * ) 0;
		str++ ;
	}
	return str;
	
}


and the problem is why should I have to use question5:: always ?

why that new #include syntax , I mean like
#include <iostream>
using namespace std ;
Like , not working for here .

I mean using like this .
#include <./Question5>
using namespace question5 ;

and any ideas to improve how to write a good interface ?
Last edited on
You have to use question5:: because you declared the functions in your header file to be inside the question5 namespace.

The way compiler writers implemented the new include syntax --

#include <iostream>
vs.
#include <iostream.h>

is not the way it was intended to be. It was intended that the compiler intrinsically "understands" what <iostream> is and therefore knows what header files to include. The way compiler writers implemented it was to create another file called "iostream" that is little more than a forwarding header to the header that actually contains the iostream declarations.

Bottom line is that you should not use this mechanism for your header. It is appropriate to name your header "question5.h" and it is appropriate for the user to have to #include "question5.h".

yes jsmith you are a hero.

I did a little reasearch here and , I find the two include files named
/vc98/include/iostream
/vc98/include/iostream.h

In the iostream file the things are inside a namespace std block .and in the iostream.h everything are naked.

Hmm. that is how the things are working. Now it's crystal clear.

as a help for a one who will dig this thread in future , I save this note ......


when we are using like this ,
#include <iostream>
and
#include <iostream.h>

Those iostream and iostream.h are both two files. Two independent files.
you can find them in the include directory in your compiler ( for visual studio 6.0 it is \vc98\include\ ). Just go and see those two files , just drag and drop the iostream ( the file without extension ) to notepad and you will clear this .

And you can see that there is not a
namespace std {
....
}
cover in the old iostream.h helder file. Open both them using the notepad and compare them.

And note that the above is compiler implementation dependent, although all compilers I'm aware of implement it this way.
Topic archived. No new replies allowed.