Vous êtes sur la page 1sur 38

PROGRAMMING AND DATA

STRUCTURES II
CS6301

UNIT I - OBJECT ORIENTED


PROGRAMMING FUNDAMENTALS
C++ Programming features - Data Abstraction Encapsulation - class - object - constructors
static members constant members
member functions pointers references Role of this pointer Storage classes
function as arguments.

Introduction

General reasons to name


Programming languages as B,C,C++
why b language is called b?
B was called B because it was meant to be a stripped down version of the
BCPL language (Basic Combined Programming Language), so the B just
stands for basic.
Why is c language called C?
C is called C because it is based on the B programming language (and
naturally, B is followed by C).
Why is the language called C++..?
C++ has the double '+' on it because one of the new additions was the ++
operator (instead of writing x=x+1, you could write x++). The pluses also
denote that it is an addition to C.

What is C++

C++ is a compiled, object-oriented language Originally called C


with classes .
It is the successor to C, a procedural language
(the ++ is called the successor operator in C++)
Or
It is a Superset of C.
Or
The name C++ is based on Cs increment operator (++)
Indicating that C++ is an enhanced version of C
C was developed in the 1970s by Dennis Ritchie of AT&T Bell
Labs
C++ was developed in the early 1980s by Bjarne Stroustrup of
AT&T Bell Labs.

History of C++
Bjarne Stroustrup
The creator of C++ language and its
first implementation.
Born in Aarhus Denmark, 1950.
Cand.Scient. (Math. and C.S.), 1975,
University of Aarhus, Denmark.
Ph.D. (Computer Science), 1979,
Cambridge University, England
The head of AT&T Lab's Large-scale
Programming Research department,
an AT&T(American Telephone and
Telegraph Company) Bell Laboratories
Fellow, an AT&T Fellow and ACM
Fellow.
Recipient of the 1993 ACM Grace Murray

Hopper award.

History of C++
Fortran
Lisp

Algol60

1960

1960

CPL

The chart of the


first appearances
of high-level computer
languages

PL/I
BCPL
Simula 67
Pascal

1970

1970

ML
Algol 68
Modula-2

1980

Ada

Clu
C with Classes

Beta

C++
ANSI C

Smalltalk-80

1980

Objective C
Eiffel
CLOS

1990

C++arm
Modula-3

Ada9X

C++std

1990

History of C++
C++ Timeline
1979 May Work on C with Classes starts
Oct

1st C with Classes implementation in use

1980 Apr

1st internal Bell Labs paper on C with Classes [Stroustrup, 1980]

1982 Jan

1st external paper on C with Classes [Stroustrup, 1982]

1983 Aug 1st C++ implementation in use


Dec

C++ named

1984 Jan

1st C++ manual

1985 Feb

1st external C++ release (Release E)

Oct

Cfront Release 1.0 (first commercial release)

Oct

The C++ Programming Language [Stroustrnp, 1986]

1986 Aug The "what is paper" [Stroustrup, 1986b]


Sep 1st OOPSLA conference (start of OO hype centered on Smalltalk)
Nov 1st commercial Cfront PC port (Cfront 1.1, Glockenspiel)

History of C++
C++ Timeline
1987 Feb Cfront Release 1.2
Nov 1st USENIX C++ conference (Santa Fe, NM)
Dec 1st GNU C++ release (1.13)
1988 Jan

1st Oregon Software C++ release

June 1st Zortech C++ release


Oct

1st USENIX C++ implementers workshop (Estes Park, CO)

1989 June Cfront Release 2.0


Dec ANSI X3JI6 organizational meeting (Washington, DC)
1990 Mar 1st ANSI X3J 16 technical meeting (Somerset, NJ)
May 1st Borland C++ release
May The Annotated C++ Reference Manual [ARM]
July Templates accepted (Seattle, WA)
Nov Exceptions accepted (Palo Alto, CA)

History of C++
C++ Timeline
1991 June The C+ + Programming Language (second edition) [2nd]
June 1st ISO WG21 meeting (Land, Sweden)

Oct Cfront Release 3,0 (including templates)


1992 Feb 1st DEC C++ release (including templates and exceptions)
Mar 1st Microsoft C++ release
May 1st IBM C++ release (including templates and exceptions)

1993 Mar Run-time type identification accepted (Portland, OR)


July Namespaces accepted (Munich, Germany)
1994 Aug ANSI/ISO Committee Draft registered
1998 Sep The ANSI/ISO standardization of C++
The C++ Programming Language (3rd edition)
2000

The C++ Programming Language (special edition)

Difference between C and C++.doc

High level languages


Algorithms and functions to be
written without requiring detailed
knowledge of the hardware used in
the computing platform.
The compiler provides interface
transparently for the programmer.

Easier to read and program in, but


require much more memory due to
the generality necessitated in their
compilers.

Low level languages


Require more involvement with
the actual register and interrupt
interfaces to the hardware.
Provides more control and
efficiency for the program and
can be good for applications
which need high speed execution
more difficult to read and
program

Top down approach

Designing the
main module(i.e main
function)
Then decide what all other
modules to be include and
then we will design all
other sub modules
Eg:- c

Bottom up approach

Design all the sub modules


related to application
Then design main module
and then decide what are the
modules to be include..
Eg:- : we can design any no
of classes and in main only
required classes and their
functions can be used

diff bw pop & oop.doc

Basics of a Typical C++ Environment


Editor

Preprocessor

Phases of C++ Programs:


1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute

Compiler

Linker

Disk

Program is created in
the editor and stored
on disk.

Disk

Preprocessor program
processes the code.

Disk

Compiler creates
object code and stores
it on disk.

Disk
Primary
Memory

Linker links the object


code with the libraries,
creates a.out and
stores it on disk

Loader

Disk

Loader puts program


in memory.
..
..
..

Primary
Memory

CPU

..
..
..

CPU takes each


instruction and
executes it, possibly
storing new data
values as the program
executes.

Creating Source file


Turbo C++ and Borland C++ use .cpp as
extension
Zortech system uses .cxx
UNIX AT&T version uses .C and .cc

Compiling & Linking


In UNIX AT&T
To compile single file
To compile CC filename.C
The compiler would produce the object file as
filename.o
Then automatically link with the library function to
produce executable file
Default executable file is a.out

To compile multiple file


CC filename1.C filename2.o
Compiles only filename1.c and link with previously
compiled filename2.o
It is useful only when one of the files needs to be
modified.

Turbo C++ and Borland C++


Compile using compile option or ctrl+f9
Execute using Run option or alt+f9

Visual C++
Using menu choices and buttons

Simple C++ Program


\*This is
an example*/
\\simple c++ program
#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}

\*.*/ Multiline comment


line
\\ .. Single comment line

Simple C++ Program


#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}

Compiler directive: Tells


the compiler what to do
before compiling
This one includes source
code from another file

#include <iostream>
In C++, a stream is a sequence of characters associated with an
input device, or an output device, or a disk file.
Class iostream defines object cin as the stream associated
with input device (keyboard).
Class iostream defines object cout as the stream
associated with output device (screen).
Class iostream also defines:
input operator >> (extract from input stream);
output operator << (insert to output stream).

Input Operator => cin


The identifier cin is a predefined object in c++
that corresponds to the standard input
stream.
Syntax:- cin>>variable;

Output operator => cout


The identifier cout is predefined object that
represents the standard output stream in c++.
Syntax:- cout<<string;

Cascading of I/O operators


First sends the string sum= to cout and then
sends the value of sum
Then sends the newline character

The above statement provides two lines of


output

To display the output in a single line

the output is sum= ,Average=


Cascading Input operator:

Header Files
list of header files.doc

Simple C++ Program


#include <iostream.h>
using namespace std;
int main()
{
// Declarations
// Statements
return 0;
}

using namespace std;


prefixing the name with std

using namespace std before


main

#include <iostream.h>
int main()
{
std::cout<<"Hello, world!\n";
}
Namespaces.doc

#include <iostream.h>
using namespace std;
int main()
{
cout<<"Hello, world!\n";
}

Simple C++ Program


#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}

Main function

Simple C++ Program


#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}

Header for main


function
In C++, main returns an
integer type value to the
operating system.
So return type for
main() is explicitly
specified as int.
Therefore every main()
in c++ should end with a
return 0 statement.

Simple C++ Program


#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}

Braces enclose
the body of the
function

They represent
the start and end
of the function

Simple C++ Program


#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}

Declarations and
statements

Main body of
function (or main
part)

// represents the
start of a
comment

Simple C++ Program


#include <iostream.h>

int main()
{
// Declarations
// Statements
return 0;
}

Return statement
specifies the
value the function
returns
All (almost)
declarations and
statements end
with a semi-colon
;

Simple C++ Program


#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}

This
program
doesnt do
anything!

Sample 1
#include <iostream.h>
void main()
{
int number;
cout << Enter a number <<
endl;
cin >> number;
cout << You entered: <<
number << endl;
}

Variable
declaration

The identifier
number is
declared as being
of data type int,
or integer

Sample 1
#include <iostream.h>
void main()
{
int number;
cout << Enter a number <<
endl;
cin >> number;
cout << You entered: <<
number << endl;
}

cout
the output
statement for C++
Note the direction
of <<
endl represents
an end-of-line

Sample 1
#include <iostream.h>
void main()
{
int number;
cout << Enter a number <<
endl;
cin >> number;
cout << You entered: <<
number << endl;
}

cin
the input
statement
for C++
Note the
direction of
>>

Sample 1
#include <iostream.h>
void main()
{
int number;
cout << Enter a number <<
endl;
cin >> number;
cout << You entered: <<
number << endl;
}

Did you copy


this down?
You had
better, since
this will be the
first program
youll try!

Vous aimerez peut-être aussi