C++ Tutorial for C Users

C++ Tutorial for C Users - C tutorial for C users This text...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C++ tutorial for C users This text is aimed at C users who wish to learn C++. It is also interesting for experienced C++ users who leaved out some features of the language. The possibilities of C++ are briefly enunciated and illustrated. When you will try to use them for your own programs you will encounter a lot of problems and compilation errors. Come back to this text and look carefully at the examples. Make use of the help files of your development environment. Do not hesitate to copy the examples from this text and paste them inside your development environment in order to test and modify them. You can use / / to type a remark : #include <iostream.h> // This library is often used void main () // The program's main routine. { double a; // Declaration of variable a. a = 456; a = a + a * 21.5 / 100; // A calculation. cout << a; // Display the content of a. } Input from keyboard and output to screen can be performed through cout << and cin >> : #include <iostream.h> void main() { 1 of 56 12.03.99 01:19 C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm int a; // a is an integer variable char s [100]; // s points to a sring of 99 characters cout << "This is a sample program." << endl; cout << endl; // Line feed (end of line) cout << "Type your age : "; cin >> a; cout << "Type your name : "; cin >> s; cout << endl; cout << "Hello " << s << " you're " << a << " old." << endl; cout << endl << endl << "Bye !" << endl; } Variables can be declared everywhere : #include <iostream.h> void main () { double a; cout << "Hello, this is a test program." << endl; cout << "Type parameter a : "; cin >> a; 2 of 56 12.03.99 01:19 C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm a = (a + 1) / 2; double c; c = a * 5 + 1; cout << "c contains : " << c << endl; int i, j; i = 0; j = i + 1; cout << "j contains : " << j << endl; } A variable can be initialised by a calculation : #include <iostream.h> void main () { double a = 12 * 3.25; double b = a + 1.112; cout << "a contains : " << a << endl; cout << "b contains : " << b << endl; a = a * 2 + b; double c = a + b * a; 3 of 56 12.03.99 01:19 C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm cout << "c contains : " << c << endl; } Like in C, variables can be encapsulated between hooks : #include <iostream.h> void main() { double a; cout << "Type a number : "; cin >> a; { int a = 1; a = a * 10 + 4; cout << "Local number : " << a << endl; } cout << "You typed : " << a << endl; } C++ allows to declare a variable inside the for loop declaration. It's like if the variable had been declared just before the loop : #include <iostream.h> 4 of 56 12.03.99 01:19 C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm void main () { for (int i = 0; i < 4; i++) { cout << i << endl; } cout << "i contains : " << i << endl; for (i = 0; i < 4; i++) { for (int i = 0; i < 4; i++)...
View Full Document

This note was uploaded on 01/05/2011 for the course COMS 131 taught by Professor Dad during the Spring '10 term at University of Mumbai.

Page1 / 56

C++ Tutorial for C Users - C tutorial for C users This text...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online