Docsity

C++ Programming Fundamentals, Study notes of Programming Languages

Toggle sidebar
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