me 515 mechatronics
play

ME 515 Mechatronics Introduction to C++ Asanga Ratnaweera - PDF document

ME 515 Mechatronics 11/9/2006 ME 515 Mechatronics Introduction to C++ Asanga Ratnaweera Department of Mechanical Engineering Faculty of Engineering University of Peradeniya Tel: 081239 (3627) Email: asangar@pdn.ac.lk Introduction to C++


  1. ME 515 Mechatronics 11/9/2006 ME 515 Mechatronics Introduction to C++ Asanga Ratnaweera Department of Mechanical Engineering Faculty of Engineering University of Peradeniya Tel: 081239 (3627) Email: asangar@pdn.ac.lk Introduction to C++ Programming C++ � Improves on many of C's features � Has object-oriented capabilities � Increases software quality and reusability � Developed by Bjarne Stroustrup at Bell Labs in � 1980 Called "C with classes" � C++ (increment operator) - enhanced version � of C Superset of C � Can use a C++ compiler to compile C � programs Gradually evolve the C programs to C++ � Sem 7 Asanga Ratnaweera, Department of 2 Mechanical Engineering 1

  2. ME 515 Mechatronics 11/9/2006 Introduction to C++ Programming Output in C++ � Included in iostream.h header file � cout - standard output stream (connected to screen) � << stream insertion operator ("put to") � cout << "hi"; � Puts "hi" cout , which prints it on the screen � End line � endl; � Stream manipulator - prints a newline and flushes output � buffer Some systems do not display output until "there is � enough text to be worthwhile" endl forces text to be displayed � Cascading � Can have multiple << or >> operators in a single statement � cout << "Hello " << "there" << endl; Sem 7 Asanga Ratnaweera, Department of 3 Mechanical Engineering Basic components of a simple C++ Program // Program: Display greetings Preprocessor /* Author(s): A.B.C. Dissanayake directives Date: 1/24/2001*/ Comments #include <iostream> Provides simple access using namespace std; Function named int main() { main() cout << "Hello world!" << endl; indicates return 0; start of } program Insertion Function Ends executions statement of main() which ends program Sem 7 Asanga Ratnaweera, Department of 4 Mechanical Engineering 2

  3. ME 515 Mechatronics 11/9/2006 Introduction to C++ Programming // Program : Program01.cpp // First program in C++. #include <iostream.h> // function main begins program execution int main() { cout << "Welcome to C++!”; return 0; // indicate that program ended successfully } // end function main Sem 7 Asanga Ratnaweera, Department of 5 Mechanical Engineering Visual C++ Editors Click new on file menu to create a new file � Select file tab on new dialogue box � Select C++ Source File � Click OK � Write the code � Save with the � extension cpp Sem 7 Asanga Ratnaweera, Department of 6 Mechanical Engineering 3

  4. ME 515 Mechatronics 11/9/2006 Visual C++ Editors To compile a program � Press Ctrl+F7 � To build a program � Press F7 � This will straight away compile and link � a program To execute a program � Press Ctrl+F5 � This will straight away compile and link � and execute a program To run a program � Press F5 � All these commands are available in � Build Minibar Sem 7 Asanga Ratnaweera, Department of 7 Mechanical Engineering Introduction to C++ Programming // Program : Program02.cpp // Printing a line with multiple statements. #include < <iostream iostream> > #include #include < < cstdio > > #include using namespace std; // function main begins program execution int main() { cout << "Welcome "; cout << "to C++!\n“<< endl; getchar(); // program waits until key board input is given return 0; // indicate that program ended successfully } // end function main Sem 7 Asanga Ratnaweera, Department of 8 Mechanical Engineering 4

  5. ME 515 Mechatronics 11/9/2006 Escape sequences Escape Description Sequence \n Newline. Position the screen cursor to the beginning of the next line. \t Horizontal tab. Move the screen cursor to the next tab stop. \r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. \a Alert. Sound the system bell. \\ Backslash. Used to print a backslash character. \" Double quote. Used to print a double quote character. Sem 7 Asanga Ratnaweera, Department of 9 Mechanical Engineering A Simple Program: Printing a Line of Text // Program : Program03.cpp // Printing multiple lines with a single statement #include < <iostream iostream> > #include #include #include < <cstdio> > using namespace std; // function main begins program execution int main() { cout <<" \t\t Welcome to C++! \n\n“ <<endl; // Tabs and new line cout <<“ My First Programme \a“ <<endl; // Alert sound cout <<“ \t\t\t\t\"Bye\“ "<<endl; // double quotation getchar(); return 0; // indicate that program ended successfully } // end function main Sem 7 Asanga Ratnaweera, Department of 10 Mechanical Engineering 5

  6. ME 515 Mechatronics 11/9/2006 C++ Data Types Characters char short Numbers without fractions int (integers) long float Numbers with fractions double (floating-point numbers) long double String Non-numbers Sem 7 Asanga Ratnaweera, Department of 11 Mechanical Engineering Type Name Bytes Other Name Range of Values system int * signed integer system dependent dependent system unsigned int * unsigned integer system dependent dependent char 1 signed character -128 to 127 unsigned char 1 unsigned character 0 to 255 short 2 signed short integer -32,768 to 32,767 unsigned short 2 unsigned short integer 0 to 65,535 -2,147,483,648 to long 4 signed long integer 2,147,483,647 unsigned long 4 unsigned long int 0 to 4,294,967,295 float 4 none 3.4E +/- 38 (7 digits) double 8 none 1.7E +/- 308 (15 digits) long double 10 none 1.2E +/- 4932 (19 digits) bool 1 bit boolean 0 to 1 * signed means the number can be positive or negative. Sem 7 Asanga Ratnaweera, Department of 12 Mechanical Engineering 6

  7. ME 515 Mechatronics 11/9/2006 C++ Programming Variables � Location in memory where value can be stored � Common data types � int - integer numbers � char - characters � double - floating point numbers � Declare variables with name and data type before use � int integer1; int integer2; int sum; Can declare several variables of same type in one � declaration Comma-separated list � int integer1, integer2, sum; Sem 7 Asanga Ratnaweera, Department of 13 Mechanical Engineering C++ Programming � Variables Variable names (identifier) � � Series of characters (letters, digits, underscores) � Cannot begin with a digit � Case sensitive � Should not be a keyword Sem 7 Asanga Ratnaweera, Department of 14 Mechanical Engineering 7

  8. ME 515 Mechatronics 11/9/2006 C++ Programming Input � cin - standard input object (connected to � keyboard) >> stream extraction operator ("get from") � cin >> myVariable; � � Gets stream from keyboard and puts it into myVariable Sem 7 Asanga Ratnaweera, Department of 15 Mechanical Engineering #include < <iostream iostream> > #include #include #include < < cstdio > > using namespace std; // programme to add two user input variables int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored cout << "Enter first integer = "; // prompt cin >> integer1; // read an integer cout << "Enter second integer = "; // prompt cin >> integer2; // read an integer sum = integer1 + integer2; // assign result to sum cout << "Sum is " << sum << endl; // print sum getchar(); return 0; // indicate that program ended successfully } Sem 7 Asanga Ratnaweera, Department of 16 Mechanical Engineering 8

  9. ME 515 Mechatronics 11/9/2006 #include < <iostream iostream> > #include #include < < cstdio > > #include using namespace std; // programme to add two user input variables int main() { double value1; // first number to be input by user double value2 ; // second number to be input by user double sum; // variable in which sum will be stored cout << "Enter first integer = "; // prompt cin >> value1 ; // read an integer cout << "Enter second integer = "; // prompt cin >> value2 ; // read an integer sum = value1 + value2 ; // assign result to sum cout << "Sum is " << sum << endl; // print sum getchar(); return 0; // indicate that program ended successfully } Sem 7 Asanga Ratnaweera, Department of 17 Mechanical Engineering C++ Programming � Arithmetic calculations Multiplication * � Division / � Integer division truncates remainder � � 7/5 evaluates to 1 Modulus operator % � Modulus operator returns remainder � � 7%5 evaluates to 2 Sem 7 Asanga Ratnaweera, Department of 18 Mechanical Engineering 9

Recommend


More recommend