Evolution of Programming Languages
Evolution of Programming Languages
• historical sequence
– C++ tries to remedy drawbacks and limitations of C
– Java reacts to size and complexity of C++
• compatibility with C
– source and object
1
Stacks in C: a single stack
int stack[100];
int *sp = stack; /* first unused */
int pop(stack s) {
return *--s.sp;
}
2
Another stack implementation
typedef struct {
int *stk;
int *sp;
} stack;
Problems
3
C++
• a better C
– more checking of interfaces (ANSI C)
– other features for easier programming
• data abstraction
– you can hide HOW something is done in a program,
– reveal only WHAT is done
– HOW can be safely changed as program evolves
• object-oriented programming
– inheritance -- new types can be defined that inherit
properties from previous types
– polymorphism or dynamic binding -- function to be
called is determined by data type of specific object
at run time
• parameterized types
– define families of related types, where the type is a
parameter
– templates or "generic" programming
C++ classes
class thing {
public:
methods -- functions that define what operations can be
done on this kind of object
private:
variables and functions that implement the operations
};
4
C++ synopsis
• data abstraction with classes
– a class defines a type that can be used to
declare variables of that type,
control access to representation
class stack {
private: // this is the default
int stk[100];
int *sp; // next free place
public:
int push(int);
int pop();
};
5
Testing stk1.c
#include <stdio.h>
main()
{
stack s;
int i;
// stk2.c: constructors
class stack {
private:
int stk[100];
int *sp; // next free place
public:
stack(); // constructor
int push(int);
int pop();
};
stack::stack() { sp = stk; }
6
Testing stk2.c
main()
{
stack s1, s2;
int i;
7
Dynamic stack with new, delete
// stk3.c: new, destructors, delete; explicit size
class stack {
private:
int *stk; // allocated dynamically
int *sp; // next free place
public:
stack(); // constructor
stack(int n); // constructor
~stack(); // destructor
int push(int);
int pop();
};
stack::stack()
{
stk = new int[100]; sp = stk;
}
stack::stack(int n)
{
stk = new int[n]; sp = stk;
}
8
Inline definitions, default arguments
// stk4.c: inline definitions, default size
class stack {
int *stk; // allocated dynamically
int *sp; // next free place
public:
stack(int n);
~stack() { delete [ ] stk; }
int push(int n) { return *sp++ = n; }
int pop() { return *--sp; }
int top() { return sp[-1]; }
};
inline stack::stack(int n = 100)
{ // ^ default argument
stk = new int[n];
sp = stk;
}
main() {
stack s1(10), s2;
9
Change of representation
class stack {
private:
struct blk { // private to this class
int n;
blk *nb;
blk(int sz = 0, blk *next = 0);
};
blk *sp; // top == head of the list
public:
stack(int = 0) { sp = 0; }
~stack() { while (sp) pop(); }
int push(int n);
int pop();
int top() { return sp->n; }
};
int stack::push(int n)
{
blk *bp = new blk(n, sp);
sp = bp;
return n;
}
int stack::pop()
{
blk *bp = sp;
int n = sp->n;
sp = sp->nb;
delete bp;
return n;
}
10
Aside on implementation
11
Where are we now?
12