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

DATA STRUCUTERE AND

ALGORITHMS

Lecture 2
Object Oriented Programming

 Abbreviated OOP

 Used for many modern programs

 Program is viewed as interacting objects


 Each object contains algorithms to describe its behavior
 Program design phase involves designing objects and their
algorithms
OOP Characteristics
 Encapsulation
– Information hiding
– Objects contain their own data and algorithms

 Inheritance
– Writing reusable code
– Objects can inherit characteristics from other objects

 Polymorphism
– A single name can have multiple meanings depending on
its context
Introduction to C++
 Where did C++ come from?
– Derived from the C language
– C was derived from the B language
– B was derived from the BCPL language
C++ History
 C developed by Dennis Ritchie at AT&T
Bell Labs in the 1970s.
 Used to maintain UNIX systems
 Many commercial applications written in c
C++ History
 C++ developed by Bjarne Stroustrup at AT&T Bell
Labs in the 1980s.
 Killed several weaknesses of C
 Combined object oriented programming
 C remains a subset of C++
High-level Languages
 Common programming languages include …
C C++ Java Pascal Visual Basic
FORTRAN
COBOL Lisp Scheme Ada

 These high – level languages


 Resemble human languages
 Are designed to be easy to read and write
 Use more complex instructions than
the CPU can follow
 Must be translated to zeros and ones for the CPU
to execute a program
Low-level Languages
 An assembly language command such as

ADD X Y Z

might mean add the values found at x and y


in memory, and store the result in location z.

 Assembly language must be translated to


machine language (zeros and ones)
0110 1001 1010 1011
 The CPU can follow machine language
Introduction To Header File
 C++ is a huge language so much that it uses
various sets of instructions from different parts to
do its work.
 Some of these instructions come in computer files
that you simply "put" in your program.
 These instructions or files are also called libraries.
Introduction To Header File
 One of them asks the computer to receive keyboard
strokes from you the user (when you press a key)
and another asks the machine (the computer
performing some operations) to give back a result.
Introduction To Header File
 The libraries are files that you place at the
beginning of your program as if you were telling
the computer to receive its first instructions from
another program before increasing on yours. The
libraries are (also) called header files and, as
computer files, they have the extension ".h". An
example would be house.h, or person.h.
Introduction To Header File
 The first library we will be interested in is called
iostream. It asks the computer to display stuff on
the monitor's screen.
 To see how to put a library in your program, you
put it at the beginning of the file. Here is an
example:
iostream.h
Introduction To Header File
 To use a library in your program, you simply
include it by using the word "include" before the
name of the library, as follows:
include iostream.h
Introduction To Header File
 if you want the computer to "know" that the
word "include" means, "I want to include the
following library", you will have to append a
special sign to it. The pound sign "#" will do just
that. Therefore, to include a library, you precede
the include word with the # sign. Here is an
example:
#include iostream.h
Introduction To Header File
 When you include a library that came with C++,
you enclose it between < and > as follows:
#include <iostream.h>
Introduction To Namespace
 A namespace is a section of code, delimited and
referred to using a specific name. A namespace is
created to set apart a portion of code with the goal
to reduce, otherwise remove, confusion or
Mistakes.
 This is done by giving a common name to that
portion of code
Introduction To Namespace

 Because C++ is so huge, its libraries are created


in different namespaces, each with a particular
name. To use an existing namespace in your
program, you must know its name. To use such a
namespace, you can type the using namespace
expression followed by the name of the
namespace and a semi-colon.
Introduction To Namespace
 One of the namespaces used in C++ is called std.
Therefore, to use it, you can type:
using namespace std;
 After typing this, any part of the namespace
becomes available to you
Introduction To Namespace
 The iostream library we mentioned above is part
of the std namespace. When you use it, you don't
need to include the extended of the iostream file.
For this reason, you can start your program with:
#include <iostream>
using namespace std;
C++ Instructions
 C++ works by giving (separate) instructions to the
computer. These instructions can be treated as
assignments. On this site, such an assignment will
be called a function. The primary function used in
C++ is called main.
C++ Instructions
 To separate a function from the other types of
things you will be using in your programs, a
function's name is followed by an opening and a
closing parentheses. For example, the main
function will always be written at least as main().
C++ Instructions
 The body of a function starts with an opening curly
bracket "{" and closes with a closing curly bracket
"}".
 Therefore, the main() function can be written as:

main() { statements }
Testing and Debugging

 Bug
 A mistake in a program
 Debugging
 Removing mistakes in programs
Program Errors

 Syntax errors
 Damage of the grammar rules of the language
 Discovered by the compiler
 Error messages may not always show correct location of
errors
 Run-time errors
 Error conditions detected by the computer at run-time
 Logic errors
 Errors in the program’s algorithm
 Most difficult to diagnose
 Computer does not recognize an error
C ++ Keywords
25

Copyright © Lecturer: Eng. Abdulkarim Dhaqane


To create New File
 Click File Manu
 Select New Button
 Click Source File
 Then write your code.
How To Create a Simple Program In C++

#include <iostream.h>
using namespace std;
int main()
{
cout<<"Wel come To C++!" <<endl;
system("pause");
return (0);
}
What Is a Variable
 A variable/ identifier is a specific piece of memory in
your computer that consists of one or more connecting
bytes.
 An identifier is the name to denote labels, types,
variables, constants or functions, in a C++ program.
 C++ is a case-sensitive language.
 Work is not work
 Identifiers should be descriptive
 Using meaningful identifiers is a good programming practice
int,char,float and double
Logical Operators
 The Equality Operator ==
 The Logical Not Operator !
 The Comparison for a Lower Value <
 Combining Equality and Lower Value <=

 The Comparison for a Greater Value >

 The Greater Than or Equal Operator >=

 Boolean Variables
Using Blocks of Code in if Statements
30

 The syntax for an if statement that involves


statement blocks is as follows:
if(expression)
{
StatementA1;
StatementA2;
...
}
else
{
StatementB1;
StatementB2;
...
}
Next_statement;
Logical Operators
 The AND Operator &&
 The OR Operator ||
 The NOT Operator !
Using else-if Statements for Multiple Choices
32

 The use of the else-if statement for selecting one of a


set of choices looks like this:
 if(choice1)
 /* Statement or block for choice 1 */
 else if(choice2)
 /* Statement or block for choice 2 */
 else if(choice3)
 /* Statement or block for choice 2 */
 /* … and so on … */
 else
 /* Default statement or block */
The switch Statement
33

 The syntax of the switch statement is:

switch(Expression)
{
case Choice1:
Statement1;
case Choice2:
Statement2;
case Choice-n:
Statement-n;
}
Loops
34

 There are three methods by way of which we can


repeat a part of a program. They are:
 Using a for statement
 Using a while statement
 Using a do-while statement
Increment and Decrement Operators
35

 The increment operator (++) and the decrement


operator (--) will increment or decrement the value
stored in the integer variable that they apply to by
1.
 Suppose you have defined an integer variable,
number, that currently has the value 6. You can
increment it by 1 with the following statement: +
+number;
What is Functions?
 A function in C++ is a group of statements that
together perform a specific task.
37
Function Body
 In the body of the function, you describe the
assignment the function is supposed to perform.
As simple as it looks, a function can be used to
display a message. Here is an example:
void Message()
{
cout << “Welcome To C++ Language.";
}
END
?

You might also like