0% found this document useful (0 votes)
68 views16 pages

Cse 222 L2

A Sample C++ Program Namespaces A Closer Look at the I/O Operators Declaring Local Variables No Default to int The bool Data Type Old-Style vs. Modern C++ The New C++ Headers C++ Console I/O (Input) C++ Console I/O (Output)

Uploaded by

Nazmul Haque
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views16 pages

Cse 222 L2

A Sample C++ Program Namespaces A Closer Look at the I/O Operators Declaring Local Variables No Default to int The bool Data Type Old-Style vs. Modern C++ The New C++ Headers C++ Console I/O (Input) C++ Console I/O (Output)

Uploaded by

Nazmul Haque
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

CSE 222 : Object

Oriented Programming
Lecture 2: Some C++
Fundamentals
Topics to be Covered
 A Sample C++ Program
 Namespaces
 A Closer Look at the I/O Operators
 Declaring Local Variables
 No Default to int
 The bool Data Type
 Old-Style vs. Modern C++
 The New C++ Headers
 C++ Console I/O (Input)
 C++ Console I/O (Output)
Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 2
A Sample C++ Program
Modern Style of C++ I/O Operation Header
#include <iostream>
using namespace std;
Specify standard library of global namespace std
int main()
{
cout << output operator
int i;

cout << "This is output.\n"; // this is a single line comment


/* you can still use C style comments */

// input a number using >>


cout << "Enter a number: ";
cin >> i;
cin >> input operator

// now, output a number using <<


cout << i << " squared is " << i*i << "\n";

return 0;
}

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 3


Namespaces
 A namespace is a declarative region.
 It localizes the names of identifiers to
avoid name collisions.
 The contents of new-style headers are
placed in the std namespace.
 A newly created class, function or global
variable can put in an existing namespace,
a new namespace, or it may not be
associated with any namespace
 In the last case the element will be placed in the
global unnamed namespace.

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 4


A Closer Look at the I/O Operators
#include <iostream>
using namespace std;
int main()
{
float f;
char str[80];
double d;

cout << "Enter two floating point numbers: ";


cin >> f >> d;

cout << "Enter a string: ";


cin >> str; the >> operator stops reading input when the
first white-space character is encountered.

cout << f << "\t" << d << "\n" << str;


return 0;
} C++ I/O operators recognize the
entire set of backslash character

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 5


Declaring Local Variables
Invalid in C Valid in C++
/* Incorrect in C89. #include <iostream>
But OK in C++. */ using namespace std;
int main()
int f() {
{ float f;
int i; double d;
i = 10; cout <<"Enter two floating point numbers:" ;
cin >> f >> d;
int j; cout << "Enter a string: ";
/*won't compile as C program */
j = i*2; char str[80];
// str declared just before 1st use
return j; cin >> str;
}
cout << f << " " << d << " " << str;
return 0;
}
Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 6
No Default to int
C C++
func( int i) int func( int i)
{ {
return i*i; return i*i;
} }

common practice to not specify int In Standard C++, this function must
explicitly when a function returned have the return type of int Specified.
an integer result.

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 7


The bool Data Type
 C++ defines a built-in Boolean type
called bool.
 Objects of type bool can store only the values
true or false.
 values true or false are keywords defined by
C++.
 Automatic Conversion of bool values:
 values true or false, which are keywords defined by
C++.
 true is converted to 1 and false is converted to zero.

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 8


Old-Style vs. Modern C++
A traditional-style C+ A modern-style C+
+ program + program
#include <iostream.h> #include <iostream>
using namespace std;
int main()
{ int main()
/* program code */ {
return 0; /* program code */
} return 0;
}

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 9


The New C++ Headers
 The new-style headers do not specify filenames.
 They simply specify standard identifiers that
might be mapped to files by the compiler, but
they need not be.
 <iostream>
 <vector>
 <string>, not related with <string.h>
 <cmath>, C++ version of <math.h>
 <cstring>, C++ version of <string.h>
 Programmer defined header files should end in
“.h”.

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 10


C++ Console I/O ( cout , cin)

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 11


C++ Console I/O (Output)
 cout << “Hello World!”;
 printf(“Hello World!”);
 cout << iCount; /* int iCount */
 printf(“%d”, iCount);
 cout << 100.99;
 printf(“%f”, 100.99);
 cout << “\n”,
 cout << ‘\n’
 cout << endl
 printf(“\n”)
 In general, cout << expression;

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 12


C++ Console I/O (Input)
 cin >> strName; /*char strName[16]*/
 scanf(“%s”, strName);
 cin >> iCount; /* int iCount */
 scanf(“%d”, &iCount);
 cin >> fValue; /* float fValue */
 scanf(“%f”, &fValue);
 In general, cin >> variable;

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 13


C++ Console I/O (IO Chaining)
 cout << “Hello” << ‘ ‘ << “World” << ‘!’;

 cout << “Value of iCount is: ” << iCount;

 cout << “Enter day, month, year: ”;


 cin >> day >> month >> year;

 cin >> day;


 cin >> month;

 cin >> year

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 14


C++ Console I/O (Example)
include <iostream> include <iostream>
int main() using namespace std;
{ int main()
char str[16]; {
std::cout << “Enter a char str[16];
string: ”; cout << “Enter a
std::cin >> str; string: ”;
std::cout << “You cin >> str;
entered: ” cout << “You entered:
<< str; ” << str;
} }

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 15


Readings
 Chapter 11 : An Overview of C++,
 The Complete Reference : C++, By- Herbert
Schildt, 4th Edition, pp:260-270
 Chapter 2: C++ Programming Basics
 Robert Lafore, “Object-Oriented Programming
in C++”, Fourth Edition, Sams Publishing ,
pp:30-68

Mohammad Nazmul Haque Lecturer, Dept. of CSE, DIU 16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy