3 Oop
3 Oop
• // thank you
• The c comment symbols /* ….*/ are still valid and more suitable for multi
line comments.
6
Input Operator:
• The statement cin >> number1; is an input statement and causes the
program to wait for the user to type in a number.
• It extracts value from the keyboard and assigns it to the variable on its
right.
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;
8
C++ Console I/O (Output)
Output Operator
• The statement cout << ”Hello, world” displayed the string with in quotes on
the screen.
• The identifier cout can be used to display individual characters, strings and
even numbers.
• It is a predefined object that corresponds to the standard output stream.
• Stream just refers to a flow of data and the standard Output stream
normally flows to the screen display.
• The cout object, whose properties are defined in iostream.h represents
that stream.
• The insertion operator << also called the ‘put to’ operator directs the
information on its right to the object on its left.
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”, or cout << ‘\n’, or cout << endl
• printf(“\n”)
• In general, cout << expression;
11
Cascading Of I/O Operator:
• cout<<”sum=”<<sum<<”\n”;
• cout<<”sum=”<<sum<<”\n”<<”average=”<<average<<”\n”;
• cin>>number1>>number2;
Two Versions of C++
• A traditional-style C++ program -
#include <iostream.h>
int main()
{
/* program code */
return 0;
}
Two Versions of C++ (cont.)
• A modern-style C++ program that uses the new-style headers and a
namespace -
#include <iostream>
using namespace std;
int main()
{
/* program code */
return 0;
}
Namespaces
• A namespace is a declarative region.
15
STRUCTURE OF C++ PROGRAM
• Include files
• Class declaration
• Class functions, definition
• Main function program
Example :-
# include<iostream.h> void person::display()
class person {
{ cout<<”\n name:”<<name
char name[30]; cout<<”\n age:”<<age;
int age; }
public:
void getdata(); int main( )
void display();
{
};
person p;
void person :: getdata ()
p.getdata();
{
p.display();
cout<<”enter name”;
return(0);
cin>>name;
}
cout<<”enter age”;
cin>>age;
}
#include <iostream>
#include <iostream> using namespace std;
using namespace std; // namespace_1
// namespace_1 namespace space_1
namespace space_1 {
{ void func()
void func() {
{ cout << "Inside space_1" << endl;
cout << "Inside space_1" << endl; }
} }
}
// namespace_2
// namespace_2 namespace space_2
namespace space_2 {
{ void func()
void func() {
{ cout << "Inside space_2" << endl;
cout << "Inside space_2" << endl; }
} }
} using namespace space_1::space_2;
using namespace space_1; int main ()
int main () {
{ // This calls function from space_2.
// This calls function from space_1. func();
func();
return 0; return 0;
} }
TOKENS
The smallest individual units in program are known as tokens. C++ has the
following tokens.
i. Keywords
ii. Identifiers
iii. Constants
iv. Strings
v. Operators
Keywords
• The keywords implement specific C++ language feature.
• They are explicitly reserved identifiers and can’t be used as names for the
program variables or other user defined program elements.