C++ Programming Fundamentals, Study notes of Programming Languages

C++ Programming Fundamentals is an introductory course offered by the Arab Academy for Science, Technology and Maritime Transport (AAST). The course provides students with the basic concepts and techniques required for computer programming using the C++ language. Topics include problem-solving methods, algorithms, data types, variables, operators, input and output operations, conditional statements, loops, functions, arrays, strings, pointers, and basic object-oriented programming concepts. Students learn how to design, implement, test, and debug programs to solve computational problems efficiently. Practical programming exercises and laboratory sessions help develop coding skills and logical thinking. Emphasis is placed on writing clear, structured, and maintainable programs. The course aims to establish a strong foundation in programming and prepare students for advanced courses in software development, data structures, and computer engineering.

Typology: Study notes

2024/2025

Uploaded on 06/03/2026

Dr_Karim_Shady
Dr_Karim_Shady 🇪🇬

9 documents

1 / 48

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A Simple Program:
“Prinng a Line of Text”
Comments
o Document programs
o Improve program readability
o Ignored by compiler
o Single-line comment
Begins with //
Multiple-lines comment
Begins with /* and ends with
*/
Preprocessor directives
o Processed by preprocessor before
compiling
o Begin with #
o Preprocessor directives can be used in:
Defining used libraries
#include<iostream>
Defining macro with
parameters
#define sum(a,b) (a+b)
Defining macro constant
#define A 20
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30

Partial preview of the text

Download C++ Programming Fundamentals and more Study notes Programming Languages in PDF only on Docsity!

A Simple Program:

“PrinƟng a Line of Text”

  • Comments

o Document programs

o Improve program readability

o Ignored by compiler

o Single-line comment

  • Begins with //
  • Multiple-lines comment
    • Begins with /* and ends with */
  • Preprocessor directives

o Processed by preprocessor before

compiling

o Begin with

o Preprocessor directives can be used in:

 Defining used libraries

#include

 Defining macro with

parameters

#define sum(a,b) (a+b)

 Defining macro constant

#define A 20

  • Standard output stream object
    • 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 using namespace std
  • Escape characters
    • \
    • Indicates “special” character output
  • Input stream object
    • >> (stream extraction operator)
      • Used with std::cin
      • Waits for user to input value, then press Enter (Return) key
      • Stores value in variable to right of operator
        • Converts value to variable data type
  • = (assignment operator)
    • Assigns value to variable
    • Binary operator (two operands)
    • Example: sum = variable1 + variable2;

“Memory Concepts”

  • Variable names
    • Correspond to actual locations in computer's memory
    • Every variable has name, type, size and value
    • When new value placed into variable, overwrites previous value in memory
    • Reading variables from memory nondestructive

“Using statement”

  • Eliminate use of std:: prefix
  • Write cout instead of std::cout Without the use of using statement: std::cin>>x; With the use of using statement: using namespace std; cin>>x; cin, cout, endl can be used alone EXAMPLE 1: Write a program to prompt the user to enter his name initial, age, and height and display them on the screen Inputs: name iniƟal, age, height Outputs: name iniƟal, age, height Formula: None Constants: None #include using namespace std; int main() { char name_I; int age; float height; cout<<“Please enter your name iniƟal, age and height \n”; cin>>name_I>>age>>height; cout<<“name is”< #include using namespace std; int main() { const int one_tree_space = 4; int length, width, area, no_of_trees; cout<<“Enter the length of the land \n”; cin>>length; cout<<“Enter the width of the land \n”; cin>>width; area = Length*width; no_of_trees= area/one_tree_space; cout<<“The available number of trees = “< using namespace std; int main() { const float PI = 3.141593; float radius, area, circum; cout<<“Enter radius of the circle \n”; cin>>radius; area = PI * radius * radius; circum = 2 * PI * radius; cout<<“The area of the circle = “<

EXAMPLE 4:

Write a program to define two integer variables containing 3 and 5, then calculate their sum and their product and display the result. You must use comments. Inputs: None Outputs: sum, product Formula: sum = first + second Product = first * second Constants: first=3, second = // This program calculates the sum and product of 3 and 5 #include using namespace std; int main() { int first, second, sum, product; //declaring variables first = 3; second = 5; sum = first + second ; //calculaƟng sum product = first * second; //calculaƟng product cout<<“The sum is “<< sum<<“ \n”; cout<<“The product is “<< product<<“ \n”; return 0 ; }

#include using namespace std; int main() { int No1, No2, Temp; cout<<"Enter two numbers\n"; cin>>No1>>No2; cout<<"Before Swapping\n"; cout<<"1st Number is:"< “ArithmeƟc expressions”

There are many operators for manipulaƟng numeric values

and performing arithmeƟc operaƟons

We have

Integer arithmeƟc operaƟons

float arithmeƟc operaƟons

Integer

results

float

results

2. Arithmetic Expression

 An expression is a valid arrangement of variables, constants, and operators.  C++ applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those in algebra:

Operators Precedence

( )

3. Shortcut assignment

4. Mathematical Library Functions

 The cmath library provides a lot of useful predefined math

functions.

 Before you use them, remember to include the math

library in your code:

#include

R1 = (-b + sqrt(pow(b,2) – 4 * a * c)) / (2 * a);

R2 = (-b - sqrt(pow(b,2) – 4 * a * c)) / (2 * a);

R1=

a

b b ac

a

b b ac

2

R2=

Inputs: a, b, c Outputs: R1, R Formula: d = pow(b,2) – 4 * a * c; R1 = (-b + sqrt(d)) / (2 * a); R2 = (-b - sqrt(d)) / (2 * a); Constants: None

#include #include int main() { using namespace std; double a,b,c,d,x1,x2; cout<<"Enter the equaƟon coefficients:"; cin>>a>>b>>c; d=(bb-4ac); if (d<0) cout<<"The two roots are imaginary"; else { d=sqrt(d); x1=(-b+d)/(2a); x2=(-b-d)/(2*a); cout<<"The two roots are"<<"\n"; cout< Making Decisions (selecƟon) - II SWITCH Statement

⮚ Used to select one of several alternatives

⮚ BASED on the value of a single variable.

⮚ This variable may be an int or a char but NOT a float

or double.

Switch (variable) { case constant_1: acƟon_1; break; case constant_2: acƟon_2; break; case constant_3: acƟon_3; break; default: acƟon_4; }

SYNTAX

break is very important. WHY?

Write a C++ program that accept a leƩer A, B, or C and print

the entered leƩer. You must use SWITCH