Computer Programming
ICT-1205
Dr. Mohammad Abu Yousuf
[email protected]
1
Introduction
• C++ began as an expanded version of C.
• The C++ extensions were first invented by Bjarne
Stroustrup in 1979 at Bell Laboratories.
• C++ is a object-oriented programming (OOP) language.
• Object-oriented programming (OOP) is a programming
paradigm based on the concept of "objects", which may
contain data, in the form of fields, often known
as attributes; and code, in the form of procedures, often
known as methods.
2
Introduction
• C++ fully supports object-oriented programming, including
the three pillars of object-oriented development:
– Encapsulation
– Inheritance
– Polymorphism
3
Encapsulation
• Encapsulation is the mechanism that binds together
code and the data it manipulates, and keeps both safe
from outside interference and misuse.
• In an object-oriented language, code and data may be
combined in such a way that a self-contained "black box"
is created.
• When code and data are linked together in this fashion,
an object is created. In other words, an object is the
device that supports encapsulation.
• In OOP paradigm, "object" refers to a particular
instance of a class.
4
Encapsulation
• Within an object, code, data, or both may be private to
that object or public.
• Private code or data is known to and accessible only by
another part of the object.
• When code or data is public, other parts of your program
may access it even though it is defined within an object.
5
Polymorphism
• Polymorphism means to process objects differently
based on their data type.
• In other words it means, one method with multiple
implementation, for a certain class of action. And which
implementation to be used is decided at runtime
depending upon the situation (i.e., data type of the
object).
• A real-world example of polymorphism is a thermostat.
No matter what type of furnace your house has (gas, oil,
electric, etc.), the thermostat works the same way. In this
case, the thermostat (which is the interface) is the same
no matter what type of furnace (method) you have.
6
Inheritance
• Inheritance is the process by which one object can
acquire the properties of another object.
• It supports the concept of hierarchical classification.
• For example, a Red Delicious apple is part of the
classification apple, which in turn is part of the fruit class,
which is under the larger class food.
• Without the use of classifications, each object would
have to define explicitly all of its characteristics.
7
C++ Basic Syntax
• A C++ program, it can be defined as a collection of objects that
communicate via invoking each other's methods.
• Object - Objects have states and behaviors. Example: A dog
has states - color, name, breed as well as behaviors - wagging,
barking, eating. An object is an instance of a class.
• Class - A class can be defined as a template/blueprint that
describes the behaviors/states that object of its type support.
• Methods - A method is basically a behavior. A class can
contain many methods. It is in methods where the logics are
written, data is manipulated and all the actions are executed.
• Instance Variables - Each object has its unique set of instance
variables. An object's state is created by the values assigned to
these instance variables.
8
C++ Program Structure
9
Explanation of previous example
• The header supports C++ style I/O operations.
(<iostream> is to C++ what stdio.h is to C.).
• There is no .h extension to the name iostream. The
reason is that <iostream> is one of the modern-style
headers defined by Standard C++. Modern C++ headers
do not use the .h extension.
10
Explanation of previous example
• The line using namespace std tells the compiler to use
the std namespace. Namespaces are a relatively recent
addition to C++.
• A namespace creates a declarative region in which
various program elements can be placed.
• The using statement informs the compiler that you want
to use the std namespace. This is the namespace in
which the entire Standard C++ library is declared.
• By using the std namespace, we simplify access to the
standard library. For example, The cout object is defined
within a namespace named std (for “standard”) in the
<iostream> header file.
11
Explanation of previous example
• The line int main() is the main function where program
execution begins.
• The next line cout << "This is output.\n"; causes the
message " This is output ." to be displayed on the screen.
• It (<<) is an output operator. (It is also called the put
operator or the stream insertion operator). It inserts
values into the output stream that is named on its left.
For example, cout << 66; would display the number 66 on
the screen.
• The next line // this is a single line comment. is a single-
line comment available in C++. Single-line comments
begin with // and stop at the end of the line. 12
Explanation of previous example
• Next, the program prompts the user for a number. The
number is read from the keyboard with this statement: cin
>> i; Here, >> is an input operator.
• This statement causes i to be given a value read from the
keyboard. The identifier cin refers to the standard input
device, which is usually the keyboard. In general, you can
use cin >> to input a variable of any of the basic data types.
• There is not supposed to be an & in front of the i. However,
because of the way the >> operator is implemented in C++,
you do not need (in fact, must not use) the &. 13
Explanation of previous example
• Another interesting line in the program is shown here:
cout << i << "squared is " << i*i << "\n";
Assuming that i has the value 10, this statement causes
the phrase 10 squared is 100 to be displayed, followed by
a carriage return-linefeed. As this line illustrates, you can
run together several << output operations.
• The next line return 0; terminates main( )function and
causes it to return the value 0 to the calling process.
14
Another Example
• Yet another “Hello, World” program
int main()
{ // prints "Hello,W orld!":
cout << "Hel" << "lo, Wo" << "rld!" << endl;
}
Output: "Hello, World!"
15
Explanation of previous example
• The output operator is used four times here, dropping
the four objects "Hel", "lo,Wo" , "rld!", and endl into the
output stream.
• The first three are strings that are concatenated together
(i.e., strung end-to-end) to form the single string
"Hello,World!" .
• The fourth object is the stream manipulator object endl
(meaning “end of line”). It does the same as appending
the endline character '\n' to the string itself: it sends the
print cursor to the beginning of the next line.
16
Thank you
17