C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,480 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,763 people online right now. Registration is fast and FREE... Join Now!




C++ Basics 1 - Comments and Data Types

 
Reply to this topicStart new topic

> C++ Basics 1 - Comments and Data Types

BetaWar
Group Icon



post 22 Sep, 2008 - 05:07 PM
Post #1


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:
cpp
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:

cpp
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:
cpp
/* This function attempts to return
an integer upon successful completion
*/
int main(){
return EXIT_SUCCESS;
}


However, people normally format their block comments like so:
cpp
/* 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:
cpp
class Student{
private:
char* name;
public:
Student(char* nName){
name = nName;
}
char* getName(void){
return name;
}
};


Which can be implemented like so:
cpp
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++.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Xer0
*



post 4 Oct, 2008 - 07:07 PM
Post #2
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: 4 Oct, 2008 - 07:07 PM
Go to the top of the page
+Quote Post

gabehabe
Group Icon



post 7 Oct, 2008 - 04:37 AM
Post #3
Nice writeup~ I like the way you even managed to show some basic OOP principals in such an intro topic. smile.gif

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,
cpp
printf("The Student %s\n", test->getName());
will actually be
cpp
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.

smile.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 03:47AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month