0% found this document useful (0 votes)
18 views21 pages

C++ Programming Fundamentals

Uploaded by

werkezebo5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views21 pages

C++ Programming Fundamentals

Uploaded by

werkezebo5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CHAPTER ONE

Basics of C++

November 6, 2023
Outline

 Introduction

 Structure of a Program

 Constants

 Operators

 Basic input / Output


2
Introduction
• Computer requires programs to function, and a computer
programs does nothing unless its instructions are executed by
a CPU.
• Computer programming shortened to programming or coding
is the process of writing, testing, debugging/troubleshooting,
and maintaining the source code of computer programs.
• Writing computer programs means writing instructions that
will make the computer follow and run a program based on the
instructions.
• computer programs=software programs=programs -instructions
that tells the computer what to do.
3
…continued
• A computer program(source code) written by professionals known
as Computer Programmers (programmers).
• Source code is written in one of programming languages. A
programming language is an artificial language that can be used to
control the behavior of a machine, particularly a computer.
• Programming languages are defined by syntactic and semantic
rules which describe their structure and meaning respectively.
• The syntax of a language describes the possible combinations of symbols
that form a syntactically correct program.
• Semantics handled meaning of a given combined symbols.
4
…continued
• Program Development Lifecycle
• Feasibility Study, Requirement analysis, Designing solution, Testing,
Implementation, Integration and system testing, Maintenance
• An algorithm is defined as a step-by-step sequence of
instructions that must terminate and describe how the data is
to be processed to produce the desired outputs.
• Tools to describe algorithm: flowcharts, Structure chart,
Pseudo code.

5
Structure of a Program
• Documentation

• Link Section

• Definition Section

• Global Declaration Section

• Function Definition Section

• Main Function
6
Structure of a Program
// Documentation Section
// Global Declaration Section
/* This is a C++ program to find the factorial of a
int num = 0, fact = 1, storeFactorial = 0;
number
The basic requirement for writing this program is to
// Function Section
have knowledge of loops
int factorial(int num)
To find the factorial of a number iterate over the
{
range from number to 1
// Iterate over the loop from
*/
// num to one
for (INTEGER i = 1; i <= num; i++) {
// Linking Section
fact *= i;
#include <iostream>
}
using namespace std;
// Return the factorial
return fact;
// Definition Section
}
#define msg "FACTORIAL\n"
typedef int INTEGER; 7
Structure of a Program
// Main Function
int main()
{
// Given number Num
int Num = 5;

// Function Call
storeFactorial = factorial(Num);
cout << msg;

// Print the factorial


cout << Num << "! = "<< storeFactorial << endl;
return 0;
}

8
…Continued

9
Constants
• Variables -name given to a memory location. It is the basic unit of
storage in a program.
• The value stored in a variable can be changed during program execution.

• A variable is only a name given to a memory location, all the operations


done on the variable effects that memory location.
• All variables must be declared before use(Single and Multiple
declaration)
• Have Data type, Variable name, value 10
…continued
• Variable name can be start with letters and underscore(not number)
• Contain number, letters, underscore
• Can not be keywords
• Declaration
• Definition = Declaration + initialization
• Reserved/Key words have a unique meaning within a C++
program. These symbols, the reserved words, must not be used
for any other purposes.
• All reserved words are in lowercase letters. (float,if,do…)
11
…Continued
• Constants - read-only variables whose values cannot be
modified once they are declared
• Using the keyword const

• Const float gravity = 9.8

• Using #define preprocessor (#define pi 3.14)

12
Operators
• An operator is a symbol that operates on a value to perform
specific mathematical or logical computations.
• An operator operates the operands. For example,
• int c = a + b; ‘+’ is the addition operator. ‘a’ and ‘b’ are the operands
that are being ‘added’.
• Operators in C++
• Arithmetic Operator(+, -, *, /, %,++,--)
• Assignment operator (=, +=,-=,*=, /=)

13
…Continued
• Relational Operator( comparsion)-
<,>,==, >= , <=
• Logical Operator(combine 2 or more
condition/constraint) - &&,||,!
 Increment/Decrement Operator
• Prefix(++Age) and postfix(Age++)
 Operator Precedence
14
 int a,b,c,d, a=b+c*d (* have priority)
Basic input / Output
• C++ have libraries that provide many ways for performing
input and output. In C++ input and output are performed in
the form of a sequence of bytes or more commonly known
as streams.
• Input Stream: If the direction of flow of bytes is from the
device(Keyboard) to the main memory then this process is
called input.
• Output Stream: If the direction of flow of bytes is opposite,
i.e. from main memory to device( display screen ) then this
process is called output.
15
…Continued
• Header files available in C++ for Input/Output operations are:
• iostream: iostream stands for standard input-output stream. This header
file contains definitions of objects like cin, cout, cerr….
• iomanip: iomanip stands for input-output manipulators. The methods
declared in these files are used for manipulating streams. This file contains
definitions of setw, setprecision, etc.
• fstream: This header file mainly describes the file stream. This header file
is used to handle the data being read from a file as input or data being
written into the file as output. 16
…Continued
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<“Enter your age?”; //insertion operator
cin>>age; //extraction operator
cout<<“Your age is: ”<<age;
return 0;
}
17
Exercises
1. int a=5, y,
y=++a + 10;
y= a++ + 10;
y= --a + 10;
y= a-- + 10;
2. Write C++ program that display the sum of two numbers(user
have to enter those two numbers)
3. Write program that compare two number.

18
Exercises
#include <iostream>
cout << mark << endl;
using namespace std;
int main() mark--;
{ cout << mark << endl;
int mark = 76;
--mark;
cout << mark << endl;
mark++; cout << mark << endl;
cout << mark << endl; mark = mark - 1;
++mark;
cout << mark << endl;
cout << mark << endl;
return 0;
mark = mark + 1;
}
19
Thank you
Questions?

20
Next
Chapter 2
Control Structure
21

Common questions

Powered by AI

Header files in C++ such as iostream, iomanip, and fstream are crucial for input and output operations. 'iostream' includes standard input-output stream objects like 'cin' and 'cout', enabling basic console-based input and output . 'iomanip' is used for manipulators that allow modifying the format of input-output operations, while 'fstream' is used for file stream operations, enabling the reading from and writing to files . These header files provide the necessary definitions and tools to handle data flow efficiently between devices and memory, and they encapsulate complex operations within simple objects and functions, aiding in cleaner and more maintainable code.

In C++, it is essential to declare variables before use to allocate the necessary memory space and define the type of data that the variable will store . A variable declaration conveys several important pieces of information: the data type, the variable name, and optionally the initial value. This information allows the compiler to understand and manage how the variable will be used in the program, ensuring type safety and efficient memory use . Not declaring variables before use would lead to compilation errors as the program would lack the necessary information to handle data properly.

In programming languages, syntax refers to the set of rules that define the structure and form of the expressions and statements in a program, essentially dictating how symbols can be combined to create valid instructions . Semantics, on the other hand, refers to the meaning of the syntactically correct symbols and how they affect the execution of the program, thus impacting the behavior of the machine being programmed . The significance of this distinction lies in ensuring that programs are both correctly formed and meaningful, which is essential for accurate program execution and functionality.

Operators in C++ are symbols used to perform specific mathematical or logical computations on values known as operands . Examples include arithmetic operators like '+', '-', and '*', as well as logical operators like '&&' and '||' . Operator precedence is crucial because it determines the order in which operations are performed within expressions, thereby affecting the outcome of computations. For example, in the expression int a, b, c, d, a=b+c*d, the multiplication operation takes precedence over addition, meaning c*d is calculated first . Understanding this precedence prevents logical errors in programming.

Reserved words in C++ are keywords that have a specific meaning and function within the language and cannot be used for any other purpose, such as variable names . These words are used to perform important language-specific operations like loops, conditionals, and data type declarations, among others. They must be used correctly according to the language's syntax rules to ensure the program operates as intended. Using reserved words appropriately ensures that there is no conflict or ambiguity in the program, which supports accurate and error-free code development .

The program development lifecycle includes several critical stages: feasibility study, requirement analysis, designing a solution, testing, implementation, integration and system testing, and maintenance . This lifecycle is important because it provides a structured approach to software development, ensuring all aspects of a program are thoroughly examined and developed, which reduces errors, optimizes functionality, and enhances maintainability and scalability of software solutions .

Increment (++) and decrement (--) operators in C++ are used to increase or decrease the value of a variable by one, respectively . They come in two forms: prefix and postfix. In the prefix form (++Age, --Age), the operator alters the variable's value before yielding it, whereas, in the postfix form (Age++, Age--), the original value is yielded, and then the variable is altered . This distinction impacts expressions by determining the order of operations, which can lead to different results in computations depending on which form is used.

A C++ program is organized into several sections: Documentation, Link, Definition, Global Declaration, Function Definition, and Main Function . The Documentation Section is used for comments that describe the program's purpose. The Link Section includes headers that allow for standard or custom functionalities. The Definition Section contains preprocessor definitions and type definitions. Global Declaration Section is where global variables are declared. Function Definition Section contains the program's functions, and the Main Function is the starting point of the program where execution begins . Each section serves to organize the program and make the code more readable, understandable, and maintainable.

Flowcharts, structure charts, and pseudocode are three different tools used to describe algorithms. A flowchart uses graphical symbols to represent the sequential flow and operations of a process, making it easier to visualize the logic of an algorithm . Structure charts represent the hierarchical relationship between different modules or functions in a system, focusing more on the architecture rather than the specific logic . Pseudocode combines programming language structure with human-readable language, making it useful for outlining algorithms in a format that is both easy to understand and easily translatable into code . These tools differ in focus and form but all aim to clarify system design and logic.

Constants in C++ can be declared using the keyword 'const', for example, 'const float gravity = 9.8;', or through preprocessor directives such as '#define pi 3.14' . They are essential because they represent fixed values that cannot be altered during program execution, which ensures data integrity and helps prevent bugs caused by unintended modification of values that should remain constant . Constants also improve code readability and maintainability by allowing the usage of descriptive names instead of magic numbers.

You might also like