0% found this document useful (0 votes)
2 views20 pages

Lecture 1 - Introduction to Computer Programming Cs217

The document provides an introduction to Object Oriented Programming using C++, covering essential topics such as programming basics, data types, functions, and control structures. It outlines the phases of C++ program development, including editing, compiling, and executing code, while also discussing syntax and the use of various operators. Additionally, it includes examples of simple C++ programs demonstrating input/output operations and decision-making using relational operators.

Uploaded by

hussaincheema715
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)
2 views20 pages

Lecture 1 - Introduction to Computer Programming Cs217

The document provides an introduction to Object Oriented Programming using C++, covering essential topics such as programming basics, data types, functions, and control structures. It outlines the phases of C++ program development, including editing, compiling, and executing code, while also discussing syntax and the use of various operators. Additionally, it includes examples of simple C++ programs demonstrating input/output operations and decision-making using relational operators.

Uploaded by

hussaincheema715
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/ 20

Object Oriented Programming

Introduction

Lecture No. 1

Original author of the sides is Jawad


Hassan, FAST-NUCES, Islamabad but
slides have been modified time by
time as per the requirements of the
class.
Lecture Contents
• Introduction
• C++ Programming basics
• Pointers
• Functions
• Structures
• Classes (OOP basics, inheritance, …)
• Operator overloading
• Streams and Files
• Templates and Exceptions
What is a Computer?
• Computer
• A device capable of performing computations and making
logical decisions
• Computer programs
• Sets of instructions that control a computer’s processing of
data
• Hardware
• Various devices comprising a computer
• Examples: keyboard, screen, mouse, disks, memory, CD-ROM, and processing
units

• Software
• Programs that run a computer
Making Software Load into
memory and
start execution
Problem

Executable file
Develop
Algorithm
Compile the code
Express Algorithm in the
form of pseudo code or
flow chart Write algorithm in
C/C++/Java … Language

Writing code in “C++” language means, we must have to


follow a certain rules of “C++” language.
This is called syntax of “C++” language.
Making Software Load into
memory and
Compiler
start execution
Problem “Turbo C” in DOS
“gcc” or “g++” in Linux
“Visual C++” in windows Executable file
Develop “Borland” in
Algorithm DOS/Windows.
Compile the code
Express Algorithm in the
form of pseudo code or
flow chart Write algorithm in
C/C++/Java … Language

Writing code in “C++” language means, we must have to


follow a certain rules of “C++” language.
This is called syntax of “C++” language.
Basics of a Typical C++ Environment
Program is created in
Editor Disk the editor and stored
Phases of C++ Programs: on disk.

Preprocessor Preprocessor program


Disk processes the code.
1. Edit
Compiler creates
Compiler Disk object code and stores
it on disk.
2. Preprocess Linker links the object
code with the libraries,
Linker Disk creates a.out and
stores it on disk
3. Compile Primary
Memory
Loader

4. Link Loader puts program


in memory.
Disk ..

5. Load ..
..

Primary
Memory
6. Execute CPU CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
..
executes.
..
..
Introduction to C++ Programming
• C++ language
• Facilitates a structured and disciplined approach to computer program design

• There are various C++ standards:


• C++98: First ISO standard

• C++11 (aka C++0x) : Major Enhancement to language and library

• C++14 (aka C++1y): Bug fixes and improvements to C++

• C++17 (aka C++1z): Library additions and Bug fixes

• C++2a (next planned standard in 202x)

• Following are several examples


• The examples illustrate many important features of C++

• Each example is analyzed one statement at a time.


1 // Fig. 1.2: fig01_02.cpp

2 // A first program in C++


Comments
3 #include <iostream>
Written between /* and */ or following a //.
4
Improve program readability and do not cause the
5 int main() computer to perform any action.
6 {
preprocessor directive
7 std::cout << "Welcome to C++!\n";
Message to the C++ preprocessor.
8
Lines beginning with # are preprocessor directives.
9 return 0; // indicate that program ended successfully
#include <iostream> tells the preprocessor to
10 } include
C++ the contents
programs containofone
theor <iostream>,
filemore which
functions, one of
includes
which be main operations (such as printing to
input/output
must
the screen).
Welcome to C++! Parenthesis are used to indicate a function
int means
Prints the string of characters that main
contained between the an integer value.
"returns"
quotation marks. More in Chapter 3.
return is a way to exit a function
from a function. A left
The entire line, including brace { begins
std::cout, the the
<< body of every function
return 0, in this case,
operator, and a right to
thatstring "Welcome
means the } ends it. and
braceC++!\n"
the program terminatedthe semicolon (;), is called a statement.
normally.

All statements must end with a semicolon.


A Simple Program: Printing a Line of Text

• std::cout
• Standard output stream object
• “Connected” to the screen
• std:: specifies the "namespace" which cout belongs to
• std:: can be removed through the use of using statements

• <<
• Stream insertion operator
• Value to the right of the operator (right operand) inserted into output
stream (which is connected to the screen)
• std::cout << “Welcome to C++!\n”;
• \
• Escape character
• Indicates that a “special” character is to be output
A Simple Program: Printing a Line of Text
Escape Sequence Description

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

• There are multiple ways to print text


• Following are more examples
1 // Fig. 1.4: fig01_04.cpp

2 // Printing a line with multiple statements

3 #include <iostream>

5 int main()

6 {

7 std::cout << "Welcome ";

8 std::cout << "to C++!\n";

10 return 0; // indicate that program ended successfully

11 }

Welcome to C++!

Unless new line '\n' is specified, the text continues


on the same line.
Another Simple Program: Adding Two Integers

• Variables
• Location in memory where a value can be stored for use by a
program
• Must be declared with a name and a data type before they
can be used
• Some common data types are:
• int - integer numbers
• char - characters
• double - floating point numbers
• Example: int myvariable;
• Declares a variable named myvariable of type int
• Example: int variable1, variable2;
• Declares two variables, each of type int
Another Simple Program: Adding Two Integers

• >> (stream extraction operator)


• When used with std::cin, waits for the user to input a value and
stores the value in the variable to the right of the operator
• The user types a value, then presses the Enter (Return) key to send
the data to the computer
• Example:
int myVariable;
std::cin >> myVariable;
• Waits for user input, then stores input in myVariable

• = (assignment operator)
• Assigns value to a variable
• Binary operator (has two operands)
• Example:
sum = variable1 + variable2;
1 // Fig. 1.6: fig01_06.cpp
2 // Addition program
3 #include <iostream>
4
5 int main()
6 {
7 int integer1, integer2, sum; // declaration
8
9 std::cout << "Enter first integer\n"; // prompt
Notice how std::cin is used to get user
10 std::cin >> integer1; // read an integer
input.
11 std::cout << "Enter second integer\n"; // prompt
12 std::cin >> integer2; // read an integer
13 sum = integer1 + integer2; // assignment of sum
14 std::cout << "Sum is " << sum << std::endl; // print sum
15
16 return 0; std::endl
// indicate that program ended successfully flushes the buffer and
17 } prints a newline.

Enter first integer


45 Variables can be output using std::cout << variableName.
Enter second integer
72
Sum is 117
Arithmetic
C++ op era tion Arithm etic Alg eb ra ic C++ exp ression
op era tor exp ression
•Addition
Arithmetic
+ operators:
f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y

Modulus % r mod s r % s

Operator(s) Operation(s) Order of evaluation (precedence)

()
• Rules ofParentheses
operator precedence:
Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right.
*, /, or % Multiplication Division Evaluated second. If there are several, they re
Modulus evaluated left to right.
+ or - Addition Evaluated last. If there are several, they are
Subtraction evaluated left to right.
Decision Making: Equality/Relational Operators

• using statements
• Eliminate the need to use the std:: prefix
• Allow us to write cout instead of std::cout
• To use the following functions without the std:: prefix, write
the following at the top of the program
using std::cout;
using std::cin;
using std::endl;
1 // Fig. 1.14: fig01_14.cpp
2 // Using if statements, relational
3 // operators, and equality operators
4 #include <iostream>
5
6 using std::cout; // program uses cout
7 using std::cin; // program uses cin Notice the using statements.
8 using std::endl; // program uses endl
9
10 int main()
11 {
12 int num1, num2;
13
14 cout << "Enter two integers, and I will tell you\n"
15 << "the relationships they satisfy: ";
16 cin >> num1 >> num2; // read two integers
Enter two integers, and I will tell you
17
the relationships they satisfy: 3 7
18 if ( num1 == num2 )
19 cout << num1 << " is equal to " << num2 << endl;
20
The if statements test the truth
21 if ( num1 != num2 ) of the condition. If it is true,
22 cout << num1 << " is not equal to " << num2 << endl; 3body of if
is not statement
equal to 7 is
23 executed. If not, body is
24 if ( num1 < num2 ) skipped.
25 cout << num1 << " is less than " << num2 << endl; 3 is less than 7
26 To include multiple statements
27 if ( num1 > num2 ) in a body, delineate them with
28 cout << num1 << " is greater than " << num2 << endl;
braces {}.
29
30 if ( num1 <= num2 )
31 cout << num1 << " is less than or equal to " 3 is less than or equal to 7
32 << num2 << endl;
33
34 if ( num1 >= num2 )

35 cout << num1 << " is greater than or equal to "

36 << num2 << endl;

37

38 return 0; // indicate that program ended successfully

39 }

Enter two integers, and I will tell you


the relationships they satisfy: 3 7
3 is not equal to 7
3 is less than 7
3 is less than or equal to 7

Enter two integers, and I will tell you


the relationships they satisfy: 22 12
22 is not equal to 12
22 is greater than 12
22 is greater than or equal to 12

Enter two integers, and I will tell you


the relationships they satisfy: 7 7
7 is equal to 7
7 is less than or equal to 7
7 is greater than or equal to 7
References
• Course slides – based on course slides prepared by
Dr. Shariq Bashir and Dr. Uzair Khan, and Dr.
Ehatsham CP Spring 2011-2012.

• Text Books:
• “Object Oriented Programming in C++”, Author (s): Robert
Lafore.
• “C++ How to program”, Author (s): M. Deitel & Dietel.
• “Turbo C Programming”, Author (s): Robert Lafore.
• Questions?

You might also like