Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Reference: Data Types, Operators, I/O, Statements, Looping, Functions, Pointers, Memor, Lecture notes of Printing

Data Structures and AlgorithmsSystems ProgrammingObject-Oriented ProgrammingSoftware Engineering

A comprehensive reference for c++ programming language. It covers various data types, operators, console input and output, file input and output, decision statements, looping, functions, pointers, dynamic memory, structures, classes, inheritance, operator overloading, exceptions, function templates, and class templates. It includes examples and explanations for each concept.

What you will learn

  • How do you use pointers in C++?
  • How do you overload operators in C++?
  • What is the difference between a struct and a class in C++?
  • How do you read and write files in C++?
  • What are exceptions in C++ and how do you use them?
  • What are function templates and how do you use them in C++?

Typology: Lecture notes

2021/2022

Uploaded on 07/05/2022

lee_95
lee_95 🇦🇺

4.6

(59)

1K documents

1 / 2

Toggle sidebar

Related documents


Partial preview of the text

Download C++ Reference: Data Types, Operators, I/O, Statements, Looping, Functions, Pointers, Memor and more Lecture notes Printing in PDF only on Docsity! C++ Reference Card C++ Data Types Data Type Description bool boolean (true or false) char character ('a', 'b', etc.) char[] character array (C-style string if null terminated) string C++ string (from the STL) int integer (1, 2, -1, 1000, etc.) long int long integer float single precision floating point double double precision floating point These are the most commonly used types; this is not a complete list. Operators The most commonly used operators in order of precedence: 1 ++ (post-increment), -- (post-decrement) 2 ! (not), ++ (pre-increment), -- (pre-decrement) 3 *, /, % (modulus) 4 +, - 5 <, <=, >, >= 6 == (equal-to), != (not-equal-to) 7 && (and) 8 || (or) 9 = (assignment), *=, /=, %=, +=, -= Console Input/Output cout << console out, printing to screen cin >> console in, reading from keyboard cerr << console error Example: cout << "Enter an integer: "; cin >> i; cout << "Input: " << i << endl; File Input/Output Example (input): ifstream inputFile; inputFile.open("data.txt"); inputFile >> inputVariable; // you can also use get (char) or // getline (entire line) in addition to >> ... inputFile.close(); Example (output): ofstream outFile; outfile.open("output.txt"); outFile << outputVariable; ... outFile.close(); Decision Statements if Example if (expression) if (x < y) statement; cout << x; if / else Example if (expression) if (x < y) statement; cout << x; else else statement; cout << y; switch / case Example switch(int expression) switch(choice) { { case int-constant: case 0: statement(s); cout << "Zero"; break; break; case int-constant: case 1: statement(s); cout << "One"; break; break; default: default: statement; cout << "What?"; } } Looping while Loop Example while (expression) while (x < 100) statement; cout << x++ << endl; while (expression) while (x < 100) { { statement; cout << x << endl; statement; x++; } } do-while Loop Example do do statement; cout << x++ << endl; while (expression); while (x < 100); do do { { statement; cout << x << endl; statement; x++; } } while (expression); while (x < 100); for Loop for (initialization; test; update) statement; for (initialization; test; update) { statement; statement; } Example for (count = 0; count < 10; count++) { cout << "count equals: "; cout << count << endl; } Functions Functions return at most one value. A function that does not return a value has a return type of void. Values needed by a function are called parameters. return_type function(type p1, type p2, ...) { statement; statement; ... } Examples int timesTwo(int v) { int d; d = v * 2; return d; } void printCourseNumber() { cout << "CSE1284" << endl; return; } Passing Parameters by Value return_type function(type p1) Variable is passed into the function but changes to p1 are not passed back. Passing Parameters by Reference return_type function(type &p1) Variable is passed into the function and changes to p1 are passed back. Default Parameter Values return_type function(type p1=val) val is used as the value of p1 if the function is called without a parameter. Pointers A pointer variable (or just pointer) is a variable that stores a memory address. Pointers allow the indirect manipulation of data stored in memory. Pointers are declared using *. To set a pointer's value to the address of another variable, use the & operator. Example char c = 'a'; char* cPtr; cPtr = &c; Use the indirection operator (*) to access or change the value that the pointer references. Example // continued from example above *cPtr = 'b'; cout << *cPtr << endl; // prints the char b cout << c << endl; // prints the char b Array names can be used as constant pointers, and pointers can be used as array names. Example int numbers[]={10, 20, 30, 40, 50}; int* numPtr = numbers; cout << numbers[0] << endl; // prints 10 cout << *numPtr << endl; // prints 10 cout << numbers[1] << endl; // prints 20 cout << *(numPtr + 1) << endl; // prints 20 cout << numPtr[2] << endl; // prints 30 Dynamic Memory Allocate Memory Examples ptr = new type; int* iPtr; iPtr = new int; ptr = new type[size]; int* intArray; intArray = new int[5]; Deallocate Memory Examples delete ptr; delete iPtr; delete [] ptr; delete [] intArray; Once a pointer is used to allocate the memory for an array, array notation can be used to access the array locations. Example int* intArray; intArray = new int[5]; intArray[0] = 23; intArray[1] = 32; Structures Declaration Example struct name struct Hamburger { { type1 element1; int patties; type2 element2; bool cheese; }; }; Definition Example name varName; Hamburger h; name* ptrName; Hamburger* hPtr; hPtr = &h; Accessing Members Example varName.element=val; h.patties = 2; h.cheese = true; ptrName->element=val; hPtr->patties = 1; hPtr->cheese = false; Structures can be used just like the built-in data types in arrays.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved