Introduction to C++
Dept. of Computer Science & Engineering
1
Programming with C++
Two versions of C++
Old version of C++ New version of C++
#include <iostream.h> #include <iostream>
int main(){ using namespace std;
/* program code */ int main(){
return 0; /* program code */
return 0;
}
}
2 Department of CSE, CUET
General form of C++ Console I/O
Input Command: cin >> variable;
Output Command: cout << expression;
3 Department of CSE, CUET
Namespaces
A namespace is a declarative region that localizes the names of
identifiers to avoid name collisions.
The contents of new-style headers are placed in the std namespace
#include <iostream> #include <iostream>
int main() { using namespace std;
char str[16]; int main() {
std::cout << “Enter a string: ”; char str[16];
std::cin >> str; cout << “Enter a string: ”;
std::cout << “String: ” << str; cin >> str;
return 0; cout << “String: ” << str;
} return 0;
}
4 Department of CSE, CUET
Scope Resolution Operator (::)
To access a hidden global variable
#include <iostream>
int count = 0;
int main(void) {
int count = 0;
::count = 1; // set global count to 1
count = 2; // set local count to 2
return 0;
}
5 Department of CSE, CUET
Variable Can Be Declared Anywhere!
6 Department of CSE, CUET
Bool Type
7 Department of CSE, CUET
C++ Comments
Multi-line comments
/* one or more lines of comments */
Single line comments
// …
8 Department of CSE, CUET
9 Department of CSE, CUET