1 / 15

C++ Language Presentation

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.

winola
Download Presentation

C++ Language Presentation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ LanguagePresentation By Pichai Sodsai 49541170 Software & Knowledge Engineering

  2. 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.

  3. Background • C++ 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.

  4. 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).

  5. 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.

  6. 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";

  7. 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

  8. 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 ) statement if ( condition ) statement else statement switch ( condition ) case/default statements while ( condition ) statement do statement while ( condition ) for ( init statement ; condition ; update expression ) case constant expression : statement default : statement break; continue; return expression (expression is optional)

  9. Example Program //This is theexample 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 }

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

  11. Example Program using 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.

  12. Example Program double 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.

  13. Example Program int main() // main function { double num1 = 0.0, num2 = 0.0; // declare two double variables cout << "This is the test program to add two numbers." << endl; // new line cout << "Please input two numbers:"; cin >> num1 >> num2; // assign inputs from user cout << num1 << " add " << num2 << " = " // show result << add(num1, num2) // call add() function << endl; // new line return 0; // return to the operating system }

  14. Example Program double add(double num1, double num2) { return num1 + num2; // return the result } • This function will add two numbers from parameters and return the result

  15. Reference • http://www.hitmill.com/programming/cpp/cppHistory.htm • http://www.cprogramming.com/reference/ • http://www.cplusplus.com

More Related