Uploaded by ijeomaokapla859

principles of programming updated note

advertisement
Why study priniciple of programming
languages?
To improve the ability to develop effective algorithms.
To improve the use of familiar languages.
To increase the vocabulary of useful programming
constructs.
To allow a better choice of programming language.
To make it easier to learn a new language.
To make it easier to design a new language.
To simulate useful features in languages that lack them.
To make better use of language technology wherever it
appears.
Overall advancement of computing
There is a global view of computing that can justify
the study of programming language concepts.
• Better use of languages that are already known
By studying the concepts of programming
l a n g u a g e s , p ro g ra m m e rs c a n l e a r n a b o u t
previously unknown and unused parts of the
languages they already use and begin to use those
features.
Increased ability to learn new
languages
In software development, continuous learning is essential.
The process of learning a new programming language can
be lengthy and difficult, especially for someone who is
comfortable with only two or more languages.
Once a thorough understanding of the fundamental
concepts of languages is acquired, it becomes far easier to
see how these concepts are incorporated into the design of
the language being learned
Different Programming Domains
Scientific applications
– Large number of floating point computations.
The most common data structures are arrays and matrices
The most common control structures are counting
loops and selections
– The first language for scientific applications was Fortran,
ALGOL 60 and most of its descedants
– Examples of languages best suited: Maple
Business applications
• – Business languages are characterized by
facilities for producing reports, precise ways of
describing and storing decimal numbers and
character data, and ability to specify decimal
arithmetic operations.
• – Use decimal numbers and characters
• – COBOL is the first successful high-level
language for those applications.
Artificial intelligence
– Symbols rather than numbers are typically
manipulated
– Symbolic computation is more conveniently done
with linked lists of data rather than arrays
– This kind of programming sometimes requires
more flexibility than other programming domains
– First AI language was LISP and is still most widely
used
– Alternate languages to LISP are Prolog – Clocksin
and Mellish
Systems programming
– The operating system and all of the
programming support tools of a computer
system are collectively known as systems
software
– C i s ex t e n s i v e l y u s e d fo r sy st e m s
programming.
The UNIX OS is written almost entirely in C
Web software
– Markup languages
• Such as HTML
– Scripting languages
• A list of commands is placed in a file
or in XHTML document for execution
example : Perl, JavaScript, PHP
Language Evaluation Criteria
• The following factors influences Language evalution criteria
• 1) Readability
• 2) Simplicity
• 3) Orthogonality
• 4) Writ ability
• 3) Reliability
• 4) Cost
Readability
• One of the most important criteria for
judging a programming language is the
ease with which programs can be read and
understood.
Overall simplicity
• Language with too many features is more
difficult to learn
• Orthogonolity is closely related to simplicity.
The more orthogonal the design of a
language, the fewer exceptions the
language rules require. Fewer
exceptions mean a higher degree of
regularity in the design, which makes
the language easier to learn, read,
and understand.
Writability
– Most readability factors also apply to
writability
– Simplicity and orthogonality
– Control statements, data types and
structures
– Support for abstraction
Reliability
A program is said to be reliable if performs to its
specifications under all conditions.
Cost
– Training programmers to use
language
– Writing programs in a particular
problem domain
– Compiling programs
– Executing programs
– Language implementation system (free?)
Different types of programming
languages
• Imperative
– Central features are variables, assignment
statements, and iteration
– Examples: C, Pascal
• Functional
– Main means of making computations is by
applying functions to given parameters
– Examples: LISP, Scheme
• Logic (declarative)
– Rule-based (rules are specified in no particular
order)
– Example: Prolog
• Object-oriented
– Data abstraction, inheritance, late binding
– Examples: Java, C++
• Markup
– New; not a programming per se, but used to specify
the layout of information in Web documents
Imperative
Ø procedural : C, Ada, Pascal, Algol, Fortran, . . .
Ø object-oriented :Scala, C#,Java, Smalltalk, SIMULA, . . .
Ø Scripting: Perl, Python, PHP, JavaScript, . . .
Declarative
• Functional: Haskell, SML, Lisp, Scheme, . . .
• Logic: Prolog
• dataflflow : Id, Val
• constraint-based : spreadsheets
• template-based : XSLT
Different types Implementation Methods for
programming languages.
• We have three different types of implementation
methods . They are
•
Compilation
– Programs are translated into machine language
•
Pure Interpretation
– Programs are interpreted by another program
known as an interpreter
•
Hybrid Implementation Systems
–A compromise between compilers and pure
interpreters
The Compilation Process
• Translate high-level program (source
language) into machine code (machine
language)
• Slow translation, but fast execution.
• Translates whole source code into object
code at a time and generate errors at the
end . If no errors ,generates object code .
• Ex: C++ is a compiler
Pure Interpretation Process
• Translation of source code line by line so that
generates errors line by line. If no errors in the
code generates object code.
• • Immediate feedback about errors
• • Slower execution
• • Often requires more space
• Used mainly for scripting languages
• Example for interpreter is Dbase III plus
Hybrid Implementation Process
• A compromise between compilers and pure
interpreters
• • A high-level language program is translated
to an intermediate language that allows easy
interpretation
• • Faster than pure interpretation
• Example for hybrid implementation is Java.
Programming Environments’
The collection of tools used in software
development
• • compiler
• • debugger
• • Integrated Development Environments
provide a graphical interface to most of the
necessary tools
• Integrated Development Environments
Eclipse
• – An integrated development environment for Java, written
in java
• – Support for other languages is also available
Borland JBuilder, NetBeans
• – Other integrated development environments for Java
Microsoft Visual Studio.NET
• – A large, complex visual environment
• – Used to program in C#, Visual BASIC.NET, Jscript, J#, or
C++
• codeblock
The languages like C++ and Java use classes and
object in their programs and are called Object
Oriented Programming languages The main task
is divided into several modules and these are
represented as classes Each class can perform
some tasks for which several methods are
written in a class This approach is called Object
Oriented approach
We shall use C++ to implement OOP features
Example of C++ Program
Write a program in C++ to sum two numbers and print your output
#include <iostream>
using namespace std;
int main() {
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
}
Features of OOP:
Class: In object-oriented programming, a class is a programming language
construct that is used as a blueprint to create objects This blueprint includes
attributes and methods that the created objects all share Usually, a class
represents a person, place, or thing
General form of a class:
class class_name
{
Properties (variables);
Actions (methods);
}
Object: An Object is a real time entity An object is an instance of
a class. Instance means physically happening
An object will have some properties and it can perform some
actions. Object contains variables and methods
The objects which exhibit similar properties and actions are
grouped under one class “To give a real world analogy, a house is
constructed according to a specification Here,
the specification is a blueprint that represents a class,
and the constructed house represents the object”
Encapsulation
Encapsulation: Wrapping up of data (variables) and methods into single unit is called
Encapsulation Class is an example for encapsulation
Encapsulation can be described as a protective barrier that prevents the code and data
being randomly accessed by other code defined outside the class.
Encapsulation is the technique of making the fields in a class private and providing access
to the fields via methods If a field is declared private, it cannot be accessed by anyone
outside the class.
Below are the features of encapsulation:
We can not access any function from the class directly.
We need an object to access that function that is using the member variables of that
class.
The function which we are making inside the class must use only member variables,
only then it is called encapsulation.
If we don’t make a function inside the class which is using the member variable of the
class then we don’t call it encapsulation.
Increase in the security of data
It helps to control the modification of our data members.
C++
// C++ program to demonstrate Encapsulation
#include <iostream>
using namespace std;
class Encapsulation {
private:
// Data hidden from outside world
int x;
public:
// Function to set value of variable x
void set(int a) { x = a; }
// Function to return value of variable x
int get() { return x; }
};
// Driver code
int main()
{
Encapsulation obj;
obj.set(5);
cout << obj.get();
return 0;
}
Output
5
Explanation: In the above program, the variable
x is made private. This variable can be accessed
and manipulated only using the functions get()
and set() which are present inside the class.
Thus we can say that here, the variable x and the
functions get() and set() are bound together
which is nothing but encapsulation.
Download