15
C++ Language Presentation By Pichai Sodsai 49541170 Software & Knowledge Engineering

C++ Language Presentation

  • Upload
    xena

  • View
    69

  • Download
    0

Embed Size (px)

DESCRIPTION

C++ Language Presentation. By Pichai Sodsai 49541170 Software & Knowledge Engineering. C++ Language Background. C++ was written by Bjarne Stroustrup at Bell Labs during 1983-1985. C++ is an extension of C. - PowerPoint PPT Presentation

Citation preview

Page 1: C++ Language Presentation

C++ LanguagePresentation

By

Pichai Sodsai 49541170Software & Knowledge

Engineering

Page 2: C++ Language Presentation

C++ LanguageBackground

C++ was written by Bjarne Stroustrup at Bell Labs during 1983-1985. C++ is an extension of C.

Bjarne Stroustrup added features to C and formed what he called "C with Classes". He had combined the Simula's use of classes and object-oriented features with the power and efficiency of C. The term C++ was first used in 1983.

Page 3: C++ Language Presentation

BackgroundC++ was designed for the UNIX system

environment. With C++ programmers could improve the quality of code they produced and reusable code was easier to write.

C++ is one of the most successful programming language and programmers use C++ to produce many kind of programs because of the OOP features and the efficiency of code.

Page 4: C++ Language Presentation

Contribution to Programming Languages

C++ supports object-oriented programming features.

C++ provides enhanced error handling capabilities such as exception handling.

C++ provides a means for identifying an object's type at runtime it’s called Run-Time Type Identification (RTTI).

Page 5: C++ Language Presentation

Language Structure and Syntax The Function main

C++ programs begin execution with a function main. The smallest C++ program is shown below. It doesn’t do anything, but it’s syntactically legal C++.

int main(){

return 0;}

The return value is passed back to the “system,” a nonzero value indicates some kind of failure. The function main can have parameters, these are so-called command-line parameters, passed to the program when the function is run.

Page 6: C++ Language Presentation

Language Structure and SyntaxVariable Definition and Assignment

Variables are defined when storage is allocated. Variable definitions include a type and a name for the variable. An initial value may optionally be supplied. The C++ statements

below define an int variable without an initial value, a double variable with an initial value, two string variables with initial values and one without.

int minimum; double xcoord = 0.0; string first = "hello", second, third="goodbye";

Page 7: C++ Language Presentation

Language Structure and SyntaxVariable Definition and Assignment

Variables that are instances of a class, as opposed to built-in types like int or bool, are constructed when they are defined. Typically the syntax used for construction looks like a function call, but the assignment operator can be used when variables are defined as in the second line below. This statement constructs a variable only, it does not construct then assign, although the syntax looks like this is what happens.

Dice cube(6); // construct a 6-sided Dice Dice dodo = 12; // construct a 12-sided Dice Date usb(1,1,2007); // construct Jan 1, 2007

It’s legal to use constructor syntax for built-in types too: int x(0); int y = 0; // both define int with value zero

Page 8: C++ Language Presentation

Language Structure and SyntaxControl Flow

We use most of the C++ statements that change execution flow in a program, but not all of them.

if ( condition ) statementif ( condition ) statement else statementswitch ( condition ) case/default statementswhile ( condition ) statementdo statement while ( condition )for ( init statement ; condition ; update expression )case constant expression : statementdefault : statementbreak;continue;return expression (expression is optional)

Page 9: C++ Language Presentation

Example Program//This is the example program that adds two numbers and show the result, the number come from user input.

#include "stdafx.h"#include <iostream>using std::endl;using std::cout;using std::cin;

double add(double num1, double num2); // function prototype

int main(){

double num1 = 0.0, num2 = 0.0;cout << "This is the test program to add two numbers." << endl;cout << "Please input two numbers:";cin >> num1 >> num2;cout << num1 << " add " << num2 << " = " << add(num1, num2) << endl;return 0;

}double add(double num1, double num2){

return num1 + num2; // return the result}

Page 10: C++ Language Presentation

Example Program#include "stdafx.h"#include <iostream>

These two statement are directives that add the contents of the file stdafx.h to this file in place of this #include directive. This is the standard way of adding the contents of .h source files to a .cpp source file a in a C++ program. #include<iostream> is the directive that adds the contents of one of the standard libraries for ISO/ANSI C++ to the source file. The <iostream> library defines facilities for basic I/O operations, and the one you used to add writes output to the command line. std::cout is the name of the standard output stream.

Page 11: C++ Language Presentation

Example Programusing std::endl;

using std::cout;

using std::cin;

These are using declarations that tell the compiler that you intend to use the names endl , cout and cin from the namespace std without specifying the namespace name. The compiler will now assume that wherever you use the name cout in the source file subsequent to the first using declaration, you mean the cout that is defined in the standard library. The name cout represents the standard output stream that by default corresponds to the command line and the name endl represents the newline character and cin represents the standard input stream.

Page 12: C++ Language Presentation

Example Programdouble add(double num1, double num2);

At the point at which you use a function in a program, the compiler must know something about it to compile the function call. It needs enough information to be able to identify the function, and to verify that you are using it correctly. Unless you the definition of the function that you intend to use appears earlier in the same source file, you must declare the function using a statement called a function prototype.

Page 13: C++ Language Presentation

Example Programint main() // main function

{double num1 = 0.0, num2 = 0.0; // declare two double

variablescout << "This is the test program to add two numbers." << endl; // new linecout << "Please input two numbers:";cin >> num1 >> num2; // assign inputs from usercout << num1 << " add " << num2 << " = " // show result

<< add(num1, num2) // call add() function << endl; // new line

return 0; // return to the operating system}

Page 14: C++ Language Presentation

Example Programdouble add(double num1, double num2)

{return num1 + num2; // return the result

}

This function will add two numbers from parameters and return the result

Page 15: C++ Language Presentation

Referencehttp://www.hitmill.com/programming/cpp/cppHistory.htmhttp://www.cprogramming.com/reference/http://www.cplusplus.com