Adjusting to C++ from JAVA

Bananapie

New Member
Recently transferred to a new University, and my Computer Science courses I am taking this semester are UNIX Systems and Data Structures and Algorithm Analysis. Both code in C++.

My previous college put me through a year of JAVA programming, to get the prerequisites for this semester's courses. Absolutely horrible idea on their part. I got an introductory test today requesting me to code in C/C++ style. I had no idea what it was asking... luckily she said that it is not being graded, but being used to know our skills.

Just looking for some good and basic sites in C++ that could get me in the flow with it. Now that I read a tutorial site, it seems that the if/loops and functions kind of run the same as JAVA. About all that I have got to so far.

This semester just got a whole lot worse. Hopefully within the next month though, I am relatively fluent on what I am doing in C++ as opposed to working in JAVA. Probably going to have to find some Professors who are available to adjust.

What are some good tutorial/guide sites or reference sites that one could use to get a good description on basic/intermediate commands?

Also, what is a good C++ compiler for free, or relatively cheap that one could use?


Just another fast question.

cout<<"(insert here)" kind of works like JAVA's system.out.println("insert here") right?

and cin>> works like the Scanner system in java?
 
Last edited:

Cromewell

Administrator
Staff member
http://www.bloodshed.net/devcpp.html is a decent free windows compiler. It hasn't been updated in a long time though.

Unix has a built in compiler.

For command references I usually use cplusplus.com
cout<<"(insert here)" kind of works like JAVA's system.out.println("insert here") right?

and cin>> works like the Scanner system in java?
More or less, the use is effectively the same. You have to be careful using cin though, if you try and read the wrong datatype it doesn't do nice things.
 

Bananapie

New Member
http://www.bloodshed.net/devcpp.html is a decent free windows compiler. It hasn't been updated in a long time though.

Unix has a built in compiler.

For command references I usually use cplusplus.com

More or less, the use is effectively the same. You have to be careful using cin though, if you try and read the wrong datatype it doesn't do nice things.


Is the Unix one called Gypsy? I am getting a virtual drive of Unix and I believe that is what I will be using for one of my classes.

So you recommend bloodshed otherwise, for a Windows Compiler?

As well, what alternate is there to cin? Or should I use it, and be sure that the data type is correct, or try casting over to the correct one...etc. Either way, it wouldn't enjoy the entry just like JAVA and kick you out, where you enter s for an integer variable, I would assume?


Also, I am at the cprogramming.com/tutorial website, and I am going down the list to see what differences there are. I am seeing several similarities in it. Just a few minor changes. Really hoping that I can adjust fairly well. haha
 
Last edited:

Cromewell

Administrator
Staff member
Is the Unix one called Gypsy? I am getting a virtual drive of Unix and I believe that is what I will be using for one of my classes.

So you recommend bloodshed otherwise, for a Windows Compiler?

As well, what alternate is there to cin? Or should I use it, and be sure that the data type is correct, or try casting over to the correct one...etc. Either way, it wouldn't enjoy the entry just like JAVA and kick you out, where you enter s for an integer variable, I would assume?


Also, I am at the cprogramming.com/tutorial website, and I am going down the list to see what differences there are. I am seeing several similarities in it. Just a few minor changes. Really hoping that I can adjust fairly well. haha

Most unix systems will have GCC.

Yes, for a windows compiler bloodshed is ok. Visual Studio is ok as well.

For cin, I usually suggest reading as a string and casting. You don't get nice exceptions when data types don't match.
 

mihir

VIP Member
On windows if you want to use GCC you can try this - http://www.mingw.org/
I have used it once, when my Linux OS got corrupted.

And Dev C++ as mentioned by Cromwell is also pretty good.

For string inputs your can use gets and getline.
Make sure you include appropriate header files
 

Bananapie

New Member
On windows if you want to use GCC you can try this - http://www.mingw.org/
I have used it once, when my Linux OS got corrupted.

And Dev C++ as mentioned by Cromwell is also pretty good.

For string inputs your can use gets and getline.
Make sure you include appropriate header files

Header files are essentially JAVA's version of import, right?

So including <iostream> is essentially importing java.util.* or java.util.Scanner to implement input, but C++ also has to import cout to output... right?
 

Cromewell

Administrator
Staff member
Header files are essentially JAVA's version of import, right?

So including <iostream> is essentially importing java.util.* or java.util.Scanner to implement input, but C++ also has to import cout to output... right?

Sort of. iostream handles input and output streams, not just input as is the case with Scanner.
 

Bananapie

New Member
Sort of. iostream handles input and output streams, not just input as is the case with Scanner.

Oh yeah, that's what I meant when I said that it also imports cout to output, being different from scanner. What other major header files are there, ones common to use? I need to figure out pointers now. Then hopefully I am roughly up to par in c++ to where I should be.
 

Bananapie

New Member
Study up
stdio
iomanip
fstream
math
process
string
stdlib
conio

Yeah haha I plan too. Kind of difficult atm. No internet in the apartment until tomorrow night so I do what I can between classes on campus. Ill definkitely google those, and hopefully figure out pointers. My prof said that is the major thing I missed by taking java. Hope that is the majority of it. Then won't feel as stressed lol:cool:
 

mihir

VIP Member
It won't take a lot of time, there will be only a certain set of functions from each header file which you will be using.
Like the squareroot function ie sqrt() which will be in math and then fabs (f absolute) will be in math
Then functions like clrscr() will be in conio and then string operations will be in string.
strlength and stuff.
And file operations in fstream. There are three major classes ifstream ofstream and fstream.
I am guessing this now fstream would be the base class.

And then in iomanip you can change the color of the text and functions like those.

stdio will have printf scanf(if you don't want to use cout cin) and also gets and puts.
process will have exit() the only useful function in it, but you might also find other functions which would be useful for you.
 

Bananapie

New Member
I just found out today that I do not need to be concerned about header files right now, as one of our professor's has one header file for all of the standard stuff that I will be using in the class.

She did mention however that it does not include what header files we would build ourselves.

How often is it that one would build themselves a header file? Haven't majority already been coded, or what?
 

mihir

VIP Member
I just found out today that I do not need to be concerned about header files right now, as one of our professor's has one header file for all of the standard stuff that I will be using in the class.

She did mention however that it does not include what header files we would build ourselves.

How often is it that one would build themselves a header file? Haven't majority already been coded, or what?

Even I do that,
in the include folder just make a file like this

Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
.
.
.
.
All the header files and save this file as All.h or anything else you want to name it.
And then in every program just include all.h
You will be making header files soon enough.But there is always an alternative to making a Header file, but I prefer to make a header file for programs using structures and class and alot of functions keeps the main code cleaner. :D
 

Bananapie

New Member
Even I do that,
in the include folder just make a file like this

Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
.
.
.
.
All the header files and save this file as All.h or anything else you want to name it.
And then in every program just include all.h
You will be making header files soon enough.But there is always an alternative to making a Header file, but I prefer to make a header file for programs using structures and class and alot of functions keeps the main code cleaner. :D

haha yeah, I imagine including them all in one file would make it look nicer. It is kind of handy. What kind of header files would one make? You would think there would be one out there for virtually everything, unless I am misunderstanding what they actually are. haha

I managed to get PuTTY, which I guess is what we are programming in. At least I believe. Hook up to the Universities alpha server and do it there. Not really sure as to how yet. Going to have to run in and ask the Teaching Assistant tomorrow so I can figure out how to do my program before Tuesday. We're doing a basic bubble sorting program that reads in some file, and then sorts. Supposed to be efficient. At least my JAVA bubble sorting notes should be efficient to get me through the coding at least hopefully. :good:
 

Cromewell

Administrator
Staff member
I just found out today that I do not need to be concerned about header files right now, as one of our professor's has one header file for all of the standard stuff that I will be using in the class.

She did mention however that it does not include what header files we would build ourselves.

How often is it that one would build themselves a header file? Haven't majority already been coded, or what?

In your class you will probably build quite a few headers. A header isn't anything special, it's mainly a place to prototype the functions/classes you will be using.

It is easy to include your own though, use "" instead of <> to include them from the local directory.
 

Bananapie

New Member
In your class you will probably build quite a few headers. A header isn't anything special, it's mainly a place to prototype the functions/classes you will be using.

It is easy to include your own though, use "" instead of <> to include them from the local directory.

Yeah, I had to use "/ to get one of the prof's header files. I went into the directory to see what it included exactly, and there were like 20-30 other headers in there. So we just have to include his in our program for pretty much every header imaginable, besides what we would be making, just for the sake of ease.

Started my first assignment. The one that requires you to program an efficient bubble sort. So far, think I am doing pretty alright. I can't see any errors like NetBeans would have showed you, but I keep looking over what I write, and it makes sense.

Just a fast question on code. I am trying to get input from keyboard(Actually, we are supposed to treat it like it was input from keyboard, but we send input from a file later using cin(can't use fstream). I am getting input into a string, and then trying to convert it into an integer. Is this how one would go about it?

Code:
string input = "";

int *integerArray = new int[200];

for (int i = 0; i < 199; i++)
{//begin for
        getline(cin, input);
        integerArray[i] = atoi[input];
}//end for

This would convert the string from input into the integerArray right?
 

mihir

VIP Member
I did not quite get the question. Can you re-word it.
And what input are you taking, String or integers or float?
And when you are taking the input from the user why save it in a file?
 

Bananapie

New Member
I did not quite get the question. Can you re-word it.
And what input are you taking, String or integers or float?
And when you are taking the input from the user why save it in a file?


There is no input from a user. It's actually from a file, but we are supposed treat it like we are getting it from the keyboard.

So when we actually execute the program, we do (file name) < (input from the directory)

It is a file with 200 random numbers.

So I am getting what I assume is a string from the file, and need to parse them into an integer.

Given, would the code I had listed actually parse from a string into the integer array?
 

Bananapie

New Member
Fast question.

So unlike JAVA, C++ requires functions that you call to be written above being called right?

So say I wrote the program

Code:
int main()
{
printArray(intArray) //this is a function I am calling in main()
}

void printArray(int array[])
{
 ladedadadfa
}

That would not fly? It would have to have printArray be above main, so that main could call printArray?

My current program, I had to write my print function and my sorting function above main so that they would be "define in the scope"

Correct?
 

mihir

VIP Member
There are two problems in the code above.
1. You need to Declare the function first before main().

void samplefunc();
void main(){


}

void samplefunc(){

printf("Function Definition ");
}
You can also use an inline function. Which has slightly different syntax.
And the second one is you do not pass Arrays as arguments like that.
 
Top