Professional Documents
Culture Documents
C++ is a superset of C. Any correct C program is also a correct C++ program, except for some loopholes in C that are not allowed in C++.
C++: A Better C
Convenient syntax for inline comments: // Declaration anywhere Function overloading Default arguments Simplified IO: cin >>, cout<<, and more A new Boolean data type: bool Easier dynamic memory allocation: new & delete References (automatically dereferenced pointers) Function templates: data types as parameters Tag names as new data types Better type system: tighter use of void *
3
The C-syntax for comments, /* */, can still be used for multi-tine comments.
4
Declaration Anywhere
Declarations need no longer be at the head of blocks. Variables and functions can be declared any time, anywhere in a program, preferably as close to where a variable is used the first time. For example: note i is declared within for for (int i=0;i<n;i++)
5
Function Overloading
Two or more functions can have the same name but different parameters Example:
int max(int a, int b) { if (a>= b) return a; else return b; } float max(float a, float b) { if (a>= b) return a; else return b; }
6
Default Arguments
A default argument is a value given in the function declaration that the compiler automatically inserts if the caller does not provide a value for that argument in the function call. Syntax: return_type f(, type x = default_value,);
7
If we call f(2), is it to f in (1) or (2)? The 1st returns 1. The 2nd returns 4.
11
12
Simplified IO
Instead of the complicated syntax of printf and scanf, and the many variations of print and scan, C++ offers a much simpler syntax For standard output, use cout For standard input, use cin File IO is also simpler, and will be discussed later Note: one can still use the IO syntax of C in C++
15
Cout
For printing output, use this syntax:
cout << a string or an expression;
This prints out the string or the value of the extpression. For output chaining, use this syntax
cout << S1 <<S2<<S3<<endl;
Each Si is a string or an expression. The effect is to print out the value of S1 followed by the value of S2, followed by the value of S3.
16
Cout (Contd.)
The reserved word, endl, ensures that the next cout command prints its stuff on a new line. New lines can also be added using \n.
17
Cout (Examples)
Statements
int x=3; double y =4.5; cout <<the value of x=<<x; cout<<; y=<<y<<endl; cout <<x+y<<x+y; int x=3; double y =4.5; cout <<x = <<x<<\n; cout<<y=<<y; cout <<\nx+y<<x+y;
Output
the value of x=3; y=4.5
x+y=7.5
x=3 y=4.5 x+y=7.5
18
Cin
For reading input values into variables:
cin >> variableName1 >> variableName2 >> variableName3;
This reads the input from the standard input (say the screen for now), puts the first read value in variableName1, the second read value in variableName2, and the third read value in variableName3.
19
Cin (Examples)
Suppose you want to read an int value and a double value into variables n and x. This code will do it (see next slide):
int n; double x; cout<<enter an int, a space, and a double: ;
cin>>n>>x; // use of cin cout<<You entered int n=<<n; cout<<, and double x=<<x<<endl;
20
The code next will store 3 in n, and 9.8 in x, and print out to you:
You entered int n=-3, and double y=9.8
21
22
Example of bool
// Precondition: x[] is an integer array of length n. // n is a positive integer. // Postcondition: The output is true if the elements // of the input array are all positive,. Else, false. bool isAllPositive(int x[], int n){ for(int i=0;i<n;i++) if (x[i] <= 0) return false; return true; }
23
A Comprehensive Example
#include <cstdlib> #include <iostream> using namespace std; // codes for isAllPositive( ), and for overloaded // function max( ) should be inserted here int main(int argc, char *argv[]){ cout << " enter five integers :"; int x[5]; int n=5; for (int i=0;i<n;i++) // read the input to x[] cin>>x[i];
24
cout << "You entered: "; for (int i=0;i<n;i++) cout << x[i]<<" "; if (isAllPositive(x,n)) cout << \nYour data is all positive\n"; else cout << \n Your data is not all positive\n"; int M = max(x,n); cout<<Your maximum value is: "<<M<<endl; } // end of main( )
25
Semantics of new
For type *pointer = new type; :
The system allocates dynamically (during execution) a chunk of memory large enough to hold data of the specified type, and returns a pointer pointing to the address of that chunk. This pointer is stored in the user-provided pointer-variable pointer.
27
Example of new
// Precondition: n is a positive integer // Postcondition: computes and prints out the first n // elements of the Fibonacci sequence void fibonacci(int n){ int *x = new int [n]; // creation of a dynamic array x[0]=1; x[1]=1; for (int i=2;i<n;i++) x[i]=x[i-1]+x[i-2]; cout<<"The Fibonacci sequence of "<<n<<" values are:\n"; for (int i=0;i<n;i++) cout<<"x["<<i<<"]="<<x[i]<<endl; }
29
References
A reference is an autmatically dereferenced pointer Syntax of reference declaration:
Type& refname = variable;
Semantics: refname becomes another name (or alias) for variable. Any change to the value of variable causes the same change to refname, and vice versa.
31
Illustration of References
Statements
int x=17; int& xref = x; cout<<x=<<x<<endl; cout<<xref=<<xref<<endl; x=x+5; cout<<x=<<x<<endl; cout<<xref=<<xref<<endl; xref = xref-10; cout<<x=<<x<<endl; cout<<xref=<<xref<<endl;
Outcome
int x=5; int y=10; swap(x,y); cout<<x<<, <<y; Outcome: 10, 5 // it did swap
33
Example:
int x[]={11, 13, 5, 7, 4, 10}; double y[]={4.5, 7.13, 3, 17}; int minx = min<int>(x,6); double miny=min<double>(y,4); cout<<the minimum of array x is: <<minx<<endl; cout<<the minimum of array y is: <<miny<<endl;
37
38