background preloader

Basic

Facebook Twitter

Placeholder(where i am)

C++ Tutorial - Introduction to C. This tutorial series is designed for everyone: even if you've never programmed before or if you have extensive experience programming in other languages and want to expand into C++! It is for everyone who wants the feeling of accomplishment from a working program. What do I mean? C++ is a programming language--it will allow you to control your computer, making it do what you want it to do.

This programming tutorial series is all about helping you take advantage of C++. Getting Set Up - C++ Compilers The very first thing you need to do, before starting out in C++, is to make sure that you have a compiler. Intro to the C++ Language A C++ program is a collection of commands, which tell the computer to do "something". But how does a program actually start? So how do you get access to those prewritten functions? Let's look at the elements of the program. The next important line is int main(). The next line of the program may seem strange. The next command is cin.get(). Case Sensitivity. If Statements in C++ - Cprogramming.com Tutorial.

The ability to control the flow of your program, letting it make decisions on what code to execute, is valuable to the programmer. The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based upon the user's input. For example, by using an if statement to check a user entered password, your program can decide whether a user is allowed access to the program.

Without a conditional statement such as the if statement, programs would run almost the exact same way every time. Before discussing the actual structure of the if statement, let us examine the meaning of TRUE and FALSE in computer terminology. When programming, the aim of the program will often require the checking of one value stored by a variable against another value to determine whether one is larger, smaller, or equal to the other. For example: Else. For, While, and Do While Loops in C. Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. (They may be executing a small number of tasks, but in principle, to produce a list of messages only requires repeating the operation of reading in some data and displaying it.)

Now, think about what this means: a loop lets you write a very simple statement to produce a significantly greater result simply by repetition. One Caveat: before going further, you should understand the concept of C++'s true and false, because it will be necessary when working with loops (the conditions are the same as with if statements). There are three types of loops: for, while, and do..while. Each of them has their specific uses. They are all outlined below. FOR - for loops are the most useful type. Functions in C and C. Now that you should have learned about variables, loops, and conditional statements it is time to learn about functions. You should have an idea of their uses as we have already used them and defined one in the guise of main. cin.get() is an example of a function. In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.

Functions that a programmer writes will generally require a prototype. Just like a blueprint, the prototype tells the compiler what the function will return, what the function will be called, as well as what arguments the function can be passed. When I say that the function returns a value, I mean that the function can be used in the same manner as a variable would be. For example, a variable can be set equal to a function that returns a value between zero and four.

For example: The general format for a prototype is simple: return-type function_name ( arg_type arg1, ..., arg_type argN ); Still not getting it? Switch Case in C and C. Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point. The condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. The default case is optional, but it is wise to include it as it handles any unexpected cases. Below is a sample program, in which not all of the proper functions are actually declared, but which shows how one would use switch in a program.

Still not getting it? Tutorial: Pointers. Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need. Wow, that's kind of cool.

Actually, it's very cool, as we'll see in some of the next tutorials. For now, let's just get a basic handle on what pointers are and how you use them. What are pointers? Pointers are aptly named: they "point" to locations in memory. The cool thing is that once you can talk about the address of a variable, you'll then be able to go to that address and retrieve the data stored in it. C++ Pointer Syntax The pointer declaration looks like this: Tutorial: Structures. Before discussing classes, this lesson will be an introduction to data structures similar to classes. Structures are a way of storing many different values in variables of potentially different types under the same name.

This makes it a more modular program, which is easier to modify because its design makes things more compact. Structs are generally useful whenever a lot of data needs to be grouped together--for instance, they can be used to hold records from a database or to store information about contacts in an address book. In the contacts example, a struct could be used that would hold all of the information about a single contact--name, address, phone number, and so forth. The format for defining a structure is struct Tag { Members }; Where Tag is the name of the entire type of structure and Members are the variables within the struct. Struct Tag name_of_single_structure; To access a variable of the structure it goes name_of_single_structure.name_of_variable; For example: Arrays in C and C. Arrays are useful critters because they can be used in many ways to store large amounts of data in a structured way.

For example, a tic-tac-toe board can be held in an array. Arrays are essentially a way to store many values under the same name. You can make an array out of any data-type including structures and classes. Think about arrays like this: Each of the bracket pairs is a slot(element) in the array, and you can put information into each one of them. Let's look at the syntax for declaring an array. int examplearray[100]; // This declares an array This would make an integer array with 100 slots, or places to store values(also called elements). What can you do with this simple knowledge? For example: char astring[100]; will allow you to declare a char array of 100 elements, or slots. The most useful aspect of arrays is multidimensional arrays. This is a graphic of what a two-dimensional array looks like when I visualize it. int twodimensionalarray[8][8]; or, for two dimensional arrays. C Strings in C and C. In C++ there are two types of strings, C-style strings, and C++-style strings.

This lesson will discuss C-style strings. C-style strings are really arrays, but there are some different functions that are used for strings, like adding to strings, finding the length of strings, and also of checking to see if strings match. The definition of a string would be anything that contains more than one character strung together.

For example, "This" is a string. However, single characters will not be strings, though they can be used as strings. Strings are arrays of chars. String literals are words surrounded by double quotation marks. "This is a static string" To declare a string of 49 letters, you would want to say: char string[50]; This would declare a string with a length of 50 characters. TAKE NOTE: char *arry; Can also be used as a string. Arry = new char[256]; which allows you to access arry just as if it were an array. For example: delete [] arry. The prototype for that function is: For a example: C++ File I/O Tutorial. This is a slightly more advanced topic than what I have covered so far, but I think that it is useful.

File I/O is reading from and writing to files. This lesson will only cover text files, that is, files that are composed only of ASCII text. C++ has two basic classes to handle files, ifstream and ofstream. To use them, include the header file fstream. Ifstream handles file input (reading from files), and ofstream handles file output (writing to files).

The way to declare an instance of the ifstream or ofstream class is: ifstream a_file; or ifstream a_file ( "filename" ); The constructor for both classes will actually open the file if you pass the name as an argument. The beauty of the C++ method of handling files rests in the simplicity of the actual functions used in basic input and output operations. For example: The default mode for opening a file with ofstream's constructor is to create it if it does not exist, or delete everything in it if something does exist in it. Type Casting in C and C. Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable.

(char)a will make 'a' function as a char. For example: #include <iostream> using namespace std; int main() { cout<< (char)65 <<"\n"; // The (char) is a typecast, telling the computer to interpret the 65 as a // character, not as a number. One use for typecasting for is when you want to use the ASCII characters. #include <iostream> using namespace std; int main() { for ( int x = 0; x < 128; x++ ) { cout<< x <<".

"<< (char)x <<" "; //Note the use of the int version of x to // output a number and the use of (char) to // typecast the x into a character // which outputs the ASCII character that // corresponds to the current number } cin.get(); } The typecast described above is a C-style cast, C++ supports two other types. Classes in C. C++ is a bunch of small additions to C, with a few major additions. One major addition is the object-oriented approach (the other addition is support for generic programming, which we'll cover later). As the name object-oriented programming suggests, this approach deals with objects. Of course, these are not real-life objects themselves. Instead, these objects are the essential definitions of real world objects. Classes are collections of data related to a single object type. Classes not only include information regarding the real world object, but also functions to access the data, and classes possess the ability to inherit from other classes.

(Inheritance is covered in a later lesson.) If a class is a house, then the functions will be the doors and the variables will be the items inside the house. The syntax for these classes is simple. Let's look at these different access restrictions for a moment. Classes must always contain two functions: a constructor and a destructor. Inline functions in C. Although you've already learned about basic functions in c++, there is more: the inline function.

Inline functions are not always important, but it is good to understand them. The basic idea is to save time at a cost in space. Inline functions are a lot like a placeholder. Once you define an inline function, using the 'inline' keyword, whenever you call that function the compiler will replace the function call with the actual code from the function. How does this make the program go faster? Using the inline keyword is simple, just put it before the name of a function. #include <iostream> using namespace std; inline void hello() { cout<<"hello"; } int main() { hello(); //Call it like a normal function... cin.get(); } However, once the program is compiled, the call to hello(); will be replaced by the code making up the function. Finally, note that the compiler may choose, in its infinite wisdom, to ignore your attempt to inline a function. Still not getting it? Command Line Arguments in C. In C++ it is possible to accept command line arguments.

Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system. To use command line arguments in your program, you must first understand the full declaration of the main function, which previously has accepted no arguments. In fact, main can actually accept two arguments: one argument is number of command line arguments, and the other argument is a full list of all of the command line arguments. The full declaration of main looks like this: int main ( int argc, char *argv[] ) The integer, argc is the ARGument Count (hence argc). It is the number of arguments passed into the program from the command line, including the name of the program. The array of character pointers is the listing of all the arguments. argv[0] is the name of the program, or an empty string if the name is not available. How could this be used? Linked Lists in C++ Tutorial.

Linked lists are a way to store data with structures so that the programmer can automatically create a new place to store data whenever necessary. Specifically, the programmer writes a struct or class definition that contains variables holding information about something, and then has a pointer to a struct of its type. Each of these individual struct or classes in the list is commonly known as a node. Think of it like a train. The programmer always stores the first node of the list. This would be the engine of the train. The pointer is the connector between cars of the train. In memory it is often described as looking like this: ---------- ---------- - Data - - Data - ---------- ---------- - Pointer- - - -> - Pointer- ---------- ---------- The representation isn't completely accurate, but it will suffice for our purposes.

So far we know what the node struct should look like: This so far is not very useful for doing anything. Think back to the train. Here's what that looks like: For example: Recursion in C and C. Variable Argument Lists in C and C. Tutorial: Binary Trees. C++ Inheritance. Inheritance in C++ Syntax Tutorial. Initialization Lists in C. Object-Oriented C++ Class Design - CProgramming.com. Enumerated Types - Enums in C. Formatted Output in C++ Using iomanip.