Chapter 2 (Part 1)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Basic C++ Programming

1
Contents
 Objectives
 Learning Outcomes
 Introduction of C++
 Basic Structure of C++ Program
 Whitespace
 Escape Sequence
 The endl manipulator
 Comments
 Exercise
 Summary
 References

2
Objectives

 To understand the C++ compiler and basic C++ program


construct
 To understand how to write, compile and execute(Run) the
program
 To know how to use library functions in C++ program

3
Learning Outcomes

 On completion of the lecture the student will be able to:


 understand the basic program construction

 understand how to write the output on screen

 understand how to write comments, escape sequence

4
Introduction of C++

 C++ is a high-level Programming Language

 C++ is a compiler
 Compiler take source code and transform it into executable
files.
 Source files are text files(extension .CPP) file.

 Executable files have (extension .EXE) and can be


executed either from within your compiler or directly from
a DOS window.

5
Introduction of C++

6
Basic structure of C++ program
//Welcome.cpp C++ Program

#include <iostream>  Preprocessor Directive Editor (IDE)


.cpp file
using namespace std;  Using Directive
Preprocessor
int main()
{ Compiler
programstatement1; .obj file
programstatement2; Function
Library Linker
return 0;
} .exe file
Loader

CPU

Output

7
About Basic Program
 Line 1, Start with a number sign ‘#’ is called preprocessor
directive. The <iostream.h> is called a header file.
 Line 2 is Using Directive.
 Line 3 is a function called main( ). The int is the return value
of function main().
 Line 5 and 6 are the program statements.
 Line 7 is the return statement.
 Line 4 is the begin of the function main() and line 8 is the end
of the function main().

8
Preprocessor Directives

The first line of the program - #include <iostream.h>


 It starts with a number sign, (#) and does not end with a
semicolon, called a preprocessor directive.
 A preprocessor directive is not a program statement.
 It is an instruction to the compiler.
 The preprocessor directive #include tells the compiler to insert
another files into your source file.
 The type file usually included by #include is called a header
file.

9
Header Files
 Many functions and symbols needed to run a C++ program are
provided as collection of libraries.
 Every library has a name and is referred to by a header file.
 Each header file contains file access, mathematical computations,
and data conversion, among other its information for a particular
group of library functions.
 The preprocessor directives #include tells the compiler to add the
source file iostream.h to the source file before compiling.
 iostream.h is an example of a header file, and also called include
file.
 It’s concerned with basic input/output operations.
10
Using Directive
 Using Directive allows all the names in a namespace to be used.
 Namespace is a declarative region that provides a scope to the
identifiers (the names of types, functions, variables, etc) inside it.
 Namespace a part of the program in which certain names are
recognized; outside of namespace they’re unknown.
 Example: using namespace std;
 If you do not use the using directive,
 std :: cout<< “Hi! Students” ;

11
Functions

 Functions are one of the fundamental building blocks of C++.


 In a C++ program, consists a single function called main ().
 The parentheses following the word main are the
distinguishing feature of a function.
 This function has a return value of type int.
 The body of the function is surround by braces, called curly
brackets. A function body can consist of many statements.
 Parentheses aren’t always empty.

12
Always Start with main()
 When you run a C++ program, the first statement executed will
be at the beginning of a function called main().
 The program may consist of many functions, classes, and other
program elements, but on startup, control always goes to main().
 If there is no function called main() in your program, an error
will be reported when you run the program.

13
Getting Started
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}

14
Output statement

 Syntax for output


cout << “Hello world!” ;
 The identifier “cout” is the standard output stream, flow of
data. (pronounced “C out” )
 The operator “<<” is called the insertion or put to operator
which sends the text output to display.
 The phrase in quotation marks “text output” is called a string
constant which is the text you would like to display.

15
Whitespace
 Consider the program: #include <iostream>
using namespace std;
  int main ()
  {     cout
     <<
    "Hello! Welcome to UCSY"
    ; return
 0;}

 Whitespace is defined as spaces, carriage returns, linefeeds,


tabs, vertical tabs, and form feeds. These characters are
invisible to the compiler.
 Actually, the compiler ignores whitespace almost completely.

16
Example Program

Write a C++ program that will display the following message


on the screen.

My name is “Cherry Phue”.


My registration number is ‘7777’.

17
Escape Sequences
 The backslash causes an “escape” “\’” from the normal way
characters are interpreted.

 cout<<“My name is \“Cherry Phue\”.”;

 Escape sequences can be used as separate characters or


embedded in string constants.

18
Escape Sequences (Cont’d)

19
Escape Sequences (Cont’d)

#include <iostream>
using namespace std;
int main()
{
cout<<"My name is \"Cherry Phue\".\nMy Registeration
number is \'7777\'.";
return 0;
}

20
The endl manipulator

 The endl manipulator causes a linefeed to be inserted into the


stream, subsequent text is display on the next line.
Manipulators are instructions to the output stream that modify
the output in various ways.
Same effect as using ‘\n’ character but it causes the output buffer
to be flushed, but it happens invisibly so for most purposes the
two are equivalent.

21
The endl manipulator

#include <iostream>
using namespace std;
int main()
{
cout<<"My name is \"Cherry Phue\"."<<endl<<"My
Registeration number is \'7777\'.";
return 0;
}

22
Cascading

 The cascading of the input and output operators refers to the


consecutive occurrence of input or output operators in a single
statement.
 Example cascading cout
 cout << “Hello !“ << “\“Happy New Year\”” <<“\n”;

23
Comments
 Comments are an important part of any program.
 Comments help the person writing a program and anyone else
who must read the source file, understand what’s going on.
 Compiler ignores comments, so they do not add the file size or
execution time of the executable program.

24
Comments syntax
C++ has two comments style:
1. Comments start with a double slash symbol(//) and terminate
at the end of the line.
// single line comment (preferred)

2. You can write a multiline comment with only two comment


symbols:
/* this is an old-style comment */
/* this
is a potentially
very long
multiline comment */

25
When to use Comments

Use comments to
Explain to the person looking at the listing what you’re
trying to do.
Example,
Identify program
 who wrote it
Record when program was written
Add descriptions of modifications

26
Comments (Example)
// comments.cpp
// This program demonstrates comments
#include <iostream> //preprocessor directive
using namespace std; // using directive
int main() //function name “main”
{ //start function body
cout << “Every age has a language of its own\n”; //statement

return 0; // return statement


} //end function body

27
Exercise
1. Write a C++ program that will display the following welcome
message on the screen.

Welcome to UCSY and I love writing C++ program.

2. Write a C++ program that will display your name and ID on


the screen.

My name is Aye Aye and My registration number is 22022.

28
Exercises
3. Type the following codes and compile and run it. What output will
be display?
cout<<“Listen to the beep now.\a”;
cout<<“\n Where is the \’t\’ in cat\b?\n\n”;
cout<<“I earned $50 \r where is the money?\n”;
cout<<“The rabbit jumps \t\t two tabs.\n\n”;

29
Summary
 How to write new program with basic structure
 How to save the program
 How to open the specific program
 Whitespace
 Escape Sequence
 The endl manipulator
 Comments

30
References

1. Object-Oriented Programming in C++ (Fourth Edition) by Robert Lafore

2. Programming Logic and Design Comprehensive (Sixth Edition) by Joyce

Farrell

3. Data Structures using C++ by Varsah H. Patil

4. C++ Language Tutorial (e-book)

5. C Programming for Engineering & Computer Science by H.H. Tan and T.B.

D’Orazio

31
Thank You!

32

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy