Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Watabou

macrumors 68040
Original poster
Feb 10, 2008
3,426
759
United States
I'm beginning my c++ programming.

So I'm kinda of a newbie. I learned java if that helps.
I'm a beginner so please help me as I'm really new to programming c++. Basically, I don't know anything. So I hope someone can help me here.
I looked for a compiler for c++ and tried both eclipse and XCode. I don't know how to use any of them :(

I used eclipse for java programming in class and this was easy to use. So thinking that, I installed both XCode and eclipse to give them both a try.

Tried XCode but that seemed unfamiliar and I Started off as New-> Project and when I selected, C++ Carbon, it included bunch of Mac specific files.

So I tried eclipse. Whenever I create a new project, it creates two classes for one class I create. For example, I created HelloWorld class and it created two classes for me: "HelloWorld.cpp" and "HelloWorld.h" What am I supposed to do with those?

I typed this code in the cpp class and then ran it and it gave me an error saying no binary found. What does that mean? THis is the code:

Code:
#include "Code.h"
#include <iostream>

using namespace std;

int main()
{

cout<<"Hello World!\n";
cin.get();
}

And in Eclipse, when I created the class, in the cpp class this code was already inserted by eclipse :

Code:
#include "HelloWorld.h"

HelloWorld:: HelloWorld() {
	// TODO Auto-generated constructor stub

}

HelloWorld::~ HelloWorld() {
	// TODO Auto-generated destructor stub
}

And this was included in the HelloWorld.h file it already created for me:

Code:
#ifndef HELLOWORLD_H_
#define HELLOWORLD_H_

class HelloWorld {
public:
	HelloWorld();
	virtual ~HelloWorld();
};

#endif /* HELLOWORLD_H_ */

If someone can help me how to compile in either eclipse or XCode, I will be very grateful. Remember, I'm just starting out.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Code:
#include <iostream>

using namespace std;

int main()
{

cout<<"Hello World!\n";
cin.get();
}
I modified the code you posted very slightly, removing the include that I didn't see a need for. I saved this file as hello.cpp. After that, from the terminal, I ran:
g++ -o hello hello.cpp
and ran the program it generated with:
./hello
I got the output:
Hello World!

And the program waited for a line of input then exited (with an implicit return of 0).

At this level, using an IDE is overkill. Use a text editor and g++ to compile and the terminal to run programs. You'll get a better feel for what's going on.

-Lee
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Sorry, double post.
Wanted to take it a step further, here's a version that uses a class, some std::strings, etc.:

TestHello.cpp
Code:
#include <iostream>
#include "HelloWorld.h"
using namespace std;

int main(int argc, char *argv[]) {
  HelloWorld myHello;
  myHello.display();
  cin.get();
  return 0;
}

HelloWorld.h
Code:
#ifndef HELLOWORLD_H_
#define HELLOWORLD_H_
#include <string>
class HelloWorld {
public:
        HelloWorld();
        ~HelloWorld();
        void display();
private:
         std::string ToWho;
};

#endif /* HELLOWORLD_H_ */

HelloWorld.cpp
Code:
#include "HelloWorld.h"
#include <iostream>

HelloWorld:: HelloWorld() {
  ToWho = "World";
}

HelloWorld::~ HelloWorld() {
  ToWho.clear();
}

void HelloWorld::display() {
  std::cout << "Hello, " << ToWho << "!" << std::endl;
}

Compiled with:
g++ -o TestHello TestHello.cpp HelloWorld.cpp

and run with:
./TestHello

The result is:
Hello, World!

And the program waits for a line of input, then exits with an explicit return code of 0. Any C++ experts might have advice on better practices, etc... i'm not much of a C++ programmer.

-Lee

Edit: Doing some reading, apparently "using namespace" in .h is frowned upon. Removed this.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi Lee,

In your example code, the constructor is best written using an initialisation list, ie

Code:
HelloWorld:: HelloWorld()

   :   ToWho( "World" )

{
}

In this way, ToWho uses a constructor that initialises the string. This is better and faster I think than using the default string constructor and then assigning a value in the body of the HelloWorld constructor (which is what your constructor was doing).

Also the HelloWorld destructor will automatically call the destructor for member variables, so there is no need to clear ToWho first, ie the destructor should simply be:-

Code:
HelloWorld::~ HelloWorld()
{
}

(as a side note, since, in this case , the destructor is empty, there is no need to declare or implement it).

RAII is the fancy term for the way C++ manages resource allocation and release. Memory management in C++ is much easier in my opinion than Obejctive-C!

b e n
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX

Good times. At least I managed to write something that compiled, ran properly, and didn't leak, even if it was overly verbose and sub-optimal =). I'm glad someone was able to point out the right way to do this.

Protip for beginners: You can even mess up Hello World. =)

-Lee
 

Watabou

macrumors 68040
Original poster
Feb 10, 2008
3,426
759
United States
I modified the code you posted very slightly, removing the include that I didn't see a need for. I saved this file as hello.cpp. After that, from the terminal, I ran:
g++ -o hello hello.cpp
and ran the program it generated with:
./hello
I got the output:
Hello World!
the terminal to run programs. You'll get a better feel for what's going on.

-Lee

Ok Thanks for helping but where did you write the code? Text Edit or XCode? I don't even see a editor there. Sorry for being so stupid but I really can't see how to bring the editor in Xcode.

Edit: OK, So I got Xcode to show the editor when I created an empty project and then created a c++ file. Then I wrote the code and saved the file as hello.cpp to my desktop. How do I access my desktop through the terminal again> Wasn't it some cd/desktop or something like that? I can't find it on google.


EDIT2: Success! I got the result. I saved the file to my desktop right? I accessed my desktop through the terminal via this command: cd ~/Desktop

and then ran the g++ -o hello hello.cpp

and then double clicked the Executable file it created on the desktop and saw Hello World there.

Although I don't know what the ./hello you were talking about is. But thanks lee and lazydog.

Ok So now what I wanted to know is what the hell is the .cpp and .h after the file names? And How do I save the .h file? Same way as the .cpp?

Is the .cpp like a main class like how Java's is? In Java, I know that you can have multiple classes and one main class that has the "public static void main()" function.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Watabou

If you want to use Xcode, then start a new project, and select C++ command line tool. In the project tree on the left, look around and find the main.cpp file. It will be in a folder called src. You can bring the editor window up either by double clicking on a source file, selecting the menu item View->Zoom Editor In, or using the key combination shift command E.

Lee, your example code was fine and very useful for a newbie. I've always admired the amount of time and energy you put into helping people on this forum.

b e n
 

Watabou

macrumors 68040
Original poster
Feb 10, 2008
3,426
759
United States
Watabou

If you want to use Xcode, then start a new project, and select C++ command line tool. In the project tree on the left, look around and find the main.cpp file. It will be in a folder called src. You can bring the editor window up either by double clicking on a source file, selecting the menu item View->Zoom Editor In, or using the key combination shift command E.

b e n

Hmm, when I create a new project, I'm met with this window and it only has the Carbon C++ and Carbon C++ Standard applications. I just create a empty project right?

Edit: Lazydog, thanks so much, I just had to scroll down to command line and expand it. It worked. Thanks!

P.S. Now the .cpp and .h help please :) thanks.
 

Attachments

  • Picture 1.png
    Picture 1.png
    63 KB · Views: 77

Sam Yikin

macrumors regular
Oct 28, 2007
230
0
Hmm, when I create a new project, I'm met with this window and it only has the Carbon C++ and Carbon C++ Standard applications. I just create a empty project right?

Edit: Lazydog, thanks so much, I just had to scroll down to command line and expand it. It worked. Thanks!

P.S. Now the .cpp and .h help please :) thanks.

Command Line Utility->C++ Tool.
 

Attachments

  • Picture 38.jpg
    Picture 38.jpg
    63.1 KB · Views: 59

Watabou

macrumors 68040
Original poster
Feb 10, 2008
3,426
759
United States
Command Line Utility->C++ Tool.

Yep I found it before in my previous post. Thanks anyways though lol

Now all I want help is on what .cpp and .h is.
As I also said in my previous post:

Ok So now what I wanted to know is what the hell is the .cpp and .h after the file names? And How do I save the .h file? Same way as the .cpp?

Is the .cpp like a main class like how Java's is? In Java, I know that you can have multiple classes and one main class that has the "public static void main()" function.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Now all I want help is on what .cpp and .h is.

cpp stands for (c) (p)lus (p)lus. It's to indicate the type of source file, as .c is generally a C-only source file. From the gcc man page:
file.cc
file.cp
file.cxx
file.cpp
file.CPP
file.c++
file.C
C++ source code which must be preprocessed. Note that in .cxx, the
last two letters must both be literally x. Likewise, .C refers to
a literal capital C.

It also lists .ii as a c++ source file that will not go through the pre-processor.

A .h file is a header file. This name is shared with C. I have seen .hpp as well. Since the compiler does not deal with these files directly, and instead their text is pulled in to other source files by means of #include statements, their extension is for human-recognizability, and not for the compiler's benefit.

The .cpp files will contain your source code, function implementations, etc. The .h files will contain things that need to be shared amongst source files such as struct definitions, function prototypes, class declarations, some #define declarations, etc. You could get away with never having a .h file, but you would have to duplicate things in every source file you wanted to use a particular class, function, etc. in so the compiler would know the types of the functions you were using, etc.

.cpp files and .h files are both just plain text. You should be able to save them in the same manner.

Code:
int main(int argc, char *argv[])

Is the same as java's
Code:
public static void main(String[] args)

This is the program's entry point. Each .cpp does not contain this function, only one per program. This is the same as java (you can have lots of classes defined, but you only have one class that has a main entry point per program. C and C++ are a little more free-form than java, so you may have helper functions in their own files, in the same file as main, etc.

-Lee
 

Watabou

macrumors 68040
Original poster
Feb 10, 2008
3,426
759
United States
Thank you very much lee. I really appreciate the help. I will keep everything you said in my mind.

Thank you, again. :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.