Sunday, May 1, 2016

Input/Output Stream in C++(or OOP) and Data Hiding provided by class concept

Introduction

C++ support set of function for performing input and output operations. The syntax using this input/output function is totally consistent with the device. C++ new feature for handling input/output operation are called streams. Streams are abstraction that refers to data flow. Stream in C++ are:
1. Output Stream
2. Input Stream

1. Output Stream:

The output stream allow us to write operation on output devices such as screen, discs etc. Output on the standard stream is performed using the "cout" object. C++ uses the bit wise left shift operator for performing console output operations. The syntax of console output operation is:
cout<<variable name;
cout<<variable 1<<variable 2<<variable n;
The word cout followed by symbol "<<" called the insertion or "put to" operator and followed by items (variable, constant, expressions) that are to be output. Variable can be of any basic data types:
Example:
cout<<"A";
cout<<20;
cout<<"Hello Nepal";
int x=10,y=20
cout<<x+y;

2. Input Stream:

Input stream allows us read operation with input devices.Such as keyboard, discs, etc. Input from standard stream is performed using "cin"object. C++ used bit wise right shift operator for performing console input operation. The syntax of console input operation is:
cin>>variable name;
cin>>variable 1>>variable 2>>variable n;
Example: int x,y,z;
float m,n;
char a;
cin>>x>>y>>z>>m>>n>>a;

-->How class provide data hiding?

The data declared inside the class are called data member and function are called member function. Only the member function can have access to the private data member and member function. The private member have no access to the external function (i.e. private member are hidden from the external function so provide the data encapsulation.
The binding of data and function together into a single class called encapsulation. Example:
Class student
{ private
int roll;
float marks;
public
void getdata();
};

--> Accessing class member:

We can access class member by using dot operator as:
1. Accessing data member:
Syntax is- object name.variable name;
2. Accessing member function:
Syntax is- Object name.function name (actual parameter); 
Example:
#include<iostream.h>
#include<conio.h>
class student{
char name[50];
int roll;
float marks;
public:
void read data(){
cout<<"enter name:";
cin>.name;
cout<<"Enter roll no:";
cin>>roll;
cout<<"Enter marks:";
cin>>narks;
}
void display data()
{ cout<<name;
cout<<roll;
cout<<marks; }
void main()
{student s1,s2;
s1.read data();
s2.read data();
s1.display data();
s2.display data();
getch();
}

Defining Member Function:

1. Function Defination inside class:
2. Function Defination outside class:

Function Defination:

The member function that are declared inside class can be defined outside the class. The function defination outside class consist of function header with associated class level to represent membership of the class and contains the body of the function.
Syntax : return type class name::{
//body}
Example:
#include<iostream.h>
#include<conio.h>
class student
{private
char name[50];
int roll no;
float marks;
public:
void read data();
void display data();
};
void student:: read data(){cout<<"Enter name,roll no,marks";
cin>>name>>roll no>>marks;
}
void student::display data()
{cout<<name<<roll no<<marks;
}
void main()
{
student s1,s2;
s1.read data();
s2.read data();
s1.display data();
s2.display data();
}

No comments:

Post a Comment

Popular Posts