C++ Introduction to basic C++ IN THIS YOU WOULD KHOW ABOUT BASIC C++

sanatahiratoz0to9 9 views 55 slides Feb 27, 2025
Slide 1
Slide 1 of 55
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55

About This Presentation

it is slide on basic introduction of c++


Slide Content

Programming Fundamentals Course Instructor: Dr. Romana Farhan Lecture # 1

Course Learning outcomes S. No Course Learning Outcome (CLO’s) Bloom’s Learning Level 1 Learn skills to propose solutions for real life problems in terms of computer programs. C2 2 Analyze programs with standard notation/ methods to identify their structures. C3 3 Develop programs using high level programming language making use of modular as well as object-oriented design approach. C3

Evaluation Criteria Evaluation Criteria Percentage Quiz (3-4) 15% Assignment (2-3) 10% Mid 25% Final 50%

CONTENTS

Programming is like Legos Small number of primitive constructs!

You can build huge structures using these pieces .

Computer Languages

CONTD… Any high-level language program must be translated into machine language before the computer can understand and follow the program

Compiler A program that translates a high-level language like C++ to a machine language that the computer can directly understand and execute. The input to the compiler is a program in high level language ( source program or source code) and the translated version produced by the compiler is called the object program or object code.

CONTD…

The magic of Compiler

Assembler The Assembler is a Software that converts an assembly language code to machine code. We can also name an assembler as the compiler of assembly language. An assembler works by assembling and converting the source code of assembly language into object code or an object file that constitutes a stream of zeros and ones of machine code, which are directly executable by the processor.

CONTD…

Linker The object code for your C++ program must be combined with the object code for routines (such as input and output routines) that your program uses. This process of combining object code is called linking and is done by a program called a linker.

Linker

Algorithms A sequence of precise instructions which leads to a solution is called an algorithm. To qualify as an algorithm, a set of instructions must completely and unambiguously specify the steps to be taken and the order in which they are taken.

Example: Sum of two numbers

Flow chart a graphical representation of a computer program in relation to its sequence of functions (as distinct from the data it processes).

Flow Chart Symbols

Example: Sum of two numbers

Program design process

Testing and Debugging Mistake in a program is usually called a bug, and the process of eliminating bugs is called debugging.

Syntax error Violation of the syntax (that is, the grammar rules) of the programming language, such as omitting a semicolon.

Example He eats lunch the… I going am school to…

Logical error The program will run perfectly but it will not behave in desired manner. It is most difficult to spot logical errors. Can only be resolved by careful testing.

Example If you intend to write: “A dog bit a man…” BUT mistakenly end up with “A man bit a dog…”

Runtime Error There are certain kinds of errors that the computer system can detect only when a program is run, these are called run-time errors.

Example If the computer attempts to divide a number by zero, that is normally a run-time error.

Brainstorming Time Why study C++ language?? What are latest job trends in C++?? What are major applications of C++ in the field of computing ?? …. …..

Variables, Data types and Input/output constructs

Outline

Layout of a simple C++ program

Sample Program // This is a simple C++ program. #include < iostream > using namespace std; int main () { cout<<“Welcome to Computer Programming course”; return 0; }

CONTD… #include<iostream> A program includes various programming elements that are already defined in the standard C++ library. In order to use such pre-defined elements in a program, an appropriate header/directives must be included in the program. Directives always begin with the symbol #. <iostream> is the name of a library that contains the definitions of the routines that handle input from the keyboard and output to the screen. Do not include extra space between the < and the iostream file name or between the end of the file name and the closing >.

using namespace std; This line says that the names defined in iostream are to be interpreted in the “standard way”. int main() It tells that your main part of a program starts here. { } Braces mark beginning and end of the main function. return 0; The last line in the program. It marks end of the program.

Variables Variable is the basic storage unit in a program. It is a name given to a memory location . The compiler assigns a memory location to each variable name in the program. The value of the variable, in a coded form, is kept in the memory location assigned to that variable. We do not know what addresses the compiler will choose for the variables in our program. Data held in a variable is called its value or a literal; Number/data held by a C++ variable can be changed. A C++ variable is guaranteed to have some value in it, if only a garbage number left in the computer’s memory by some previously run program.

Names: Identifiers Identifiers are used as names for variables and other items in a C++ program. To make your program easy to understand, you should always use meaningful names for variables. Rules for naming variables: You cannot use a C++ keyword (reserved word) as a variable name. Variable names in C++ can range from 1 to 255 characters. All variable names must begin with a letter of the alphabet (a-z, A-Z) or an underscore( _ ).

CONTD… After the first initial letter, variable names can also contain letters and numbers. No spaces or special characters allowed. C++ is case Sensitive. Uppercase characters are distinct from lowercase characters. Examples: A, a_1, x123 (legal) 1ab, da%, 1-2, (not acceptable) Test, test, TEST (case-sensitive)

Variable declarations Every variable in a C++ program must be declared before the variable can be used. When you declare a variable, you are telling the compiler—and, ultimately, the computer—what kind of data you will be storing in the variable, and what size of memory location to use for the variable. Each declaration ends with a semicolon (;) . When there is more than one variable in a declaration, the variables are separated by commas. The kind of data that is held in a variable is called its type and the name for the type, such as int or double , is called a type name.

Syntax The syntax for a programming languages is the set of grammar rules for that language. The syntax for variable declarations is as follows: Syntax Type_Name Var_Name_1 , Var_Name_2 , ...; Examples int count, sum, number_of_person ; double distance;

Assignment statements In an assignment statement, first the expression on the right-hand side of the equal sign is evaluated, and then the variable on the left-hand side of the equal sign is set equal to this value. In an assignment statement, the expression on the right-hand side of the equal sign can simply be another variable or a constant. Syntax Variable = Expression ; Examples sum=a; //variable distance = rate * time; //expression count=12; //constant

Uninitialized variables Variable that has not been given a value is said to be uninitialized. One way to avoid an uninitialized variable is to initialize variables at the same time they are declared. You can initialize some, all, or none of the variables in a declaration that lists more than one variable. Examples: int count=0; double avg=99.9; int a=10, b, c=0;

C++ Keywords/Reserved words

Output using cout The values of variables as well as strings of text may be output to the screen using cout . The arrow notation << is often called the insertion operator. You can simply list all the items to be output preceding each item to be output with the arrow symbols << . Strings must be included in double quotes. Examples: cout <<“This is our first c++ program”; cout <<“The sum is”<<sum; cout <<“distance is”<<(time * speed);

Input using cin A cin statement sets variables equal to values typed in at the keyboard. cin  is a predefined variable that reads data from the keyboard with the extraction operator (>>). Syntax cin >> Variable_1 >> Variable_2 >> ... ; Example cin >> number >> size; cin >> time;

Data Types Data types are used to tell the variables the type of data it can store.  Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-type with which it is declared. Every data type requires a different amount of memory.

Integer types The integer data type basically represents whole numbers (no fractional parts). The reason is threefold. First, some things in the real world are not fractional. Second, the integer data type is often used to control program flow by counting. Third, integer processing is significantly faster within the CPU than is floating point processing.

CONTD…

Floating point types The floating-point family of data types represents number values with fractional parts. They are technically stored as two integer values: a  mantissa  and an  exponent . They are always signed. A floating_point number can also be a scientific number with an "e" to indicate the power of 10:

Type char Values of the type char are single symbols such as a letter, digit, or punctuation mark. A variable of type char can hold any single character on the keyboard e.g., ’A ' or '+' or an 'a’ . Note that uppercase and lowercase versions of a letter are considered different characters. The text in double quotes that are output using cout are called string values. Be sure to notice that string constants are placed inside of double quotes, while constants of type char are placed inside of single quotes.

Type bool Expressions of type bool are called Boolean after the English mathematician George Boole, who formulated rules for mathematical logic. Boolean expressions evaluate to one of the two values, true or false . Boolean expressions are used in branching and looping statements.

Comment In C++ the symbols // are used to indicate the start of a comment. All of the text between the // and the end of the line is a comment. The compiler simply ignores anything that follows // on a line. Anything between the symbol pair /* and the symbol pair */ is considered a comment and is ignored by the compiler. Unlike the // comments, /* to */ comments can span several lines,

THE END