Download presentation
Presentation is loading. Please wait.
Published byἨσαῦ Φωτόπουλος Modified over 5 years ago
2
C++ Programing Primarily workshop
3
content Introduction on C++ Structure of C++ programing
General In visual studio Variables and std Random numbers Operators and math.h Conditional operators Function declaration Arrays and strings Pointers Object oriented programing Pooriya Gholipoor 1/40
4
Computer language Machine language
Only language computer directly understands Defined by hardware design Machine-dependent Generally consist of strings of numbers Ultimately 0s and 1s Instruct computers to perform elementary operations One at a time Cumbersome for humans Example: Pooriya Gholipoor 2/40
5
Computer language Assembly language
English-like abbreviations representing elementary computer operations Clearer to humans Incomprehensible to computers Translator programs (assemblers) Convert to machine language Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY Pooriya Gholipoor 3/40
6
Computer language High-level languages
Similar to everyday English, use common mathematical notations Single statements accomplish substantial tasks Assembly language requires many instructions to accomplish simple tasks Translator programs (compilers) Convert to machine language Interpreter programs Directly execute high-level language programs Example: grossPay = basePay + overTimePay Pooriya Gholipoor 4/40
7
History of C History of C Evolved from two other programming languages
BCPL and B “Typeless” languages Dennis Ritchie (Bell Laboratories) Added data typing, other features Development language of UNIX Hardware independent Portable programs 1989: ANSI standard 1990: ANSI and ISO standard published ANSI/ISO 9899: 1990 Pooriya Gholipoor 5/40
8
History of C++ History of C++ Extension of C
Early 1980s: Bjarne Stroustrup (Bell Laboratories) Provides capabilities for object-oriented programming Objects: reusable software components Model items in real world Object-oriented programs Easy to understand, correct and modify Hybrid language C-like style Object-oriented style Both Pooriya Gholipoor 6/40
9
Basics of a Typical C++ Environment
C++ systems Program-development environment Language C++ Standard Library C++ program names extensions .cpp .cxx .cc .C Pooriya Gholipoor 7/40
10
keywords Pooriya Gholipoor 8/40
11
STD Standard output stream object Namespace Escape characters
std::cout “Connected” to screen << Stream insertion operator Value to right (right operand) inserted into output stream Namespace std:: specifies using name that belongs to “namespace” std std:: removed through use of using statements Escape characters \ Indicates “special” character output Pooriya Gholipoor 9/40
12
List of std Common Input/output functions cin cout endl
Standard input stream Normally keyboard cout Standard output stream Normally computer screen endl Standard end stream Display previous messages in each line Pooriya Gholipoor 10/40
13
fig01_02.cpp (1 of 1) fig01_02.cpp output (1 of 1)
// Fig. 1.2: fig01_02.cpp // A first program in C++. #include <iostream> 4 // function main begins program execution int main() { std::cout << "Welcome to C++!\n"; 9 return 0; // indicate that program ended successfully 11 12 } // end function main Single-line comments. Function main returns an integer value. Preprocessor directive to include input/output stream header file <iostream>. Left brace { begins function body. fig01_02.cpp (1 of 1) fig01_02.cpp output (1 of 1) Function main appears exactly once in every C++ program.. Statements end with a semicolon ;. Corresponding right brace } ends function body. Name cout belongs to namespace std. Stream insertion operator. Keyword return is one of several means to exit function; value 0 indicates program terminated successfully. Welcome to C++! Pooriya Gholipoor 11/40
14
Visual Studio File > new > project > visual c++>win32 console app >next>finish Pooriya Gholipoor 12/40
15
Structure on v.s Pooriya Gholipoor 13/40
//include headers; these are modules that include functions that you may use in your //program; we will almost always need to include the header that // defines cin and cout; the header is called iostream.h #include <iostream.h> int main() { //variable declaration //read values input from user //computation and print output to user return 0; } After you write a C++ program you compile it; that is, you run a program called compiler that checks whether the program follows the C++ syntax if it finds errors, it lists them If there are no errors, it translates the C++ program into a program in machine language which you can execute Pooriya Gholipoor 13/40
16
Hello world Take a output to review the bases of C++ code
Pooriya Gholipoor 14/40
17
Variables and identifiers
Pooriya Gholipoor 15/40
18
Variable declaration Variables
Location in memory where value can be stored Common data types int - integer numbers char - characters double - floating point numbers Declare variables with name and data type before use int integer1; int integer2; int sum; Can declare several variables of same type in one declaration Comma-separated list int integer1, integer2, sum; Pooriya Gholipoor 16/40
19
fig01_06.cpp (1 of 1) Pooriya Gholipoor 17/40
// Fig. 1.6: fig01_06.cpp // Addition program. #include <iostream> 4 // function main begins program execution int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored 11 std::cout << "Enter first integer\n"; // prompt std::cin >> integer1; // read an integer 14 std::cout << "Enter second integer\n"; // prompt std::cin >> integer2; // read an integer 17 sum = integer1 + integer2; // assign result to sum 19 std::cout << "Sum is " << sum << std::endl; // print sum 21 return 0; // indicate that program ended successfully 23 24 } // end function main fig01_06.cpp (1 of 1) Declare integer variables. Use stream extraction operator with standard input stream to obtain user input. Calculations can be performed in output statements: alternative for lines 18 and 20: std::cout << "Sum is " << integer1 + integer2 << std::endl; Stream manipulator std::endl outputs a newline, then “flushes output buffer.” Concatenating, chaining or cascading stream insertion operations. Pooriya Gholipoor 17/40
20
Arithmetic Arithmetic calculations * : Multiplication / : Division
Integer division truncates remainder 7 / 5 evaluates to 1 % : Modulus operator returns remainder 7 % 5 evaluates to 2 Pooriya Gholipoor 18/40
21
if Pooriya Gholipoor 19/40
22
While Pooriya Gholipoor 20/40
23
Do while Pooriya Gholipoor 21/40
24
Switch case Pooriya Gholipoor 22/40
26
Pseudo random number We need this header: We use rand()
Min 0 Max = RAND_MAX is ; Between 0 to 10: Out put???? Pooriya Gholipoor 23/40
27
Pseudo random number We use srand to generate new random numbers:
Complete: Pooriya Gholipoor 24/40
28
enum It’s a kind of List Box which contains constant variable with numbers that are assigned them. enum name { const1, const2, …. ,constn }; Pooriya Gholipoor 25/40
29
Array, 1D and 2D int g[N][N] = { {1,1,0,1,1}, //a {1,1,0,0,0}, //b
{0,1,1,0,0}, //c {0,0,0,1,1}, //d {0,1,1,0,1 } };//e #define N 5 Pooriya Gholipoor 26/40
30
string You need Theas headers: You need Theas stds:
You can declare on this type: string name; stringstream name; Use: stringstream name << i; string name = stringstream name .str() Pooriya Gholipoor 27/40
31
function At first you declare function with your global variables
Output type + name + (argument(s))+; Then you insert codes inside the function after main code { codes } Then you can use it during your main code name(argument(s)); Pooriya Gholipoor 28/40
32
pointer What is pointer? Pooriya Gholipoor 29/40
33
OOP While Simula is credited as the first object-oriented programming language, the most popular OOP languages are: Java JavaScript Python C++ Visual Basic .NET Ruby Scala PHP Pooriya Gholipoor 31/40
34
At The end Pooriya Gholipoor 1/40
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.