Learning C++

Page 1 of 2 Next >>

Forum Index -> General

United States Rostered Member for Proxiteam September 20 2010 20:56Posts: 619
So I currently go to a four-year university and I am majoring in computer science.

Just this quarter they decided to ditch Java and all of a sudden teach every computer science course in C++. As I was telling Dwain, my luck with professors was not so good this year so now I am stuck kind of teaching the BASICS of C++ by myself. The professor is teaching us how to translate Java into C++ and other C++ stuff but the basics about it are still pretty unknown to me.

I was talking to Dwain about it and I really had trouble even getting a program started. What program to use? Is .cpp your main coding file?

My professor uses NetBeans to code C++. I installed it. Then you also have to install the compiler. The process was just too much.

So I installed Visual Studios. I open up to make a new project, I get about 9 different options under the Visual C++ tab. All I'm saying to myself is "Which one do I choose to make a basic console program?..."

These are the basic questions I have. Apparently C++ programs have header files. Wtf are those for? They have source files as well; enlighten me please lol.

Yeah I never programmed before college. It was just something I tried out as being undeclared and loving computers that I fell in love with. My professor is just not the brightest as an instructor.

Also, I wrote a piece of code for our first program due tonight actually(assigned today, go figure). It's really simple though. I did it in 5 minutes in Java. In C++ however, my goodness have I been strugglin!

The assignment is simple: using a for loop display all the odd numbers from 1-45.

I got the code written out. It compiles(or builds, not sure which term to use). However, now when I run it, console windows comes up, then it just completely exits, not even letting me see the results. So I can't exactly see if I translated the code correctly. Help please?
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia Rostered Member for Proxiteam September 20 2010 21:17Posts: 1000
Bayou and Ratfink are almost certain to be more equipped to answer you Bobo. But I can contribute a little bit :)

The difference between the header file (.h) and the .cpp files is the header is used to declare the intent of the class while the .cpp is used to implement it. Does this make sense?

i.e. any public methods you want exposed on the class would have their signatures on the .h file but the signature aswell as the actual method code in the .cpp file.
aka swAMi. Melbourne! ADMIN
United States Rostered Member for Proxiteam September 20 2010 21:22Posts: 619
Oh I see. So a header file is like what a interface would be in Java? Assuming you know Java >.>.

Sorry its all I know! Lol.
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia September 20 2010 21:25Posts: 8
Hi Bobo,

Try running your code without debugging (Ctrl-F5). In Visual Studio, running console apps with debugging enabled will close the window immediately after finishing. Running it without debugging, however, will leave the window open and prompt you to close it.

If your program still isn't working, post your code and I'll try to nudge you in the right direction.

- bayou
Let's settle this like men. You. Me. Steppes Of War. Now.
United States Rostered Member for Proxiteam September 20 2010 21:30Posts: 619
Tried it out. Still isn't working. Here's the code.
/* 
* File: main.cpp
* Author: Angel Macias
*
* Created on September 18, 2010, 8:05 PM
*/

#include <cstdlib>
#include <iostream>

using namespace std;

/*
* Using a for loop, print out all the odd numbers from 1-45.
*/
int main(int argc, char** argv) {

int value = 0;

for(int count=0;count<25;count++)
{
cout << ++value <<endl;
value++;
}

return 0;
}

http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia September 20 2010 21:38Posts: 8
That program runs fine for me (except for a slight logic error). I'm guessing your problem is environmental. Is the window still disappearing? Also, which version of Visual Studio are you using, and which project type did you create?
Let's settle this like men. You. Me. Steppes Of War. Now.
Australia Rostered Member for Proxiteam September 20 2010 21:40Posts: 1000
Try opening an instance of command prompt and directly executing the executable in the bin directory.
That way when your application ends, by virtue of the fact that it is being executed by the external command prompt, you should still see the output as it won't close.
aka swAMi. Melbourne! ADMIN
United States Rostered Member for Proxiteam September 20 2010 21:45Posts: 619
@bayou: Yes the window disappears. I'm using Visual Studio 2010. I used an empty project type.
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia September 20 2010 21:52Posts: 8
Ok, I think there's a bug in VS2010 that causes this. The workaround requires a little bit of dicking around, but is not altogether difficult. Are you familiar with XML?
Let's settle this like men. You. Me. Steppes Of War. Now.
United States Rostered Member for Proxiteam September 20 2010 21:56Posts: 619
Nope. :(. All I've used before is jEdit and that was only for Java.

@Dwain: Can't seem to find the .bin file anywhere in the folder I made from Visual Studios. Not only that, I don't know the command used to run the program.

Sorry guys, like I said, my professor didn't go over the basics.

EDIT: Nvm Dwain, I got it :).
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia September 20 2010 22:01Posts: 8
Actually, scratch that. You should be able to do it through the UI.

1. Right-click your project (2nd level down) in the Solution Explorer window, choose Properties
2. In the tree on the left-hand side, navigate to Configuration Properties > Linker > System
3. In the SubSystem option on the right, choose "Console"
4. Click OK

Hopefully that will sort you out.
Let's settle this like men. You. Me. Steppes Of War. Now.
Australia September 20 2010 22:04Posts: 99

Bobotastic wrote:

Is .cpp your main coding file?

Bobotastic wrote:

These are the basic questions I have. Apparently C++ programs have header files. Wtf are those for? They have source files as well; enlighten me please lol.

The majority of your code (ie methods and function implementations) go in cpp files. The header files is where you function and class definitions go. Why? Because the C++ compiler needs to see the definitions before it can deal with the references to those definitions in the implementations. So you separate the definitions into .h files and #include them at the top of the .cpp files. The .cpp files are ones that get compiled, and the #include statements do a virtual copy/paste of the .h files in at the top of the .cpp files.

Bobotastic wrote:

So I installed Visual Studios. I open up to make a new project, I get about 9 different options under the Visual C++ tab. All I'm saying to myself is "Which one do I choose to make a basic console program?..."

In VS2010, File -> New Project -> Visual C++ -> Win32 -> Win32 Console Application

Bobotastic wrote:

However, now when I run it, console windows comes up, then it just completely exits, not even letting me see the results. So I can't exactly see if I translated the code correctly. Help please?

The console window closes because the program has ended as you haven't written any code to stop it ending immediately after looping. You can either do something like read a key from the console, or simply run your program from the command line to ensure that the output is still visible once the program has ended. There may be a Visual Studio option somewhere to help you hold the window open as well.
Charity, if you have the means, is a personal choice, but charity which is expected or compelled is simply a polite word for slavery. ADMIN
Australia September 20 2010 22:08Posts: 99

Bobotastic wrote:

Oh I see. So a header file is like what a interface would be in Java? Assuming you know Java >.>.

No. See my previous post.
There is no such thing as an "interface" in C++. However, because C++ supports multiple inheritance, you can use a class that contains only abstract methods like you would an interface in Java (this known as a pure virtual class).

LuckySword wrote:

i.e. any public methods you want exposed on the class would have their signatures on the .h file but the signature aswell as the actual method code in the .cpp file.

Not just public methods. It's the whole class definition. This includes fields and methods of any accessibility level (public, private, protected).
Charity, if you have the means, is a personal choice, but charity which is expected or compelled is simply a polite word for slavery. ADMIN
Australia Rostered Member for Proxiteam September 20 2010 22:11Posts: 1000

Ratfink wrote:

This includes fields and methods of any accessibility level (public, private, protected).

I suspected I may have been wrong on that point. Thanks for the correction.
aka swAMi. Melbourne! ADMIN
United States Rostered Member for Proxiteam September 20 2010 22:26Posts: 619

bayou wrote:

Actually, scratch that. You should be able to do it through the UI.

1. Right-click your project (2nd level down) in the Solution Explorer window, choose Properties
2. In the tree on the left-hand side, navigate to Configuration Properties > Linker > System
3. In the SubSystem option on the right, choose "Console"
4. Click OK

Hopefully that will sort you out.

Worked great. Thank you. If anyone ever reads this for help, make sure you run it without debugging :).

Ratfink wrote:

The majority of your code (ie methods and function implementations) go in cpp files. The header files is where you function and class definitions go. Why? Because the C++ compiler needs to see the definitions before it can deal with the references to those definitions in the implementations. So you separate the definitions into .h files and #include them at the top of the .cpp files. The .cpp files are ones that get compiled, and the #include statements do a virtual copy/paste of the .h files in at the top of the .cpp files.

So would it be correct programming etiquette to write all your functions in the .h file?
Just the definitions, not the actual body...
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia Rostered Member for Proxiteam September 20 2010 22:36Posts: 1000

Bobotastic wrote:

So would it be correct programming etiquette to write all your functions in the .h file?
Just the definitions, not the actual body...

.h file:
void somefunc();


.cpp file:
void somefunc()
{
...
}

though i'll probably get corrected on that too lol it's been awhile for me i'm afraid
aka swAMi. Melbourne! ADMIN
United States September 20 2010 22:39Posts: 202
I just figured I'd point out that I'm getting considerable entertainment out of reading up on this ostensibly intelligent group of people having trouble with anything over a C+. Seriously, people these days...

;p
maarzipan. Melts in your mouth and in your hand. NEWSWRITER
Australia September 20 2010 22:55Posts: 99

Bobotastic wrote:

So would it be correct programming etiquette to write all your functions in the .h file?
Just the definitions, not the actual body...

In general, that's correct.

maareek wrote:

I just figured I'd point out that I'm getting considerable entertainment out of reading up on this ostensibly intelligent group of people having trouble with anything over a C+. Seriously, people these days...

I LOLed.
Charity, if you have the means, is a personal choice, but charity which is expected or compelled is simply a polite word for slavery. ADMIN
Australia September 21 2010 00:15Posts: 8
I'm probably just confusing the situation, but what the hell. Note you don't have to forward-declare functions in a header file, or in fact at all. Like Ratfink said, the compiler reads through the file from top to bottom, and you can't have a call to a function it doesn't know about yet. You can define/declare the function in one go so long as it appears before your call to it. For example:
#include <iostream>

using namespace std;

int add(int a, int b)
{
return a + b;
}

int main(int argc, void **argv)
{
int sum = add(1, 3);
cout << sum << endl;

return 0;
}
is totally fine.

If you felt passionately about having the add function under the main function, you could do this:
#include <iostream>

using namespace std;

// forward-declare the add function in the same file
int add(int, int);

int main(int argc, void **argv)
{
int sum = add(1, 3);
cout << sum << endl;

return 0;
}

int add(int a, int b)
{
return a + b;
}
The purpose of using header files has more to do with sharing libraries with other programmers. You would distribute your compiled code and also your header files. The programmer using your library needs the header files to know function names, parameter lists, etc. Unlike Java, this information cannot be divined from a compiled library (or package, or whatever). The programmer would include your header files at the top of her code, "link" against your compiled library, and joy would ensue shortly thereafter. That way, you don't distribute your super-secret source code - just the bits other people need to use it.
Let's settle this like men. You. Me. Steppes Of War. Now.
Canada September 21 2010 16:50Posts: 132

maareek wrote:

I just figured I'd point out that I'm getting considerable entertainment out of reading up on this ostensibly intelligent group of people having trouble with anything over a C+. Seriously, people these days...

;p

Haha, I was thinking that myself, but more because of the fact that I use Visual Studio pretty much everyday at work to compile stuff, but never take the time to look at all the code behind it.. since I don't understand a thing lol.
I'm strong but I'm wild, they say I drink too much/ The only problem that I have is that I think too much.

Page 1 of 2 Next >>

Reply You must be logged in to comment. (Sign In)