Page 1 of 1

C++ Basics 1 - Comments and Data Types Rate Topic: -----

#1 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1199
  • View blog
  • Posts: 7,308
  • Joined: 07-September 06

Post icon  Posted 22 September 2008 - 06:07 PM

This is yet another one of my basics tutorial, however, as you can tell this one is on C++, and not one of the web programming languages like I normally do tutorials on.

Starting out
Before you can start programming in C++ it is a good idea to get a compiler that works well. I recommend getting Visual C++ 2008 express edition, as it is free and fairly powerful, but there are a number of compilers out there.

Basic C++ applications start out like so:
int main(){
  return EXIT_SUCCESS;
}


Using Comments in Code
Comments in C++ can consist of either a single line comment: // comment here, or a block comment: /* Comment here
and here
and here */


In source code comments can look like so:

int main(){ // create the main function
  return EXIT_SUCCESS; // exit saying that the program ran successfully
}


If you wanted to use block comments instead that can look like so:
/* This function attempts to return
an integer upon successful completion
*/
int main(){
  return EXIT_SUCCESS;
}


However, people normally format their block comments like so:
/* This function attempts to return
 *an integer upon successful completion
 */
int main(){
  return EXIT_SUCCESS;
}


To avoid confusion.

NOTICE - There is an additional * and space in each line to make the comment block line up correctly.

Data Types
There are a number of "primitive data types" in C++ that consist of int, double, float, long, char, bool, and others. These are known as "primitives" because they don't require that you import anything into your code to use them successfully.

There are also more complex data types, like String, or Student (we will create a simple class for this in a second). The complex data types need to be initialized to work correctly.

Here is a simple example of a student class:
class Student{
private:
	char* name;
public:
	Student(char* nName){
		name = nName;
	}
	char* getName(void){
		return name;
	}
};


Which can be implemented like so:
Student* test = new Student("Freddy");

	printf("The Student %s\n", test->getName());
	return 0;


The end
I know this isn't the best tutorial, but I hope it shows some of the basics behind C++.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Basics 1 - Comments and Data Types

#2 Xer0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-September 08

Posted 04 October 2008 - 08:07 PM

It's actually a really good tutorial.

Simple, yet effective. You have put code into action and shown users what it does which is something not everyone does.

Good job, pal!

Edit* I might even use it myself :$ .

This post has been edited by Xer0: 04 October 2008 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Posted 07 October 2008 - 05:37 AM

Nice writeup~ I like the way you even managed to show some basic OOP principals in such an intro topic. :)

A few things to say though. printf() is a C-standard function. It's compatible with C++ since C++ is backwards compatible with C, but it's more common to stick with C++ standards where possible. (example, for basic console I/O)

So,
printf("The Student %s\n", test->getName());
will actually be
cout << "The Student " << test->getName();


Also, char* is C standard for strings~ why not make use of the string class?

Lastly, I think you missed explaining the purpose of that get() function. It's used to access a private variable.

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1