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

Lecture 1 - Introduction and Structure of A Program

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)
23 views

Lecture 1 - Introduction and Structure of A Program

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
You are on page 1/ 25

Lecture 1

Introduction and Structure of a


Program
Objectives
• Explain the overview of the subject and give an
introduction to C++ language.
• Clarify how to create a program, compile it and
rum the program.
• Explain the structure of a simple C++ program,
including the main function and body of the
function.
• Discuss the use of comments and different ways
of commenting.
• Explain the needs of libraries.
• Know the use of std namespace and its visibility.
2
CPT111 – Introduction to Programming
• To obtain a pass in this subject, students must:
– Submit all assessment components; and
– Obtain a minimum of 50% from the final exam
and
– Obtain a minimum composite mark of 50% from
the subject

• It is a pre-requisite for:
– CPT117 – Visual Basic Programming (DIT & BIT)
– CPT203 – Data Structure and Algorithms (BIT)
– CPT205 – Object Oriented Programming (BIT)
3
Assessment Scheme
Programming Throughout the
10%
Exercises term
Course Dry Run Exam 10% Week 8
Work Practical Exam Week 9
15%
Assignment 15% Week 13
Final 2 hours During Final
50%
Exam Exam weeks

4
Week Lecture Topics
1 Introduction and Structure of a program
2 Basic input and output
3 Variables, constants and data types
4 Type conversions
5 Operators and expressions
6 Decision making and branching
7 Decision making and looping
8 Arrays
9 Preparation of Practical Exam
10 Character sequences
11 Functions I
12 Functions II
13 Name visibility
14 Classes 5
Week Tutorial Topics
1 Exercise on structure of a program
2 Exercise on input and output stamens
3 Exercise on variables, constants and data types
4 Exercise on type conversion
5 Exercise on operators and expressions
6 Exercise on decision making and branching
7 Exercise on decision making and looping
8 Exercise on arrays and Dry Run Exam
9 Practical Exam
10 Exercise on character sequences
11 Exercise on functions I
12 Exercise on functions II
13 Exercise on Name visibility and Work on Assignment
14 Work on assignment and submission of assignment 6
http://gem.mnu.edu.mv/
History of C++
• Developed by Bjarne Stroustrup at AT&T
Bell Laboratories in Murray Hills, New Jersey,
USA in the 1980s.
• Work of his Ph.D. Thesis.
• Overcame several shortcomings of C language.
• Incorporated object oriented programming.
• C remains a subset of C++
• In mid-2011, the new C++ standard (C++11)
was finished.
9
C++ Language
• Derived from the C and Simula67 languages
– Simula67 is a general purpose language
– C is a higher level language

• OOP features taken from Simula67


– Classes and Objects
– Inheritance
– Function Overloading
– Polymorphism etc...

• Other features taken from C language


10
C++ Names
• C with classes - class was a major addition to
the original C language

• In 1983, the name “C with classes” was


changed to C++.

• Idea came from C increment operator ++


– The ++ operator is an operator for incrementing a
variable by one.

11
Create, Compile and Run a Program
• To create a program
– Open Dev C++ and select File > New > Source
File (Ctrl + N)
– Type the program and save it.
• To compile the program
– Select Execute > Compile (F9)
• To run the program
– Execute > Run (F10)
• To compile and run the program
– Execute > Compile & Run ( F11)
12
Structure of a Program
//First program in this class
#include<iostream>
int main()
{
std::cout<<"Welcome to C++ class";
}
• First line with two slashes is a comment line.
• This can be used to give an explanation of the
code.
13
Structure of a Program
• #include <iostream>
• Lines beginning with a hash (#) are directives
read and interpreted by what is known as the
preprocessor.

• A directive is a language construct that specifies


how a compiler should process its input.

• A preprocessor is a program that processes its


input data to produce output that is used as input
to another program.
14
Structure of a Program
• The output is said to be a preprocessed form of
the input data, which is often used by
programs like compilers.

• These lines are interpreted before the


compilation of the program.

• The directive #include <iostream>, instructs


the preprocessor to include header iostream,
that allows to perform standard input and
output operations.
15
Structure of a Program
• int main()
• This line initiates the declaration of main()
function.

• Its definition is introduced with a type int, a name


main() and a pair of parentheses ().

• The main() is a special function in all C++


programs.

• The execution of all C++ programs begins with


the main() function.
16
Structure of a Program
• The open brace { indicates the beginning
of main's function definition, and the closing
brace } indicates its end.

• Everything between these braces is the


function's body.

• All functions use braces to indicate the


beginning and end of their definitions.
17
Structure of a Program
• std::cout << "Welcome to C++ class";

• This is a C++ statement.

• A statement is an expression that can actually


produce some effect.

• Statements are executed in the same order that


they appear within a function's body.
18
Structure of a Program
• std::cout << "Welcome to C++
class";

• This statement has three parts:


1. std::cout - identifies the standard character output
device.
2. The insertion operator << - indicates that what
follows is inserted into std::cout.
3. A string within quotes "welcome to C++ class" -
is the content inserted into the standard output.
19
Program Features
• C++ program is a collection of functions.

• Every C++ program must have a main()

• Execution of the program starts and ends in


main()

• It is a free form language.

• C++ statements terminate with semicolon.


20
Comments
• Comments can be used to document directly
within the source code.

• Comments do not affect the operation of the


program.

• Comments can be inserted in two ways:


– Single line comment starts with double slash (//)
– Block comment starts with /* and ends with */

21
Comments
/*This is the second program in this class.
This is an example of block comment */

#include<iostream>
int main()
{
std::cout<<"Welcome";//welcome message
std::cout<<"Learn with fun";//encouraging
}

22
Needs of Libraries
• It provides instructions to the compiler to link
functions from the system library.

• #include<iostream.h> header file for the use


of cin and cout objects.

• #include<math.h> header file for using


mathematical functions.

• The #include<iomanip.h> header file for


using manipulators 23
Using namespace std
• cout is part of the standard library.
• All the elements in the standard C++ library are
declared within namespace: the namespace std.
• To refer to the elements in the std namespace a
program:
– Qualify each and every use of elements of the library -
std:: cout), or
– Introduce visibility of its components.
• Use declaration to introduce visibility of these
components.
• using namespace std;
24
Using namespace std
• The last example with unqualified use of cout:

/*This is the second program in this


class. This is an example of block
comment */

#include<iostream>
using namespace std;
int main()
{
cout<<"Welcome";//welcome message
cout<<"Learn with fun";//encouraging
}
25

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