Cpp tutorial - Bshshs PDF

Title Cpp tutorial - Bshshs
Author Anonymous User
Course Strategic hrm
Institution Anna University
Pages 54
File Size 2.7 MB
File Type PDF
Total Downloads 89
Total Views 137

Summary

Bshshs...


Description

C++

About the Tutorial C++ is a middle-level programming language developed by Bjarne Stroustrup in 1979 at Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This tutorial adopts a simple and practical approach to describe the concepts of C++.

Audience This tutorial has been prepared for the beginners to help them understand the basic to advanced concepts related to C++.

Prerequisites Before you begin practicing various examples given in this tutorial, we are making an assumption that you are already aware of some basics, like the computer software installation process and computer programming language.

Copyright & Disclaimer  Copyright 2014 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected]

i

C++

Table of Contents About the Tutorial ............................................................................................................................................ i Audience........................................................................................................................................................... i Prerequisites .....................................................................................................................................................i Copyright & Disclaimer ..................................................................................................................................... i

1. O OVE VE VERVI RVI RVIEW EW .............................................................................................................................. 1 Object-Oriented Programming ........................................................................................................................ 1 Standard Libraries............................................................................................................................................ 1 The ANSI Standard ........................................................................................................................................... 1 Learning C++ .................................................................................................................................................... 2

2. EENVIRO NVIRO NVIRONM NM NMEN EN ENTT SET SETUP UP ............................................................................................................ 3 Try it Option Online ......................................................................................................................................... 3

3. B BASI ASI ASIC C SY SYNTAX NTAX ................................ ......................................................................................................................... ......................................................................................... 5 C++ Program Structure..................................................................................................................................... 5 Compile & Execute C++ Program ...................................................................................................................... 6 Semicolons & Blocks in C++.............................................................................................................................. 6 C++ Keywords .................................................................................................................................................. 7

4. C COM OM OMMENT MENT MENTSS IN C C++ ++ ................................................................ ............................................................................................................... ............................................... 10 5. D DATA ATA TY TYPES PES ........................................................................................................................... 11 Primitive Built-in Types .................................................................................................................................. 11 typedef Declarations .....................................................................................................................................13 Enumerated Types ......................................................................................................................................... 14

6. V VARI ARI ARIABLE ABLE TYP TYPES ES .................................................................................................................... 15 ii

C++

Variable Definition in C++ .............................................................................................................................. 15 Variable Declaration in C++ ............................................................................................................................ 16 Lvalues and Rvalues ....................................................................................................................................... 18

7. V VARI ARI ARIABLE ABLE SCO SCOPE PE ................................................................................................................... 19 Local Variables ............................................................................................................................................... 19 Global Variables .............................................................................................................................................20 Initializing Local and Global Variables............................................................................................................ 21

8. C CON ON ONSTA STA STANTS/LI NTS/LI NTS/LITERAL TERAL TERALSS ........................................................................................................... 22 Integer Literals ............................................................................................................................................... 22 Floating-point Literals .................................................................................................................................... 22 Boolean Literals ............................................................................................................................................. 23 Character Literals ........................................................................................................................................... 23 String Literals ................................................................................................................................................. 24 Defining Constants ......................................................................................................................................... 25

9. M MODIFIE ODIFIE ODIFIER R TYPE TYPESS ................................................................................................................... ...................................................................................................................27 27 Type Qualifiers in C++ .................................................................................................................................... 28

10. STO STORAGE RAGE CL CLASSE ASSE ASSESS ............................................................................................................... 29 The auto Storage Class ................................................................................................................................... 29 The register Storage Class .............................................................................................................................. 29 The static Storage Class ................................................................................................................................. 29 The extern Storage Class ................................................................................................................................ 31 The mutable Storage Class ............................................................................................................................. 32

11. OPE OPERATO RATO RATORS RS......................................................................................................................... 33 Arithmetic Operators ..................................................................................................................................... 33 Relational Operators ......................................................................................................................................35 Logical Operators ........................................................................................................................................... 37 iii

C++

Bitwise Operators .......................................................................................................................................... 39 Assignment Operators ................................................................................................................................... 41 Misc Operators .............................................................................................................................................. 44 Operators Precedence in C++ ......................................................................................................................... 45

12. LO LOOP OP TYP TYPES ES......................................................................................................................... 48 While Loop ....................................................................................................................................................49 Loop Control Statements ............................................................................................................................... 57 The Infinite Loop ............................................................................................................................................ 63

13. DEC DECISI ISI ISION-M ON-M ON-MAKING AKING ST STATEME ATEME ATEMENTS NTS ........................................................................................ 65 If Statement ................................................................................................................................................... 66 if…else Statement .......................................................................................................................................... 67 if...else if...else Statement ............................................................................................................................. 69 Switch Statement ..........................................................................................................................................71 Nested if Statement ....................................................................................................................................... 73 The ? : Operator ............................................................................................................................................. 76

14. FUN FUNCTION CTION CTIONSS ......................................................................................................................... 77 Defining a Function ........................................................................................................................................ 77 Function Declarations .................................................................................................................................... 78 Calling a Function ..........................................................................................................................................78 Function Arguments ......................................................................................................................................80

15. NUMBERS NUMBERS ........................................................................................................................... ...........................................................................................................................87 87 Defining Numbers in C++ ...............................................................................................................................87 Math Operations in C++ ................................................................................................................................. 88 Random Numbers in C++ ...............................................................................................................................90

16. ARR ARRAYS AYS ............................................................................................................................... ...............................................................................................................................92 92 Declaring Arrays ............................................................................................................................................92 iv

C++

Initializing Arrays ........................................................................................................................................... 92 Accessing Array Elements .............................................................................................................................. 93 Arrays in C++ .................................................................................................................................................. 94 Passing Arrays to Functions ...........................................................................................................................98

17. STRI STRINGS NGS ................................ ............................................................................................................................ ............................................................................................ 104 The C-Style Character String ........................................................................................................................104 The String Class in C++ ................................................................................................................................. 106

18. POI POINTERS NTERS .......................................................................................................................... 108 What are Pointers? ...................................................................................................................................... 108 Using Pointers in C++ ................................................................................................................................... 109 Pointers in C++ ............................................................................................................................................. 110 Null Pointers ................................................................................................................................................ 111 Pointer Arithmetic ....................................................................................................................................... 111 Pointers vs Arrays ........................................................................................................................................ 115 Array of Pointers .......................................................................................................................................... 117 Pointer to a Pointer ..................................................................................................................................... 119 Passing Pointers to Functions ......................................................................................................................120 Return Pointer from Functions ....................................................................................................................123

19. RE REFE FE FEREN REN RENCES CES...................................................................................................................... 126 References vs Pointers ................................................................................................................................. 126 Creating References in C++ ..........................................................................................................................126 References as Parameters............................................................................................................................ 127 Reference as Return Value........................................................................................................................... 129

20. DA DATE TE A AND ND TI TIME ME ................................................................................................................ 131 Current Date and Time ................................................................................................................................132 Format Time using struct tm ........................................................................................................................ 133

v

C++

21. BA BASIC SIC IINPUT/ NPUT/ NPUT/OUTP OUTP OUTPUT UT ....................................................................................................... 135 I/O Library Header Files ............................................................................................................................... 135 The Standard Output Stream (cout) ............................................................................................................. 135 The Standard Input Stream (cin) .................................................................................................................. 136 The Standard Error Stream (cerr) ................................................................................................................. 137 The Standard Log Stream (clog) ...................................................................................................................137

22. DA DATA TA STR STRUCTU UCTU UCTURE RE RESS............................................................................................................ 139 Defining a Structure ..................................................................................................................................... 139 Accessing Structure Members ...................................................................................................................... 140 Structures as Function Arguments ............................................................................................................... 141 Pointers to Structures .................................................................................................................................. 143

23. CLA CLASSES SSES AND OB OBJECT JECT JECTSS ...................................................................................................... 146 C++ Class Definitions ....................................................


Similar Free PDFs