What Is A Computer Program?: Chapter One
What Is A Computer Program?: Chapter One
Computer Vs Human:
• Computers understand a language variously known as computer language or
machine language. It’s possible but extremely difficult for humans to speak
machine language.
• Therefore, computers and humans have agreed to sort of meet in the middle,
using intermediate languages such as C++.
• Humans can speak C++ , and C++ is converted into machine language to
understand the computer
Implementation phase:
• Translate the algorithm into a programming language
• Compile the source code
• Run the program on sample data
• Results may require modification of the algorithm and the program.
4. What is Object Oriented Programming? Explain its Characteristics?
Object Oriented Programming: Program is viewed as interacting objects
and Each object contains algorithms to describe its behavior.
• OOP Characteristics
1) Encapsulation:
• Information hiding
• Objects contain their own data and algorithms
2) Inheritance:
• Writing reusable code
• Objects can inherit characteristics from other objects
3) Polymorphism:
• A single name can have multiple meanings depending on its context
Classes:
• In OOP we say that objects are members of classes.
• A class is a description of a number of similar objects. Ahmed and Hassan
are members of the musician class.
• There is no one person called “musician” but specific people with specific
names are members of this class if they possess certain characteristics.
• An object is often called an “instance” of a class.
Reusability: if Once a class has been written, created, and debugged, it can be
distributed to other programmers for use in their own programs.
C++ and C:
C++ is derived from the C language. Strictly speaking, it is a superset of C:
Almost every correct statement in C is also a correct statement in C++.
The most important elements added to C to create C++ are concerned with
classes, objects, and Object-Oriented Programming. (C++ was originally
called “C with classes.”)
6. What is a C++?
• C++ is an object-oriented Language.
• As an object-oriented language, C++ has the power and extensibility to
write large-scale programs. C++ is one of the most popular programming
languages for all types of programs. Most of the programs you use on your
PC every day are written in C++.
Program Errors
Syntax errors:
• Violation 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 compiler at run-time
• Logic errors
• Errors in the program’s algorithm
• Most difficult to detect
• Compiler does not recognize an error
C C++
The main function should not return any The main function should return
value. value.
The scope of the variable should be declared The scope of the variable can be
at the beginning of the program. where of the program.
Directives:
• The two lines that begin the program are directives.
• The first is a preprocessor directive, and the second is a using directive.
• They occupy a sort of gray area: They are not part of the basic C++
language, but they are necessary anyway.
Cout and CIN
• The identifier cout (pronounced “C out”) is actually an object which is
predefined in C++ to correspond to the standard output stream.
• The operator << is called the insertion or put to operator. It directs the
contents of the variable on its right to the object on its left.
• The keyword cin (pronounced “C in”) is an object, predefined in C++ to
correspond to the standard input stream. This stream represents data coming
from the keyboard. The >> is the extraction or get from operator. It takes
the value from the stream object on its left and places it in the variable on its
right.
Comments:
• Comments help the person writing a program, and anyone else who must
read the source file, understand what’s going on. The compiler ignores
comments, so they do not add to the file size or execution time of the
executable program.
• so the comments should concentrate on the big picture, clarifying your
reasons for using a certain statement or group of statements
• In C++, there are two types of comment:
• Single-line comment which begins with // and terminates at the end of the
current line.
• Multi-line comment which begins with /* and ends with */.
Variables:
• A variable is a location in the computer's memory where a value can be
stored for use by a program. When a variable is given a value, that value is
actually placed in the memory space assigned to the variable.
• All variables must be declared with a name and a data type before they can
be used in a program.
• For example, int x;
Constants:
• The keyword constant is used to declare a constant and precedes the data
type of a variable.
For example: const int PI = 3.14;
• It specifies that the value of a variable will not change throughout the
program.
Manipulators:
• Manipulators are operators used with the insertion operator (<<) to modify
(or manipulate) the way data is displayed.
• setw changes the field width of output. The setw manipulator causes the
number (or string) that follows it in the stream to be printed within a field n
characters wide, where n is the argument to setw(n).
Operators
Rules of Operator Precedence
PEDMAS (Parentheses, Exponents, Division, Multiplication, Addition and
Subtraction).
Library Functions:
• Many activities in C++ are carried out by library functions. These functions
perform file access, mathematical computations, and data conversion, among
other things.
• One of the most important library function is sqrt() and is to calculate the
square root of a number that it takes as argument.