17

Click here to load reader

COM 1013 C++ & perl

Embed Size (px)

DESCRIPTION

:)

Citation preview

Page 1: COM 1013 C++ & perl

CHAPTER : C++ AND PERL

COM 1013

Page 2: COM 1013 C++ & perl

C++ ( SEE-PLUS-PLUS )

Developed in the 1980s

By Bjatne Sroustrup at Bell Laborataries

C++ is an object-oriented programming language

that is an extention of the C programming language

Includes all the elements of the C language

Plus it has additional features for working with

object, classes, events, and other, and other object-

oriented concepts.

Page 3: COM 1013 C++ & perl

SAMPLE C++ PROGRAM// portion of C++ program that allows users to create// a new zip code from a string or a number and expand// zip codes, as appropriate, to a 10-digit number

ZipC : : ZipC ( const unsigigned long zipnum ){

Ostringstream strInt ;strInt << zipnum ;code = strnt. str( ) ;

}const string ZipC : : get Code ( ){

return code ;}Void ZipC : : setCode ( const string newCode )}

code = newCode ;}Void ZipC :: expand ( const string suffix )}

if (code.length ( ) = = 5 && // small size?suffix.length ( ) = = 4 ) // length ok ?

{code += “ – ” ;code . Append ( suffix ) ;}

}

Page 4: COM 1013 C++ & perl

CONTINUES……

Programmers commonly use C++ to develop

database and Web applications.

Much application software, such as word processing

and spreadsheet programs, also is written in C++

Although C++ is an outgrowth of the C programming

language , a programmer does not need C

programming Experience to be a successful C++

programmer.

Page 5: COM 1013 C++ & perl

COMPILING AND LINKING C++ EXAMPLE//This is a program to define variable

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#include < vcl . h >

#pragma hdrstop

#include < oistrem . h >

#include < tchar . h >

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#pragma argsused

Int_tmain ( int argc , TCHAR * argv [ ] )

{

Int No 1, No 2, Total, Total 2;

Std : : cout << “ Enter 1 st number : “ ;

Std : : cin >> No 1 ;

Std : : cout << “ Enter 2 nd number : “ ;

Std : : cin >> No 2 ;

Total = No 1 + No 2 ;

Std : : cout << “ Total sum : “ << Total << endl ;

Total 2 = No 1 – No 2 ;

Std : : cout << “total minus : “<< Total 2<< endl ;

System (“pause “) ;

return 0 ;

}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- -

Page 6: COM 1013 C++ & perl

OUTPUT

Enter 1 st number : 21

Enter 2 nd number : 8

Total sum : 29

Total minus : 13

Press any key to continue…

Page 7: COM 1013 C++ & perl

BASIC

#include <iostream>

using namespace std;

main( ) //is where program execution begins.

int main( )

{

cout << "Hello World"; // prints Hello World

return 0;

}

>> output : Hello World

Page 8: COM 1013 C++ & perl

#include <iostream>

using namespace std;

int main ( )

{

// Local variable declaration:

int a; int b;

int c;

// actual initialization

a = 10;

b = 20;

c = a + b;

cout << c;

return 0;

}

>>output : 30

Page 9: COM 1013 C++ & perl

#include <iostream>

// Function declaration

void func(void);

static int count = 10; /* Global variable */

main( )

{

while(count--)

{

func( );

}

return 0;

}

// Function definition

void func( void )

{

static int i = 5; // local static variable

A++;

std::cout << “A is " << A ;

std::cout << " and count is " << count << std::endl;

}

Page 10: COM 1013 C++ & perl

>> OUTPUT

A is 6 and count is 9

A is 7 and count is 8

A is 8 and count is 7

A is 9 and count is 6

A is 10 and count is 5

A is 11 and count is 4

A is 12 and count is 3

A is 13 and count is 2

A is 14 and count is 1

A is 15 and count is 0

Page 11: COM 1013 C++ & perl

#include <iostream>

using namespace std;

#include <iomanip>

using std::setw;

int main ( )

{

int n[ 10 ]; // n is an array of 10 integers

// initialize elements of array n to 0

for ( int i = 0; i < 10; i++ )

{

n[ i ] = i + 100; // set element at location i to i + 100

}

cout << "Element" << setw( 13 ) << "Value" << endl;

// output each array element's value

for ( int j = 0; j < 10; j++ )

{

cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;

}

return 0;

}

Page 12: COM 1013 C++ & perl

>> OUTPUT

Element Value

0 100

1 101

2 102

3 103

4 104

5 105

6 106

7 107

8 108

9 109

Page 13: COM 1013 C++ & perl

WHAT IS PERL?

1. Interpreted, dynamic programming

language

2. High-level language

• Functional

• Procedural

• Object-oriented

3. Extensible library modules

Page 14: COM 1013 C++ & perl

LIMITATIONS OF PERL

1. Compiled on each run

2. Large FP calculations not as fast or as easy

as FORTRAN or C/C++

3. No contiguous multi-dimensional arrays.

Complex data structures have memory

management overhead

Page 15: COM 1013 C++ & perl

SWIG (SIMPLIFIED WRAPPER AND INTERFACE

GENERATOR)

Wraps C++ with Perl or other computer

languages

Generates the connecting wrapper code for both

C++ and Perl (or other target language).

C++ code then compiled and linked into a library

Perl scripts call Perl wrapper, which in turn calls

C++ library

Page 16: COM 1013 C++ & perl

SUGGESTIONS WRAPPING C++ WITH PERL

Use built-in or STL types arguments wherever

possible

Otherwise use custom built C++ data classes

consisting of above types

Link C++ code as shared objects if possible

Place exception handling first in interface file

Use SWIG_exception_fail

Page 17: COM 1013 C++ & perl

THE END…...