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

xwk88

macrumors regular
Original poster
May 3, 2005
100
1
I know java quite well but I want to learn one more language before the next year of school and thought c++ would be the best one to learn. I looked all over the forums and found lots of references to books but I don't really want a book I want something I can get free on the net.


I saw a couple of threads on something called cocoa is the a language and if it is, is it better for me to learn that than c++ if programing o a mac.
 

GeeYouEye

macrumors 68000
Dec 9, 2001
1,669
10
State of Denial
Scheme (and Lisp) are impossible. Learn Python.

Better yet, take a look at Objective-C. C++, for higher-level programming, is very little different from Java, except for the following:
  • Not everything is an object. The primitives are int, char, float, double, short, long, long long, and the unsigned versions of each (except float and double, IIRC).
  • main is just a function, living outside of any class.
  • classes are generally split between a .h header file which just has the instance variables and prototypes for the member functions, and .cpp files which contain the function definitions.

Other than that (for high level programming only, mind you), C++ and Java are basically the same. Low-level is much different which takes up more space and time to post than I have.

But seriously, learn Objective-C.
 

mj_1903

macrumors 6502a
Feb 3, 2003
563
0
Sydney, Australia
I would highly advise learning C over C++ and I would advise learning Objective-C over C. :)

And now for a slice of really nasty code that I am optimising today:

Code:
- (NSString *)formatStringToProperCasing
{
	NSMutableString *intern = [[NSMutableString alloc] initWithString:[self smartLowercaseString]];
	int _length = [self length];
	int i; for (i = 0; i < _length; i++)
	{
		if (i == 0)
		{
			[intern replaceCharactersInRange:NSMakeRange(0, 1) withString:[[intern substringWithRange:NSMakeRange(0, 1)] uppercaseString]];
		}
		else if (i > 2) 
		{
			if ([[intern substringWithRange:NSMakeRange(i - 2, 2)] isEqualToString:@". "] || [[intern substringWithRange:NSMakeRange(i - 2, 2)] isEqualToString:@"! "] || [[intern substringWithRange:NSMakeRange(i - 2, 2)] isEqualToString:@"? "])
			{
				[intern replaceCharactersInRange:NSMakeRange(i, 1) withString:[[intern substringWithRange:NSMakeRange(i, 1)] uppercaseString]];
			}
			else if ([[intern substringWithRange:NSMakeRange(i - 2, 2)] isEqualToString:@"."] || [[intern substringWithRange:NSMakeRange(i - 2, 2)] isEqualToString:@"!"] || [[intern substringWithRange:NSMakeRange(i - 2, 2)] isEqualToString:@"?"])
			{
				[intern replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@" %@", [[intern substringWithRange:NSMakeRange(i, 1)] uppercaseString]]];
			}
		}
	}
	return [intern autorelease];
}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
mj_1903 said:
I would highly advise learning C over C++ and I would advise learning Objective-C over C. :)

And now for a slice of really nasty code that I am optimising today:
How about this:
Code:
char upperCase(char c)
 {
	if (c >= 97 && c <= 122)
		return c - 32;
	return c;
}

char lowerCase(char c)
{
	if (c >= 65 && c <= 90)
		return c + 32;
	return c;
}

- (NSString *)formatStringToProperCasing
{
	NSString *theStr = [self smartLowercaseString];
	int l = [theStr length], i;
	char newString[l];
	for (i=0; i<l; i++)
	{
		if (i == 0)
		{
			newString[i] = upperCase(newString[i]);
		}
		else if (i > 2) 
		{
			if ((newString[i-2] == '.' || newString[i-2] == '!' || newString[i-2] == '?')  && newString[i-1] == ' ') 
			{
				newString[i] = upperCase(newString[i]);
			}
			else if ((newString[i-2] == '.' || newString[i-2] == '!' || newString[i-2] == '?')  && newString[i-1] != ' ') 
			{
				// hmm not sure how to do this one in straight C......
				//[intern replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@" %@", [[intern substringWithRange:NSMakeRange(i, 1)] uppercaseString]]];
			}
		}
	}
	return [NSString stringWithUTF8String:(const)newString];
}
:confused: :)
 

mj_1903

macrumors 6502a
Feb 3, 2003
563
0
Sydney, Australia
kainjow said:
How about this:
Code:
char upperCase(char c)
 {
	if (c >= 97 && c <= 122)
		return c - 32;
	return c;
}

char lowerCase(char c)
{
	if (c >= 65 && c <= 90)
		return c + 32;
	return c;
}

- (NSString *)formatStringToProperCasing
{
	NSString *theStr = [self smartLowercaseString];
	int l = [theStr length], i;
	char newString[l];
	for (i=0; i<l; i++)
	{
		if (i == 0)
		{
			newString[i] = upperCase(newString[i]);
		}
		else if (i > 2) 
		{
			if ((newString[i-2] == '.' || newString[i-2] == '!' || newString[i-2] == '?')  && newString[i-1] == ' ') 
			{
				newString[i] = upperCase(newString[i]);
			}
			else if ((newString[i-2] == '.' || newString[i-2] == '!' || newString[i-2] == '?')  && newString[i-1] != ' ') 
			{
				// hmm not sure how to do this one in straight C......
				//[intern replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@" %@", [[intern substringWithRange:NSMakeRange(i, 1)] uppercaseString]]];
			}
		}
	}
	return [NSString stringWithUTF8String:(const)newString];
}
:confused: :)

Basically what I ended up doing. :)
 

GeeYouEye

macrumors 68000
Dec 9, 2001
1,669
10
State of Denial
Avoid using substringWithRange: (and subdataWithRange: additionally) in a for loop. It creates something like 3 autoreleased objects; if you don't have NSAutoreleasePool alloc/inits and releases inside the for loop (which have a bit of overhead, but not too much), the amount of memory consumed goes through the roof, hugely slowing down the program.
 

Mechcozmo

macrumors 603
Jul 17, 2004
5,215
2
Learn AppleScript. Works on ANY Mac since like, System 7 if you do it right. Or if you use OS X specific codes, 2000 and up. And they are small. And the programming editor is free.

:p

But seriously, if you are getting used to concepts and stuff in programing AppleScript isn't too shabby.
 

xwk88

macrumors regular
Original poster
May 3, 2005
100
1
how do I compile a cpp file from terminal I'm having too many problems creating a c++ project in xcode so I'm going to use terminal which might be easier, but if you guys have any idea on how to create a simple project on xcode or a better free ide for c++ I thouth about dev but found no versions for mac.
 

cube

Suspended
May 10, 2004
17,011
4,973
csubear said:
Ug... C++ is a very solid intro for obect oriented programing.

For subject-oriented programming it might be an intro (and not a good one anyway, as the language is a POS). If you want real object-oriented programming, you have to look at CLOS, tyniclos, Cecil to name some. Although if you consider encapsulation to be a mandatory feature of object-oriented programmimng, the only alternative is Cecil.
 

superbovine

macrumors 68030
Nov 7, 2003
2,872
0
therevolution said:
To compile:

Code:
g++ filename
This will create your program under the name a.out . To run it:

Code:
./a.out

the other way is: g++ <filename> -o <executable name>
without <>

or make a script. generally when i had to do homework for class project i usually made a small script not a formal make file. the file name was only 1 character long so i could just type "./c" and compile and test my program.
 

GeeYouEye

macrumors 68000
Dec 9, 2001
1,669
10
State of Denial
cube said:
It's only impossible for people who haven't bothered enough to get it.
Oh I bothered to get it, and I still found it impossible to work with. It is, in short, a read-eval()-print loop which has some nice features (lexical closures, anonymous functions, super-easy recursion), obscured in a syntax so convoluted (what do you expect with a single data type?) only an expert can read the code: just look at macros. For most of the power at a fraction of the cost, plus access to system calls and C libraries, look at Ruby (via irb) or Python. Includes all the magic of the end of an eval()-print, but stored in a variable you can actually do something with, because not everyone wants to write thirty line expressions with a dozen ')' at the end, only to find that you didn't close one somewhere resulting in an error trying to execute it. And Lisp users wonder why anyone would use C! Hah!
 

Davito

macrumors member
Jun 16, 2004
63
0
Zurich, Switzerland
If you want to program under OS X only, I strongly recommend you learn objective-c and cocoa. If you are already familiar with object-orientaded programming, the book of Aaron Hillegass is great for learning cocoa, together with the online-documentation for developers from apple you should have no problem learning objective-c at the same time. Good luck :)
 

cube

Suspended
May 10, 2004
17,011
4,973
GeeYouEye said:
Oh I bothered to get it, and I still found it impossible to work with. It is, in short, a read-eval()-print loop which has some nice features (lexical closures, anonymous functions, super-easy recursion), obscured in a syntax so convoluted (what do you expect with a single data type?) only an expert can read the code: just look at macros. For most of the power at a fraction of the cost, plus access to system calls and C libraries, look at Ruby (via irb) or Python. Includes all the magic of the end of an eval()-print, but stored in a variable you can actually do something with, because not everyone wants to write thirty line expressions with a dozen ')' at the end, only to find that you didn't close one somewhere resulting in an error trying to execute it. And Lisp users wonder why anyone would use C! Hah!

- Lisp's syntax is its greatest strength, and it's the simplest one available.
- What single datatype? There are many datatypes in Lisp.
- You can call C libraries from Lisp, which gives you access to system calls.
- You use something like XEmacs to edit Lisp. There's no problem with parentheses.

I didn't need a teacher to get it, but obviously you are one of the people that do (but not a faux Lisp teacher who doesn't really know it).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.