Learn C++ Programming Language - PowerPoint PPT Presentation

About This Presentation
Title:

Learn C++ Programming Language

Description:

Start Developing Software By Learning C++ With www.myassignmenthelp.net – PowerPoint PPT presentation

Number of Views:5326

less

Transcript and Presenter's Notes

Title: Learn C++ Programming Language


1
  • The C Language

2
Overview of C
  • Bjarne Stroupstrup, the languages creator
  • C was designed to provide Simulas facilities
    for program organization together with Cs
    efficiency and flexibility for systems
    programming.
  • Modern C and C are siblings

3
Outline
  • C basic features
  • Programming paradigm and statement syntax
  • Class definitions
  • Data members, methods, constructor, destructor
  • Pointers, arrays, and strings
  • Parameter passing in functions
  • Templates
  • Friend
  • Operator overloading
  • I/O streams
  • An example on file copy
  • Makefile

4
C Features
  • Classes
  • User-defined types
  • Operator overloading
  • Attach different meaning to expressions such as a
    b
  • References
  • Pass-by-reference function arguments
  • Virtual Functions
  • Dispatched depending on type at run time
  • Templates
  • Macro-like polymorphism for containers (e.g.,
    arrays)
  • Exceptions

5
Compiling and Linking
  • A C program consists of one or more source
    files.
  • Source files contain function and class
    declarations and definitions.
  • Files that contain only declarations are
    incorporated into the source files that need them
    when they are compiled.
  • Thus they are called include files.
  • Files that contain definitions are translated by
    the compiler into an intermediate form called
    object files.
  • One or more object files are combined with to
    form the executable file by the linker.

6
A Simple C Program
  • include ltcstdlibgt
  • include ltiostreamgt
  • using namespace std
  • int main ( )
  • intx, y
  • cout ltlt Please enter two numbers
  • cin gtgt x gtgt y
  • int sum x y
  • cout ltlt Their sum is ltlt sum ltlt endl
  • return EXIT_SUCCESS

7
The include Directive
  • The first two lines
  • include ltiostreamgt
  • include ltcstdlibgt
  • incorporate the declarations of the
    iostream and cstdlib libraries into the source
    code.
  • If your program is going to use a member of the
    standard library, the appropriate header file
    must be included at the beginning of the source
    code file.

8
The using Statement
  • The line
  • using namespace std
  • tells the compiler to make all names in the
    predefined namespace std available.
  • The C standard library is defined within this
    namespace.
  • Incorporating the statement
  • using namespace std
  • is an easy way to get access to the standard
    library.
  • But, it can lead to complications in larger
    programs.
  • This is done with individual using declarations.
  • using stdcin
  • using stdcout
  • using stdstring
  • using stdgetline

9
Compiling and Executing
  • The command to compile is dependent upon the
    compiler and operating system.
  • For the gcc compiler (popular on Linux) the
    command would be
  • g -o HelloWorld HelloWorld.cpp
  • For the Microsoft compiler the command would be
  • cl /EHsc HelloWorld.cpp
  • To execute the program you would then issue the
    command
  • HelloWorld

10
C Data Type
  • Basic Java types such as int, double, char have
    C counterparts of the same name, but there are
    a few differences
  • Boolean is bool in C. In C, 0 means false
    and anything else means true.
  • C has a string class (use string library) and
    character arrays (but they behave differently).

11
Constants
  • Numeric Constants
  • 1234 is an int
  • 1234U or 1234u is an unsigned int
  • 1234L or 1234l is a long
  • 1234UL or 1234ul is an unsigned long
  • 1.234 is a double
  • 1.234F or 1.234f is a float
  • 1.234L or 1.234l is a long double.
  • Character Constants
  • The form 'c' is a character constant.
  • The form '\xhh' is a character constant, where hh
    is a hexadecimal digit, and hh is between 00 and
    7F.
  • The form '\x' where \x is one of the following is
    a character constant.
  • String Constants
  • The form "sequence of characters where sequence
    of characters does not include " is called a
    string constant.
  • Note escape sequences may appear in the sequence
    of characters.
  • String constants are stored in the computer as
    arrays of characters followed by a '\0'.

12
Operators
  • Bitwise Operators
  • (complement)
  • (bitwise and)
  • (bitwise exclusive or)
  • (bitwise or)
  • ltlt (shift left)
  • gtgt (shift right)
  • Assignment Operators
  • , -, , /, , , , , ltlt, gtgt
  • Other Operators
  • (scope resolution)
  • ? (conditional expression)
  • stream ltlt var
  • stream gtgt exp

13
Increment and Decrement
  • Prefix
  • x
  • x is replaced by x1, and the value is x1
  • --x
  • x is replaced by x-1, and the value is x-1
  • Postfix
  • x
  • x is replaced by x1, but the value is x
  • x--
  • x is replaced by x-1, but the value is x

14
Object-Oriented Concept (C)
  • Objects of the program interact by sending
    messages to each other

15
Basic C
  • Inherit all C syntax
  • Primitive data types
  • Supported data types int, long, short, float,
    double, char, bool, and enum
  • The size of data types is platform-dependent
  • Basic expression syntax
  • Defining the usual arithmetic and logical
    operations such as , -, /, , , , !,
    and
  • Defining bit-wise operations, such as , , and
  • Basic statement syntax
  • If-else, for, while, and do-while

16
Basic C (cont)
  • Add a new comment mark
  • // For 1 line comment
  • / / for a group of line comment
  • New data type
  • Reference data type . Much likes pointer
  • int ix / ix is "real" variable /
  • int rx ix / rx is "alias" for ix /
  • ix 1 / also rx 1 /
  • rx 2 / also ix 2 /
  • const support for constant declaration, just
    likes C

17
Class Definitions
  • A C class consists of data members and methods
    (member functions).
  • class IntCell
  • public
  • explicit IntCell( int initialValue 0 )
  • storedValue( initialValue )
  • int read( ) const
  • return storedValue
  • void write( int x )
  • storedValue x
  • private
  • int storedValue

18
Information Hiding in C
  • Two labels public and private
  • Determine visibility of class members
  • A member that is public may be accessed by any
    method in any class
  • A member that is private may only be accessed by
    methods in its class
  • Information hiding
  • Data members are declared private, thus
    restricting access to internal details of the
    class
  • Methods intended for general use are made public

19
Constructors
  • A constructor is a special method that describes
    how an instance of the class (called object) is
    constructed
  • Whenever an instance of the class is created, its
    constructor is called.
  • C provides a default constructor for each
    class, which is a constructor with no parameters.
    But, one can define multiple constructors for the
    same class, and may even redefine the default
    constructor

20
Destructor
  • A destructor is called when an object is deleted
    either implicitly, or explicitly (using the
    delete operation)
  • The destructor is called whenever an object goes
    out of scope or is subjected to a delete.
  • Typically, the destructor is used to free up any
    resources that were allocated during the use of
    the object
  • C provides a default destructor for each class
  • The default simply applies the destructor on each
    data member. But we can redefine the destructor
    of a class. A C class can have only one
    destructor.
  • One can redefine the destructor of a class.
  • A C class can have only one destructor

21
Constructor and Destructor
  • class Point
  • private
  • int _x, _y
  • public
  • Point()
  • _x _y 0
  • Point(const int x, const int y)
  • Point(const Point from)
  • Point() void
  • void setX(const int val)
  • void setY(const int val)
  • int getX() return _x
  • int getY() return _y

22
Constructor and Destructor
  • PointPoint(const int x, const int y) _x(x),
    _y(y)
  • PointPoint(const Point from)
  • _x from._x
  • _y from._y
  • PointPoint(void)
  • / nothing to do /

23
C Operator Overloading
  • class Complex
  • ...
  • public
  • ...
  • Complex operator (const Complex op)
  • double real _real op._real,
  • imag _imag op._imag
  • return(Complex(real, imag))
  • ...
  • In this case, we have made operator a member of
    class Complex. An expression of the form
  • c a b
  • is translated into a method call
  • c a.operator (a, b)

24
Operator Overloading
  • The overloaded operator may not be a member of a
    class It can rather defined outside the class as
    a normal overloaded function. For example, we
    could define operator in this way
  • class Complex
  • ...
  • public
  • ...
  • double real() return _real
  • double imag() return _imag
  • // No need to define operator here!
  • Complex operator (Complex op1, Complex op2)
  • double real op1.real() op2.real(),
  • imag op1.imag() op2.imag()
  • return(Complex(real, imag))

25
Friend
  • We can define functions or classes to be friends
    of a class to allow them direct access to its
    private data members
  • class Complex
  • ...
  • public
  • ...
  • friend Complex operator (
  • const Complex ,
  • const Complex
  • )
  • Complex operator (const Complex op1, const
    Complex op2)
  • double real op1._real op2._real,
  • imag op1._imag op2._imag
  • return(Complex(real, imag))

26
Standard Input/Output Streams
  • Stream is a sequence of characters
  • Working with cin and cout
  • Streams convert internal representations to
    character streams
  • gtgt input operator (extractor)
  • ltlt output operator (inserter)

27
  • Thank You
Write a Comment
User Comments (0)
About PowerShow.com