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++!

C++ Tutorial - Introduction to 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. 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.

If Statements in C++ - Cprogramming.com Tutorial

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. If statements allow the flow of the program to be changed, and so they allow algorithms and more interesting code. For, While, and Do While Loops in C. Loops are used to repeat a block of code.

For, While, and Do While Loops in C

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. FOR - for loops are the most useful type. Example: WHILE - WHILE loops are very simple. Still not getting it? Functions in C and C. Now that you should have learned about variables, loops, and conditional statements it is time to learn about functions.

Functions in C and C

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: #include <cstdlib> // Include rand() using namespace std; // Make rand() visible int a = rand(); // rand is a standard function that all compilers have. 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).

Switch Case in C and C

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.

Tutorial: Pointers. Pointers are an extremely powerful programming tool.

Tutorial: Pointers

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.

Tutorial: Structures. Before discussing classes, this lesson will be an introduction to data structures similar to classes.

Tutorial: Structures

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. 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.

Arrays in C and C

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: C Strings in C and C. In C++ there are two types of strings, C-style strings, and C++-style strings.

C Strings in C and C

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. 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.

C++ File I/O Tutorial

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). 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. 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. 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.

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). 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. Recursion in C and C. Variable Argument Lists in C and C. Tutorial: Binary Trees. The binary tree is a fundamental data structure used in computer science. The binary tree is a useful data structure for rapidly storing sorted data and rapidly retrieving stored data. C++ Inheritance. Inheritance in C++ Syntax Tutorial. Before beginning this lesson, you should have an understanding of the idea of inheritance. If you do not, please read lesson 19. This lesson will consist of an overview of the syntax of inheritance, the use of the keywords public, private, and protected, and then an example program following to demonstrate each. The syntax to denote one class as inheriting from another is simple.

It looks like the following: class Bear : public Animal, in place of simply the keyword class and then the class name. Initialization Lists in C. Understanding the Start of an Object's Lifetime. Object-Oriented C++ Class Design - CProgramming.com. Understanding Interfaces. Enumerated Types - Enums in C. Formatted Output in C++ Using iomanip. Creating cleanly formatted output is a common programming requirement--it improves your user interface and makes it easier to read any debugging messages that you might print to the screen.