Basic C++

Computer builds, hardware and software discussion or troubleshooting, including peripherals. Essentially a general place to talk about desktop computers.
User avatar
MikeDB
Posts: 1224
Joined: Fri May 13, 2005 3:09 pm

Basic C++

Post by MikeDB »

Hi all just wondering if anyone could teach me some basic C++ becuase i found that google aint so good.

Archived topic from Iceteks, old topic ID:3314, old post ID:26878
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Basic C++

Post by Red Squirrel »

Good luck with that. I know exactly how it is, I've been wanting to brush up C++ but it's hard to find the right resources, I find the basic stuff is the hardest. For example, I've been trying to turn a socket application into non blocking, and set a time out for connect for the longest time. I KNOW its something really simple, but it's the thing of knowing how to do it. And you can try C++ forums but I find C++ programmers tend to be rude about helping others, I don't know why. Lot of them assume you should just know stuff off the bat, but that's not how things work.

Feel free to ask your questions here and maybe someone can help, I think a few members here know C++ and I know enough to help with basic stuff though I have not programmed much in a while.

Books are the best bet, but they're bloody expensive, and hard to get in Canada unless you want to order from the US and pay insane shipping and border fees. But it IS an option, and probably the best way to learn.

Archived topic from Iceteks, old topic ID:3314, old post ID:26881
Honk if you love Jesus, text if you want to meet Him!
Cold Drink
Posts: 96
Joined: Fri Jul 16, 2004 1:04 pm

Basic C++

Post by Cold Drink »

Look for Thinking in C++ and Beej's Guide to Network Programming. Both free and effective. Although both assume some level of competance with C, you can wing it without.

Archived topic from Iceteks, old topic ID:3314, old post ID:26884
User avatar
MikeDB
Posts: 1224
Joined: Fri May 13, 2005 3:09 pm

Basic C++

Post by MikeDB »

hmmm well what about C? can someone bother with that? or maybe something else? I was hoping on making a thing so when you activated it, it had a popup that said "hello!"

Archived topic from Iceteks, old topic ID:3314, old post ID:26903
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Basic C++

Post by Red Squirrel »

C++ is actually easier then C, at least I find. Only difference I know is i/o, and C++'s i/o is much easier. ex: you can go cout<<"string"<<variable<<"string"<<variable<<variable; and mix and match like that. In C, you'll have to have a bunch of printf's and all that, forget exactly how it works, I just know C++ is easier in that sense.

Archived topic from Iceteks, old topic ID:3314, old post ID:26904
Honk if you love Jesus, text if you want to meet Him!
User avatar
MikeDB
Posts: 1224
Joined: Fri May 13, 2005 3:09 pm

Basic C++

Post by MikeDB »

Ok cool... now i have to find someone who has alot of spare time on ther hands. :biglaugh:

Archived topic from Iceteks, old topic ID:3314, old post ID:26910
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

Basic C++

Post by wtd »

If you're new to programming, why not take a look at a truly high-level programming language?

The biggest benefits is probably the presence of interactive interpreters in languages like Ruby, Python, Haskell, Scheme, and O'Caml.

Let's look at "Hello world" programs in a number of languages.

Code: Select all

/* C */

#include <stdio.h>

int main()
{
   puts("Hello world!");

   return 0;
}[code]

[code]// C++

#include <iostream>

int main()
{
   std::cout << "Hello world!" << std::endl;

   return 0;
}[code]

[code](* O'Caml *)

print_endline "Hello world!";;[code]

[code]-- Haskell

main = putStrLn "Hello world!"[code]

[code]# Perl

print "Hello world!
";[code]

[code]# Python

print "Hello world!"[code]

[code]# Ruby

puts "Hello world!"[code]

[code]; Scheme

(display "Hello world!")
(newline)[code]

[code]// D

int main()
{
   printf("Hello world!
");

   return 0;
}[code]

[code]// Java

public class HelloWorld
{
   public static void main(String[] args)
   {
      System.out.println("Hello world!");
   }
}[code]

[code]// C#

using System;

public class HelloWorld
{
   public static void Main(string[] args)
   {
      Console.WriteLine("Hello world!");
   }
}[code]

[code]-- Eiffel

class HELLO_WORLD
creation make
feature
   make is
      do
         std_output.put_string("Hello world!")
         std_output.put_new_line
      end
end[code]

[code]-- Ada95

with Ada.Text_IO; use Ada,Text_IO;
procedure Hello_World is
begin
   Put_Line("Hello world!);
end Hello_World;[code] 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:3314, old post ID:26951[/size][/color]
User avatar
MikeDB
Posts: 1224
Joined: Fri May 13, 2005 3:09 pm

Basic C++

Post by MikeDB »

Ok i sort of get it. Now I have Edit Plus 2 if i were to use this would i put it in there and save it as a C++ file? (If i were to use C++)

Archived topic from Iceteks, old topic ID:3314, old post ID:26952
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Basic C++

Post by Red Squirrel »

I don't think extension matters but usually you save as a .cpp file and .h file for header files.

Archived topic from Iceteks, old topic ID:3314, old post ID:26953
Honk if you love Jesus, text if you want to meet Him!
User avatar
MikeDB
Posts: 1224
Joined: Fri May 13, 2005 3:09 pm

Basic C++

Post by MikeDB »

And that means?...

Archived topic from Iceteks, old topic ID:3314, old post ID:26955
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

Basic C++

Post by wtd »

Red Squirrel wrote: I don't think extension matters but usually you save as a .cpp file and .h file for header files.
Accepted extensions for C++ source files are typically .cpp, .cxx, and .C. The latter is not looked upon well due to case-insensentive file systems. ".cpp" is usually preferred. ".h" is used for all header files. ".hpp" is an option, which differentiates between C and C++ header files, but is rarely used.

Archived topic from Iceteks, old topic ID:3314, old post ID:26956
User avatar
MikeDB
Posts: 1224
Joined: Fri May 13, 2005 3:09 pm

Basic C++

Post by MikeDB »

OOK i still dont understand what you are talking about but whatever.

Archived topic from Iceteks, old topic ID:3314, old post ID:26957
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

Basic C++

Post by wtd »

I would highly recommend starting out with a language that has an interactive interpreter where you can enter code, run it and immediately see results. No saving to a file, compiling, running as separate steps.

Image

Archived topic from Iceteks, old topic ID:3314, old post ID:26960
User avatar
MikeDB
Posts: 1224
Joined: Fri May 13, 2005 3:09 pm

Basic C++

Post by MikeDB »

Ok and how do i do that?

Archived topic from Iceteks, old topic ID:3314, old post ID:26962
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

Basic C++

Post by wtd »

Install the interpreters. :)

O'Caml
Ruby
Python
Haskell



Archived topic from Iceteks, old topic ID:3314, old post ID:26964
User avatar
MikeDB
Posts: 1224
Joined: Fri May 13, 2005 3:09 pm

Basic C++

Post by MikeDB »

Oki Doki

Archived topic from Iceteks, old topic ID:3314, old post ID:26972
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

Basic C++

Post by wtd »

Once you're done that you'll have the various tools available in your Start menu somwhere. I'm in Linux at the moment (yes, they all work within Linux, Windows, Mac OS X, etc.), so I can't say for certain. :)

Archived topic from Iceteks, old topic ID:3314, old post ID:26973
syb
Posts: 222
Joined: Wed Jun 18, 2003 10:12 pm

Basic C++

Post by syb »

I never heard of those except for Python. I looked each one up and read the short intro on them. I have a C++ for dumies book. I was going to try and give a crack but now I don't know weather I should. What's the major differnce between those and more commonly know C++ ?

Archived topic from Iceteks, old topic ID:3314, old post ID:27074
The wisdom of sight comes from the father of lights
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

Basic C++

Post by wtd »

Well, Python and Ruby are different in that they're not statically typed.

You see, in C++, every variable has a type.

Code: Select all

int a;
double b;[code]

"a" is an integer and "b" is a double precision floating point number.

However, the value stored in the variable has no type.

Now, in Python and Ruby it's exactly the opposite.  The value has a type, and the variable doesn't.  The variable is just a name.  I can reuse the name for an entirely different kind of value.

I could assign a value of 5 to "a", then immediately afterwards assign a string to the same variable.

[code]a = 5
a = "hello"[code]

This is perfectly legal in both Python and Ruby (and at least here they have the same syntax).

The way things are enforced in this kind of situation is what's often called "duck typing".  That is, if my code wants a duck, and I give it something that quacks like a duck and walks like a duck, then what I've given it might as well be a duck.  :)

Ruby and Python also feature more friendly syntax than C++.  It's more straightforward.  In C+ the keyword "const" can have about half a dozen different meanings depending on where it is.  This certainly gives the language flexibility, but it comes with a steep learning curve attached, and that steep learning curve can be frustrating.

Both languages make it easier to deal with object-oriented programming, and they feature conveniences for even simpler bits of code.  Want to loop over an array in Python or Ruby, as compared to C++?

C++
[code]int a[] = {3, 4, 5, 1, 2, 9, 8};

for (int i(0); i < 7; ++i)
{
   std::cout << a[i] << std::endl;
}[code]

Python:
[code]a = [3, 4, 5, 1, 2, 9, 8]

for x in a: 
   print a[code]

Ruby:
[code]a = [3, 4, 5, 1, 2, 9, 8]

for x in a 
   puts x
end[code]

Ruby (other option):
[code]a = [3, 4, 5, 1, 2, 9, 8]

a.each { |x| puts x }[code]

Now, O'Caml and Haskell are a bit different.  They are statically-typed like C++, but they infer types based on how you use values.  They also support generics much better than C++, and generally provide lots of syntctic convenience.

They make implementing data structures beautiful.  Let's consider creating a binary tree.

C++
[code]#include <iostream>

template <typename T>
class binary_tree_node
{
   private:
      const T value;
      binary_tree_node<T> * left, * right;
   public:
      binary_tree_node(T init_value)
      : value(init_value)
      , left(NULL), right(NULL)
      { }

      binary_tree_node(T init_value, binary_tree_node<T> * l, binary_tree_node<T> * r)
      : value(init_value)
      , left(l), right(r)
      { }

      ~binary_tree_node()
      {
         if (left) delete left;
         if (right) delete right;
      }

      int length() const
      {
         int len(1);
         if (left) len += left->length();
         if (right) len += right->length();
         return len;
      }
};[code]

O'Caml:
[code]type 'a binary_tree = Empty | Leaf of 'a | Branch of 'a binary_tree * 'a * 'a binary_tree;;

btree_length tree = 
   match tree with
      Empty -> 0
    | Leaf _ -> 1
    | Branch (l, _, r) -> 1 + btree_length l + btree_length r;;[code]

Haskell:
[code]data BinaryTree a = Empty | Leaf a | Branch (BinaryTree a) a (BinaryTree a)

btreeLength Empty = 0
btreeLength (Leaf _) = 1
btreeLength (Branch l _ r) = 1 + btreeLength l + btreeLength r[code]

And for all four, with some exceptions in Haskell's case, you needn't create any source files or do any compilation.  Just type your code at the prompt and hit return. 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:3314, old post ID:27078[/size][/color]
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Basic C++

Post by Red Squirrel »

Actually when I started learning php (alreading knowing C++) I found that to be nice that you don't have to define types. Saves allot of headaches I find, for example a function that needs a char, but some other function returns a Cstring, then you have to somehow convert it to char so you can use the function. In php you don't have to worry about that. Php variables are also unlimited, well limited to memory I guess. But what's nice is you don't have to pre-allowcate the memory for each var.

I once found a way to run php as an interpreter and type commands directly in it and get the result, but I just checked and can't seem to find it now.

Archived topic from Iceteks, old topic ID:3314, old post ID:27079
Honk if you love Jesus, text if you want to meet Him!
syb
Posts: 222
Joined: Wed Jun 18, 2003 10:12 pm

Basic C++

Post by syb »

Interesting.. I am defiantly going to look into to it. Just one thing. Commercially which is used more often or are some better then others for a specific type of programing?

Archived topic from Iceteks, old topic ID:3314, old post ID:27082
The wisdom of sight comes from the father of lights
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Basic C++

Post by Red Squirrel »

C++ is probably the most comonly used language for applications, while php and asp are the in the lead in terms of web based programming, php is probably more widely used. asp is Microsoft's version of php. Not sure wether or not it's good as I've never tried it, but some sites (such as a MS's :P) do use it.

Visual basic is another one that has not been mentioned, but it's ...basic. Personally I don't like it, the syntax is just way off track compared to other languages. It will do nothing but confuse you if you are learning another language at the same time. But it can be useful for making quick windows based applications.

Archived topic from Iceteks, old topic ID:3314, old post ID:27084
Honk if you love Jesus, text if you want to meet Him!
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

Basic C++

Post by wtd »

syb wrote: Interesting.. I am defiantly going to look into to it. Just one thing. Commercially which is used more often or are some better then others for a specific type of programing?
That's a very complicated question, so it deserves a thorough answer.

Certainly C and C++ are used very widely in the business world. Java is becoming their new mainstay as well. Visual Basic and C# will be relevant simply due to Microsoft's influence on the market.

That pretty much covers the widespread, really boring "corporate" languages. :)

Now, what about the interesting ones?

Well, Objective-C is the language of choice for Mac development. It uses the Cocoa framework, which is just a work of art and a joy to use. Obviously, the Mac market is relatively small, comparatively, but it's an enthusiastic market, and that counts for a lot. You canw rite a killer program for Windows and you'll find there are already ten other programs that do the same thing. There're still lots of opportunities on the Mac side of the fence to make an impact.

Ada is big in the aerospace and defense industries. It's taken a lot of abuse, but it has a lot of really neat features, and the fact that it's relatively low-profile means there might be good opportunities if you can put it on your resume.

Python is the new Java. It's getting lots of hype, and lots of use at places like ILM and notably in the next version of Civ3, among many other uses. I believe Google even gives preference or did to programmers with Python in their toolbox.

If Python is the new Java, then Ruby is the new Python. It's exciting and it's starting to get a lot of attention due to Ruby on Rails, an amazingly easy framework for building web applications. Definitely worth checking out to "get in on the ground floor".

O'Caml is gaining popularity, mostly as an alternative to C++ which offers really fast executables and easier coding.

Haskell is mostly academic, but it has a lot to teach. Haskell programmers are better C++ programmers.

In conclusion...

I've learned a lot of programming languages over the years, so take it from me, it gets easier each time.

Since it gets easier each time, your best bet is to learn the languages that make it easiest first. You hae time before you're going to be looking for a programming job anyway, so learn a few other languages like Ruby, Python, or O'Caml before C++ or even Java.

The biggest stumbling block I think is going to be object-oriented programming, and wrapping your mind around it. It was for me (until I got to the serious functional programming).

You're going to be happy for an interactive interpreter when you're trying to grasp concepts like inheritance, encapsulation, and polymorphism. You're going to write a lot of stupid little classes that do nothing, and having to go through the "edit, save, compile, run" process each time you make a change will get frustrating.

In conclusion: don't worry about which language is used the most. Pick the one that'll make learning concepts the easiest. Save the hard stuff for later.

Archived topic from Iceteks, old topic ID:3314, old post ID:27086
syb
Posts: 222
Joined: Wed Jun 18, 2003 10:12 pm

Basic C++

Post by syb »

I think i'll start with python. Thank you for the info.

Archived topic from Iceteks, old topic ID:3314, old post ID:27092
The wisdom of sight comes from the father of lights
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

Basic C++

Post by wtd »

Feel free to ask Python questions.

Archived topic from Iceteks, old topic ID:3314, old post ID:27093
Locked