Sign in to follow this  
Abhinash

Make header files in c++ when you come up with simple ideas!!

Recommended Posts

Abhinash    102
Hey guys!! I will frankly say this-- I have just been into c++ programming for 5 days!!! Yes thats true FIVE DAYS. So I ain't no c++ whiz. If you are a rookie in c++ this will boost your will if nothing more cause each simple "TOOL" (wel thats what I named the functions in header files I created) you can program can be used as a function in another tool. The most fun part is, if you created a function in a separate source file, you would have to make a function prototype in all the source files that used the function. but since header file actually is treated as a part of the source code putting the function in the header file is just like putting the function on the individual source files. Finally, to make it simpler, what I meant is on all the source files that you include the header file, you will be able to use the function Rev() to return the reverse of the string(I have built the Rev() function below so I used the name I will get to BUILDING the funciont later) This thing I did really helped me a lot. Whenever I come up with small tools like making the reverse of a string I put it as a function in a header file that I made by myself. Now try to get this header file.. ********************************************************************************** fnc.h ********************************************************************************** #include<stdlib> #include<string> using namespace std; //FUNCTION PROTOTYPES string Rvr(string word); //ACTUAL FUNCTIONS string Rvr(string word){ int i,j; string rev; /*Try to get the trick here the string "Boom" has a length of 4 but in the string "B" is the Zeroth string (that's how it works in c++ don't blame me!!) so to get to "B" you need "Boom"[0]*/ j = word.length() - 1; /*now I will use a "for" loop to generate nos. from 0 to 3 (and three is 4 -1, which is "Boom".length() - 1.) See??? that was the trick..*/ for (i=j;i>=0;i--) { rev = rev + word[i]; } return rev; } /* And this is it..... if you save this as a header file, include this file in another source file, you will be able to use the function Rvr(<string>) to return the reverse string of the given string. I have similarly created functions like Len()(for length .. its SHORTER!! and took the idea from BASIC program.. I was a VISUAL BASIC PROGRAMER you see..;)) And again guys I am a beginner but just wanted to share what I thought made MY life easier. To the rookies again, there are still things I need to talk about files that I included as #include<iostream> but those things are sth that I have just understood... so might tale some time to explain thoroughly.. but trust me sumday I will explain it clearly...*/

Share this post


Link to post
Share on other sites
Aardvajk    13205
It is unwise to put "using namespace std;" in a header file since you then force any other files that include your header file to have the contents of std dumped into their namespace.

Really, you would be better to split the above into a .h and a .cpp file, with the function prototype in the .h and the actual implementation into the .cpp file. You could then put "using namespace std;" at the top of the .cpp file without having to worry about affecting other units that use your code.

I appreciate that you are only just beginning, but since you seem to be here offering advice, you need to ensure that you are not suggesting bad practices to people who may have less experience than you.

Share this post


Link to post
Share on other sites
mikeman    2942
Quote:

The most fun part is, if you created a function in a separate source file, you would have to make a function prototype in all the source files that used the function.


Not really. The proper way is to declare the function prototype in MyUnit.h, and define the function in MyUnit.cpp. To use a custom function, for example 'void Foo()', in another source file, you just include MyUnit.h and link to MyUnit.o(if you add MyUnit.cpp to your project, that happens automatically). Your way is mostly incorrect. Just try to include your .h file that contain function definitions in more than 1 source file in the same project. You will get a multiple definition error from the linker, since the function Foo() is defined in all object files that are linked together.

I appreciate your enthusiasm, but it would be better if you wait until you have sorted things out yourself before deciding to tutor beginners. I'm sure you don't want anyone to learn bad practices from you. It's bad karma :)

Share this post


Link to post
Share on other sites
daerid    354
Just to adopt the helpful viewpoint here, try this refactoring:


#ifndef REVERSESTR_H
#define REVERSESTR_H

#include <string>

inline std::string RevStr(const std::string& in)
{
std::string reversed;
std::string::size_t len = in.length();
for(int i = 0; i < len; ++i)
{
reversed += in[i];
}
return reversed;
}

#endif//REVERSESTR_H





However, I'd actually just use the following line to achieve what you're trying to do (note: haven't tested this so it might not be 100% accurate):


std::string in = "Hello, World!";
std::string rev(in.rbegin(),in.rend());



You could do one step better and look into Boost.StringAlgorithms

Share this post


Link to post
Share on other sites
Abhinash    102
Thank you guys!!!
See I wasn't a looser after all. An I really hope that beginners who go through my
entire comments(filed by you guys) so that even they learn the rigth way.
Again guys than you vey much!! I really appreciate it.

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by Abhinash
See I wasn't a looser after all.


Oh, you definitely got the right idea: as you start solving common problems, gathering your solutions into a library rather than cutting and pasting is definitely the way to go. However, you need to make sure that there isn't a better way to do it (e.g. using the standard C++ library) and that your code is of good quality. This is the kind of things that come with experience.

Share this post


Link to post
Share on other sites
Deyja    920
Aww, aren't new programmers cute?

Also, don't abreviate names. Never sacrifice readability to save keystrokes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Zahlman
Quote:
Original post by daerid
Just to adopt the helpful viewpoint here, try this refactoring:

...

You could do one step better and look into Boost.StringAlgorithms


Either way, you're making it much too complicated [smile]


Not if you need a reversed copy of the string. I was going to post reverse(), but then I realized that his function was returning a new string.

Share this post


Link to post
Share on other sites
Zahlman    1682
Oh, well that's easy too:


std::string reversed_string(const std::string& input) {
return std::string(input.rbegin(), input.rend());
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this