Upload
nsavi16edu
View
237
Download
0
Embed Size (px)
Citation preview
8/10/2019 1.introduction-c++ programming features
1/38
PROGRAMMING AND DATA
STRUCTURES II
CS6301
8/10/2019 1.introduction-c++ programming features
2/38
UNIT I - OBJECT ORIENTED
PROGRAMMING FUNDAMENTALS
C++ Programming features - Data Abstraction -
Encapsulation - class - object - constructors
static membersconstant membersmember functionspointersreferences -
Role of this pointerStorage classes
function as arguments.
8/10/2019 1.introduction-c++ programming features
3/38
Introduction
8/10/2019 1.introduction-c++ programming features
4/38
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.
8/10/2019 1.introduction-c++ programming features
5/38
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 BellLabs
C++ was developed in the early 1980s by Bjarne Stroustrup ofAT&T Bell Labs.
8/10/2019 1.introduction-c++ programming features
6/38
History of C++
Bjarne StroustrupThe 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 andTelegraph Company) Bell LaboratoriesFellow, an AT&T Fellow and ACMFellow.
Recipient of the 1993 ACM Grace Murray
Hopper award.
8/10/2019 1.introduction-c++ programming features
7/38
The chart of the
first appearances
of high-level computerlanguages
History of C++Fortran
Algol60
PL/I
CPL
BCPL
C
Pascal
Modula-2
Simula 67
Lisp
ML
Clu
Smalltalk-80
Ada Beta
ANSI C
C++arm
Eiffel
Objective C
Modula-3
CLOS
Ada9X C++std
C++
Algol 68
1960
1970
1980
1990
1960
1970
1980
1990
C with Classes
8/10/2019 1.introduction-c++ programming features
8/38
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)
C++ Timeline
History of C++
8/10/2019 1.introduction-c++ programming features
9/38
Nov 1st USENIX C++ conference (Santa Fe, NM)
Dec 1st GNU C++ release (1.13)
1988 Jan 1st Oregon Software C++ releaseJune 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)
C++ Timeline
History of C++
1987 Feb Cfront Release 1.2
8/10/2019 1.introduction-c++ programming features
10/38
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)
C++ Timeline
History of C++
1991 June The C+ + Programming Language (second edition) [2nd]
8/10/2019 1.introduction-c++ programming features
11/38
Difference between C and C++.doc
http://localhost/var/www/apps/conversion/tmp/scratch_10/Difference%20between%20C%20and%20C++.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/Difference%20between%20C%20and%20C++.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/Difference%20between%20C%20and%20C++.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/Difference%20between%20C%20and%20C++.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/Difference%20between%20C%20and%20C++.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/Difference%20between%20C%20and%20C++.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/Difference%20between%20C%20and%20C++.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/Difference%20between%20C%20and%20C++.doc8/10/2019 1.introduction-c++ programming features
12/38
High level languages
Algorithms and functions to be
written without requiring detailedknowledge 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 interruptinterfaces to the hardware.
Provides more control and
efficiency for the program and
can be good for applicationswhich need high speed execution
more difficult to read and
program
8/10/2019 1.introduction-c++ programming features
13/38
Top down approach
Designing the
main module(i.e main
function)
Then decide what all othermodules 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 themodules to be include..
Eg:- : we can design any no
of classes and in main onlyrequired classes and their
functions can be used
8/10/2019 1.introduction-c++ programming features
14/38
diff bw pop & oop.doc
http://localhost/var/www/apps/conversion/tmp/scratch_10/diff%20bw%20pop%20&%20oop.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/diff%20bw%20pop%20&%20oop.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/diff%20bw%20pop%20&%20oop.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/diff%20bw%20pop%20&%20oop.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/diff%20bw%20pop%20&%20oop.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/diff%20bw%20pop%20&%20oop.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/diff%20bw%20pop%20&%20oop.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/diff%20bw%20pop%20&%20oop.doc8/10/2019 1.introduction-c++ programming features
15/38
Basics of a Typical C++ Environment
Phases of C++ Programs:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute
Loader
Primary
Memory
Program is created in
the editor and stored
on disk.
Preprocessor program
processes the code.
Loader puts program
in memory.
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.
Compiler
Compiler creates
object code and stores
it on disk.
Linker links the object
code with the libraries,creates a.outand
stores it on disk
Editor
Preprocessor
Linker
CPU
Primary
Memory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
Disk
Disk
8/10/2019 1.introduction-c++ programming features
16/38
Creating Source file
Turbo C++ and Borland C++ use .cpp as
extension
Zortech system uses .cxx
UNIX AT&T version uses .C and .cc
8/10/2019 1.introduction-c++ programming features
17/38
Compiling & Linking
In UNIX AT&T
To compile single file To compile CC filename.C
The compiler would produce the object file asfilename.o
Then automatically link with the library function to
produce executable file
Default executable file is a.out
8/10/2019 1.introduction-c++ programming features
18/38
To compile multiple file CC filename1.C filename2.o
Compiles only filename1.c and link with previouslycompiled 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
8/10/2019 1.introduction-c++ programming features
19/38
Simple C++ Program
\*This is
an example*/
\\simple c++ program
#include
int main()
{
// Declarations// Statements
return 0;
}
\*.*/ Multiline commentline
\\ .. Single comment line
8/10/2019 1.introduction-c++ programming features
20/38
Simple C++ Program
#include
int main()
{
// Declarations
// Statements
return 0;
}
Compiler directive: Tellsthe compiler what to dobefore compiling
This one includes source
code from another file
8/10/2019 1.introduction-c++ programming features
21/38
#include
In C++, a stream is a sequence of characters associated with aninput device, or an output device, or a disk file.
Class iostreamdefines objectcinas the stream associatedwith input device (keyboard).
Class iostream defines object cout as the streamassociated with output device (screen).
Classiostreamalso defines: input operator >>(extract from input stream);
output operator
8/10/2019 1.introduction-c++ programming features
22/38
Input Operator => cin
The identifier cin is a predefined object in c++
that corresponds to the standard input
stream.
Syntax:- cin>>variable;
8/10/2019 1.introduction-c++ programming features
23/38
Output operator => cout
The identifier cout is predefined object that
represents the standard output stream in c++.
Syntax:- cout
8/10/2019 1.introduction-c++ programming features
24/38
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
8/10/2019 1.introduction-c++ programming features
25/38
To display the output in a single line
the output is sum= ,Average=
Cascading Input operator:
8/10/2019 1.introduction-c++ programming features
26/38
Header Files
list of header files.doc
http://localhost/var/www/apps/conversion/tmp/scratch_10/list%20of%20header%20files.dochttp://localhost/var/www/apps/conversion/tmp/scratch_10/list%20of%20header%20files.doc8/10/2019 1.introduction-c++ programming features
27/38
Simple C++ Program
#include
using namespace std;
int main()
{
// Declarations
// Statements
return 0;
}
8/10/2019 1.introduction-c++ programming features
28/38
using namespace std;
prefixing the name with std
#include
int main()
{
std::cout
8/10/2019 1.introduction-c++ programming features
29/38
Simple C++ Program#include
int main()
{
// Declarations
// Statements
return 0;
}
Main function
8/10/2019 1.introduction-c++ programming features
30/38
Simple C++ Program#include
int main()
{
// Declarations
// Statements
return 0;
}
Header for mainfunction
In C++, main returns aninteger type value to the
operating system. So return type for
main() is explicitlyspecified as int.
Therefore every main()in c++ should end with a
return 0 statement.
8/10/2019 1.introduction-c++ programming features
31/38
Simple C++ Program#include
int main()
{
// Declarations
// Statements
return 0;
}
Braces enclose
the body of the
function
They represent
the start and end
of the function
8/10/2019 1.introduction-c++ programming features
32/38
Simple C++ Program#include
int main()
{
// Declarations
// Statements
return 0;
}
Declarations and
statements
Main body of
function (or main
part)
// represents the
start of a
comment
8/10/2019 1.introduction-c++ programming features
33/38
Simple C++ Program#include
int main()
{
// Declarations
// Statements
return 0;
}
Return statement
specifies thevalue the functionreturns
All (almost)declarations andstatements endwith a semi-colon;
8/10/2019 1.introduction-c++ programming features
34/38
Simple C++ Program#include
int main()
{
// Declarations
// Statements
return 0;
}
This
program
doesnt do
anything!
8/10/2019 1.introduction-c++ programming features
35/38
Sample 1#include
void main()
{
int number;
cout number;
cout
8/10/2019 1.introduction-c++ programming features
36/38
Sample 1#include
void main()
{
int number;
cout number;
cout
8/10/2019 1.introduction-c++ programming features
37/38
Sample 1#include
void main()
{
int number;
cout number;
cout
8/10/2019 1.introduction-c++ programming features
38/38
Sample 1#include
void main()
{
int number;
cout number;
cout