These are some tips to improve your code:
Descriptive Names: First of all, try to name your variable, class and method names descriptively. For instance,
ntb
isn't a very good name. Instead try to name the class some thing like "NumberToBinaryConverter".
Document your code: You should document your code to show people what your methods, functions do and why they exist. For example,
void num_to_bin() {
std::cin>>convte;
recur(convte);
go_back();
return ;
}
The comment above the method is the documentation associated with the method. There are these tools like
doxygen that generate HTML files containing your code documentation.
Try to Follow a Naming Convention: I can see inconsistent naming style in your code. Use a style to name your variables. For example, I use this style,
1)
PascalCase
for class and function names.
2)
camelCase
for variable names.
As my style is more suitable for C#, I encourage you to follow the
Google C++ style. You follow any style but be consistent with it. But when you work on old projects, follow the convention used there.
Use Spaces Wherever Possible: Use of enough space is not bad and can improve code readability. For example,
std::cout << "Hello, world" << std::endl;
is better than
std::cout << "Hello, world" << std::endl;
That being said, overwhelming spaces aren't that good. For example,
window . DoSomeThing ( 10 , 12 , 30 , 20 ) ;
can hurt your eyes.
Brace Placement Style: As in the case of naming convention, you must be consistent with style of brace placement. Your first file uses Java style while your second file uses the ANSI style. You should have used any one of these styles.
I can also see the weird semicolon after the closing braces. While the C++ compiler allows the semicolon, that is not necessary.
Avoid Global Variables: Never use global variables. I see no reason for why you declared the global variable
Binary_Convert
. Instead, why not use a local variable,
int main()
{
ntb n;
n.num_to_bin();
}
Now, the object
n
will get destroyed when it reaches the end of the scope. But with global variables the object will remain in memory until your program exits. See this
page.
Declare a Constructor and a Destructor: Your class
ntb
should have a constructor that initializes the fields during construction. Something like this would be fine,
ntb() {
this->convte = 0;
}
A destructor should be declared if your class has pointer members to delete the data pointed by them. I don't know how familiar you are with pointers. If you're unfamiliar with pointers and references, you should learn them; they're so important to understand programming.