0% found this document useful (0 votes)
46 views

Lecture 4

This document provides an overview of programming fundamentals in C++. It discusses what a program is, the basic structure of a C++ program including preprocessor directives, the main function, and program body. It also covers important concepts like input/output streams, namespaces, variables, data types, comments, and common errors when starting with C++ programming. Examples are provided throughout to illustrate key points about writing basic C++ programs.

Uploaded by

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

Lecture 4

This document provides an overview of programming fundamentals in C++. It discusses what a program is, the basic structure of a C++ program including preprocessor directives, the main function, and program body. It also covers important concepts like input/output streams, namespaces, variables, data types, comments, and common errors when starting with C++ programming. Examples are provided throughout to illustrate key points about writing basic C++ programs.

Uploaded by

Muneeb Ashraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Programming Fundamentals

Maryam Imtiaz Malik


[email protected]
Recap to Previous Lecture

Problem Solving
Concepts

Algorithm Flow Charts Program

To be covered
today
C++

 C++ is a general-purpose programming language that was developed as an enhancement of the C


language to include object-oriented paradigm. It is a compiled language.
 C++ is a middle-level language rendering it the advantage of programming low-level (drivers,
kernels) and even higher-level applications (games, GUI, desktop apps etc.).
History of C++

It was created by Bjarne Stroustrup at Bell Labs circa


1980. C++ is very similar to C (invented by Dennis
Ritchie in the early 1970s).
Why we learn C++

 C++ is used to develop games, desktop apps, operating systems, browsers, and so on
because of its performance.

 After learning C++, it will be much easier to learn other programming languages like
Java, Python, etc.

 C++ helps you to understand the internal architecture of a computer, how computer stores
and retrieves information.
Advantages of C++

1. Portability
 C++ provides this feature of portability allowing us to develop codes without caring about the
hardware. This lets us move the development of a program from one platform to another.
 For example, you’re working on Windows OS and for some reason, you have to switch to LINUX,
the codes from Windows OS will also run in the LINUX OS without any error.
2. Mid-level programming language
 Being a mid-level programming language, we can treat it as both a low-level and high-level
language. Features of high-level language help to develop games and desktop applications, whereas
features of low-level language help make kernels and drivers.
Advantages of C++

3. Object Oriented Programming


 It proved to be of great significance since this feature was
not in C, this helped users to treat data as objects and classes.

4. Low-level Manipulation
 Since C++ is closely associated with C, which is a
procedural language closely related to the machine language,
C++ allows low-level manipulation of data at a certain level.
Embedded systems and compiler are created with the help of
C++.
Advantages of C++

5. Memory Management
 C++ gives the programmer the provision of total control over memory management.

6. Large Community Support


 C++ has a large community that supports it by providing online courses and lectures, both
paid and unpaid.
Advantages of C++

7. Compatibility with C
 C++ is pretty much compatible with C.

8. Scalability
 Scalability refers to the ability of a program to scale. It means that the C++ program is
capable of running on a small scale as well as a large scale of data.
Program

 A Program is a sequence of instructions in a particular programming language, written to

perform a specified task on a computer.

 IDE  Integrated Development Environment (IDE) software package combining a word


processor, compiler, linker, loader, and tools for finding errors
 C++ is case sensitive
Basic Structure of C++ Program

 Preprocessor Directive/ Compiler Directive


 Main () function
 Program body (C ++ statements)
Preprocessor/ Compiler Directives

 An instruction given to the compiler before execution of actual program


 Start with # symbol
 Written at the start of program
 Not terminated with semicolon
 Example:
 #include, #define
Header Files

 Collection of standard library functions to perform different tasks


 The header files are normally stored in INCLUDE subdirectory
 Syntax:
 #include<header_file_name> or #include”header_file_name”
 #include is known as a pre-processor directive.
 #include tells the pre-processor to include these header file in the program.
 <> or “” indicate the start and end of file name to be loaded.
 If the library functions are included in the program without explicitly including their header
file then the compiler generates the error.
iostream header file

 iostream is the header file which contains the functions of program like cout, cin. 
 <iostream> is called as input-output stream for C++, which comprises of various defined
functions for the console.
 #include <iostream>
Namespace std

 The compiler distinguished between the variable name used in the C++ program or a
variable used in the library function.
 std is the standard
 Namespace is a declarative region where input, output functions are defined for a class
(C++).
 cout, cin and a lot of other functions are defined in it.
 Syntax:
 using namespace std;
 Also call these functions using std::cout , std::cin.
Main() Function & Program Body

 Each program must have main function


 If a program does not have main function it can be compiled but not executed.
 Main function data type is integer as ‘int’.
 It includes a ‘return 0’ statement before ending of main function bracket. Which defines an
integer to be returned before ending of program
 Syntax:
 int main()
{
body of main function
return 0;
}
Executable & Non Executable Statements

 An executable statement causes the computer to perform some specific action when the
program runs.
 Display output
 Executable statements must always end with a semicolon.
 A non-executable statement describes some feature of the program or its data but
doesn’t cause the computer to perform any action when a program runs.
 Comment (Explanatory remarks made in a program)
 Line Comment (// Comment)
 Block Comment (/* set of lines */)
cout Object

 Console Output
 cout<< “Hello World !!!”;
 << represents insertion operator
 “ ” represents the string as sentence written to be displayed as output on console screen.
 Statement must be terminated with a semicolon.
Program Structure

#include <iostream> Program/ Pre-Processor Directives


using namespace std;

Main Function

int main ( ) Empty Arguments

Return Data Type (int) Executable & Non


{ Executable Statements
// Print Message
Program Body
cout<<“Hello World”;
return 0;
Return Type (int)
} // end of main block
First C++ Program

/* Program #1 - A first C++ program.

Enter this program, then compile and run it.


*/

#include <iostream>
using namespace std;

// main() is where program execution begins.

int main()
{
cout<<“Hello World!!!”;
return 0;
}
Common Coding Problems

The errors commonly made when first programming in C++ include the following:
1. Omitting the parentheses after main().
2. Omitting or incorrectly typing the opening brace, {, that signifies the start of a function body.
3. Omitting or incorrectly typing the closing brace, }, that signifies the end of a function.
4. Omitting the semicolon at the end of each C++ executable statement.
5. Adding a semicolon after the #include <iostream> preprocessor command.
6. Misspelling the name of an object or function, such as typing cot instead of cout.

7. Forgetting to enclose a string sent to cout with double quotation marks.

8. Forgetting the \n to indicate a new line.


Debugging in C++

 The process of finding and removing errors/bug


 Types of errors:
 Syntax Error
 Logical Error
 Run-Time Error
Syntax Errors

 Syntax is a collection of rules for writing programs in a programming language.


 Type of errors that occurs when an invalid statement is written in a program.
 Complier detect these errors and display error messages
 Causes:
 The statement terminator is missing at the end of statement.
 A misspelled keyword.
 Any of the delimiters is missing.
Logical Errors

 An error occur due to poor logic of the programmer.


 A statement with logical error may produce unexpected and wrong results in the
program.
 Compiler does not detect these errors.
 Difficult to find
 Example: Writing a>5 instead of a< 5
Run-Time Errors

 A type of error that occurs during the execution of program.


 It is caused when a statement directs the computer to execute an illegal operation, wrong
input from user such as dividing a number by zero.
 Example: The user may ask the program to open a file that does not exist.
Escape Character

 Escape Character

 Backslash ( \ )

 Special meaning in C++

 Placed in front of a group of characters, it tells the compiler to escape from normal
interpretation of these characters

 Escape sequence

 Combination of a backslash and specific characters

 Example: newline escape sequence, “\n”


Practice Problems

 Write a C++ Program to print the following output:

CSPF-121
“Programming Fundamentals”
 
References

 Gary j. Bronson, A first book of C++, Garry Bronson, 4th edition (Chapter 1)

You might also like