1 of 10

The C++ Language

Mrs. K. NITHYANANDAKUMARI

Assistant Professor of Computer Science

C.P.A. College

Bodinayakanur

2 of 10

Overview of ‘C++’�

  • Bjarne Stroupstrup, the language’s creator
  • C++ was designed to provide Simula’s facilities for program organization together with C’s efficiency and flexibility for systems programming.
  • Modern C and C++ are siblings

3 of 10

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 of 10

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 of 10

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 of 10

A Simple C++ Program

#include <cstdlib>

#include <iostream>

using namespace std;

int main ( ) {

intx, y;

cout << “Please enter two numbers:”;

cin >> x >> y;

int sum = x + y;

cout << “Their sum is “ << sum << endl;

return EXIT_SUCCESS;

}

7 of 10

The #include Directive

  • The first two lines:

#include <iostream>

#include <cstdlib>

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 of 10

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 std::cin;

using std::cout;

using std::string;

using std::getline;

9 of 10

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 of 10

THANK YOU

www.myassignmenthelp.net