C++tutorial.pdf
作者:Juan Soulié, Last revision: June, 2007This tutorial is for those people who want to learn programming in C++ and do not necessarily have any previousknowledge of other programming languages.The C++ Language tutorialTable of contentsTable of contents…Introduction∴,,…Instructions for useBasics of c++.…35577Structure of a program....Variables Data Types11Constants.17Operators.21Basic Input/Output.....29Control structures……34Control structures…34Functions(0)41Functions()…47Compound data types…,,,,…54··::····::::::::·:54Character Sequences…60Pointers63Dynamic Memory................................Data structures77Other data Types…………………………….82object Oriented Programming………,…,…,,…,,…,,…,……,,……,…,,…,……86C| asses().……·..:·:::.::·:.:::::::.:.:::.::·.::.::.:a:.86Classes(l):.:::·:::a::::::..·.a.::::::.:·::::aa:.:a:::::95Friendship and inheritance ...................a..·..:..::a:::::·a.:aa::::::a::.·:·:a.:::·100Polymorphism…,音自着。自。着音·。,,自着自自。自自,,。着非自,非音非自∴107Advanced concepts………113Templates...113Namespaces....,,…,,…,…,…,….,…,,……,,120EXceptions……123Type Casting…………,…,∴127C cplusplus. com 2008. All rights reseryThe C++ Language tutorialPreprocessor directives133c++ Standard Library………138Input/Output with files ...........138The C++ Language tutorialIntroductionInstructions for useTo whom is this tutorial directed?This tutorial is for those people who want to learn programming in C++ and do not necessarily have any previousknowledge of other programming languages. Of course any knowledge of other programming languages or anygeneral computer skill can be useful to better understand this tutorial although it is not essentialIt is also suitable for those who need a little update on the new features the language has acquired from the lateststandardsIf you are familiar with the C language, you can take the first 3 parts of this tutorial as a review of concepts, sincethey mainly explain the C part of C++. there are slight differences in the C++ syntax for some C features, so Irecommend you its reading anywayThe 4th part describes object-oriented programmingThe 5th part mostly describes the new features introduced by aNsi-C++ standardStructure of this tutorialThe tutorial is divided in 6 parts and each part is divided on its turn into different sections covering a topic eachone. You can access any section directly from the section index available on the left side bar or begin the tutorialfrom any point and follow the links at the bottom of each sectionMany sections include examples that describe the use of the newly acquired knowledge in the chapter. It isrecommended to read these examples and to be able to understand each of the code lines that constitute it beforepassing to the next chapter.A good way to gain experience with a programming language is by modifying and adding new functionalities onyour own to the example programs that you fully understand. Don't be scared to modify the examples providedwith this tutorial, that's the way to learnCompatibility notesThe ANSI-C++ standard acceptation as an international standard is relatively recent. It was first published inNovember 1997, and revised in 2003. Nevertheless, the C++ language exists from a long time before(1980s)Therefore there are many compilers which do not support all the new capabilities included in ANSI-C++, especiallythose released prior to the publication of the standardThis tutorial is thought to be followed with modern compilers that support -at least on some degree- ANSI-C++specifications. I encourage you to get one if yours is not adapted. There are many options, both commercial andFreeCompilersThe examples included in this tutorial are all console programs. That means they use text to communicate withthe user and to show their resultsThe C++ Language tutorialAll C++ compilers support the compilation of console programs. Check the user's manual of your compiler for morenfo on how to compile themThe C++ Language tutorialBasics of ct+Structure of a programProbably the best way to start learning a programming language is by writing a program. therefore, here is ourfirst program7 my first progran in C++Hello World!#include using namespace stdiint main (cout < " Hello World!"ireturn 0iThe first panel shows the source code for our first program. The second one shows the result of the program oncecompiled and executed. The way to edit and compile a program depends on the compiler you are using. Dependingon whether it has a Development interface or not and on its version Consult the compilers section and the manualor help included with your compiler if you have doubts on how to compile a C++ console programThe previous program is the typical program that programmer apprentices write for the first time, and its result isthe printing on screen of the Hello World! sentence. It is one of the simplest programs that can be written inC++, but it already contains the fundamental components that every C++ program has. We are going to look liney line at the code we have just written// my first program in C++This is a comment line. All lines beginning with two slash signs(//)are considered comments and do nothave any effect on the behavior of the program the programmer can use them to include shortexplanations or observations within the source code itself. In this case, the line is a brief description ofwhat our program isInclude ines beginning with a hash sign(#)are directives for the preprocessor. They are not regular code lineswith expressions but indications for the compilers preprocessor. In this case the directive #includeusing namespace std;int main (cout < Hello world!cout using name space std;int maincout <
下载地址
用户评论