You are on page 1of 48

7

+ intdouble

dt dt++
++
C++ C++


C++
C++



C++

1
1
C++

2
C++

X + Y "ABC" + "DEF"+
3
+*
+

5
operator operator+
operator +() +

operator // void

1
{
}
Window &operator=(Window &win)
{
}

friend operator // void
{
}
6






7
C++
+X, ++X
X++,X--
X+Y, X-Y, X*Y

++Obj1----> operator++(Obj1) //
{
}
++Obj1----> Obj1.operator++() //
{
}

int


Obj1++----> operator++(Obj1, int X) // int
{
}
Obj1++----> Obj1.operator++(int X) // int
{
}


int


Obj1+ Obj2---->operator+(Obj1, Obj2) //
{
}
Obj1+ Obj2---->Obj1.operator+(Obj2) //
{
}

2
2
1
.
.*


sizeof
2


3

C++
+ - * / % ^ & | ~ ! = < >
+= -= *= /= %= ^= &= |= << >> >>= <<=
== != <= >= && || ++ -- [] () new delete



=()[]->

<< >>
4
5 =
= &
&

&

C++ Visual C++6.0 C++ .h

complex

#include <iostream.h>
class complex {
public:
double real,imag;
complex(double r=0,double i=0) //
{ real = r; imag = i; }
};
void main()
{ complex com1(1.1,2.2),com2(3.3,4.4),total;
total = com1 + com2; //
}

#include <iostream.h>
class complex {

3
public:
double real,imag;
complex(double r=0,double i=0) //
{ real = r; imag = i; }
complex complex_add(complex &c2);
void disp();
};
complex complex::complex_add(complex &c2)
{ complex temp;
temp.real = real + c2.real;
temp.imag = imag + c2.imag;
return temp;
}
void complex::disp()
{ cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
void main()
{ complex com1(1.1,2.2),com2(3.3,4.4),total;
total = com1.complex_add(com2);
cout<<" com1="; com1.disp();
cout<<" com2="; com2.disp();
cout<<" com1+com2="; total.disp();
}
C++ com1 com2
+,-
total=com1+com2
C++
operator operator@ @
operator@

complex operator+()
friend complex operator+(complex om1,complex om2)
{ complex temp;
temp.real = om1.real + om2.real;
temp.imag = om1.imag + om2.imag;
return temp;
}
total = com1 + com2;

total = operator+(com1,com2);


1 C++
2

4
.2
.2

1

class X
{ //
type operator <> (); //
//
};

type operator


type X::operator<>()
{ //
}

2
--->
--->operator
--->
this
--->

+
c = a + b
obj1obj2 obj3 obj3 = obj1 + obj2

#include <iostream.h>
class sample
{ private: int counter;
public:
sample(){ counter=2;}
sample operator+(sample a)

5
{ sample temp; //
temp.counter = counter + a.counter;
return temp; //
}
void display(){cout<<counter<<endl; }
};
void main()
{ sample obj1 , obj2 , obj3;
obj3=obj1+obj2; //+ obj1.operator+(obj2);
obj3.display();
sample obj4;
obj4=obj3+obj1+obj2;
obj4.display();
}

1--
#include <iostream.h>
class Complex
{ public: Complex(int r=0, int i=0); //
Complex operator+(const Complex &Obj);
Complex operator-();//
Complex & operator=(Complex &Obj); //=
void show(){ cout<<real<<+<< image<<i; }
private: int real, image;
};
Complex ::Complex(int r, int i)
{ real=r; image=i;
}
Complex Complex::operator+(const Complex &Obj)
{ int r=real + Obj.real ;
//+ C++
int i=image + Obj.image ;
return Complex(r, i) ;
}
Complex Complex::operator-()
{ return Complex(-real,-image);
}
Complex & Complex::operator=(Complex &Obj) // =
{ real=Obj.real ; image=Obj.image ;
return *this;
}
void main()
{ Complex C1(10,20), C2(20,30), C ;// Complex
C=C1+C2 ; // + //+

6

//C.operator=(C1.operator+(C2));
C1.show(); cout<<+;
C2.show(); cout<<=;
C.show(); cout<<endl;
C=-C1 ; // C.operator=(C1.operator-());
C1.show();cout<<=;cout<<-(;C.show();cout<<)<<endl;
}
10+20i+20+30i=30+50i
10+20i=-(-10+-20i)


private
1

friend type operator@()
friend




type operator@()
{ //
}

operator
2
--->
--->operator
--->

--->

+
#include <iostream.h>
class sample
{ private: int counter;
public:
sample(){ counter=2;}
friend sample operator+(sample &a1, sample &a2)
{ sample temp; //
temp.counter = a1.counter + a2.counter;

7
return temp; //
}
void display(){cout<<counter<<endl; }
};
void main()
{ sample obj1 , obj2 , obj3;
obj3=obj1+obj2; //"+" operator+(obj1,obj2);
obj3.display();
sample obj4;
obj4=obj3+obj1+obj2;
obj4.display();
}

1
#include <iostream.h>
class Complex
{ public:Complex(int r=0, int i=0) ; //
friend Complex operator+(const Complex &Obj1,const Complex &Obj2) ;
friend Complex operator-(const Complex &Obj) ;
Complex & operator=(Complex &Obj) ; //=
void show(){ cout<<real<<+<< image<<i; }
private: int real, image ;
} ;
Complex::Complex(int r, int i)
{ real=r ; image=i ;
}
Complex operator+(const Complex &Obj1,const Complex &Obj2)
{ int r=Obj1.real + Obj2.real ;
//+ C++
int i=Obj1.image + Obj2.image ;
return Complex(r,i) ;
}
Complex operator-(const Complex &Obj)
{ return Complex(-Obj.real,-Obj.image) ;
}
Complex & Complex::operator=(Complex &Obj) //=
{ real=Obj.real ;
image=Obj.image ;
return *this ;
}
void main()
{ Complex C1(10,20), C2(20,30), C ;
C=C1+C2 ; //+
//C.operator=(operator+(C1,C2));

8
C1.show(); cout<<+;
C2.show(); cout<<=;
C.show(); cout<<endl;
C=-C1 ; // C.operator=(operator-(C1));
C1.show();cout<<=;cout<<-(;C.show();cout<<)<<endl;
}
10+20i+20+30i=30+50i
10+20i=-(-10+-20i)

this

private


1 7.17
2C++



= () [] ->
3



this

main():
void main()
{ Complex x(5,3),y;
y=x+27;
y=7+y;
y.show();
}
y=7+y;
operator+7y 7 Complex
7.operator+y

3
1 C++

Z=X+Y; ---> Obj3=Obj1 + Obj2


2

+
3
=

9
1
#include <iostream.h>
class sample
{ private: int counter;
public:
sample(){ counter = 2; }
void sample::operator += (sample a)
{ counter += a.counter; //
}
void display()
{ cout << counter << endl; }
};
void main()
{ sample obj1 , obj2;
obj1 += obj2; // obj1.operator+=(obj2)
obj1.display();
}
2
#include <iostream.h>
class sample
{ private: int counter;
public:
sample(){ counter = 2; }
friend void operator += (sample &a1,sample &a2)
{ a1.counter += a2.counter; //
}
void display()
{ cout << counter << endl; }
};
void main()
{ sample obj1 , obj2;
obj1 += obj2; // operator+=( obj1, obj2)
obj1.display();
}

2
person p1 = p2;person p1; p1 = p2;
person p1 = p2;person p1(p2)
p1
person p1; p1 = p2p1
= p2;

10
#include <iostream.h>
#include <string.h>
class String
{ private: char *str;
public:
String(char *s = "")
{ int length = strlen(s);
str = new char[length+1];
strcpy(str , s);
}
~String(){delete[] str; } //
String & String::operator = (String & s)
{ delete[] str;
int length = strlen(s.str);
str = new char[length+1];
strcpy( str , s.str);
return( *this );
}
void display(){ cout << str;}
};
void main()
{ String s1("Welcome to my world \n");
String s2;
s2 = s1;
s1.display();
s2.display();
}

X
class X
{
X(some_value); //

X(const X&); //

X&operator = (const X&); //

~X(); //

11
};

3

#include <iostream.h>
#include <string.h>
class String
{ char name[256];
public:
String(char *str) //
{ strcpy(name, str);}
String(){ } //
~ String(){ } //
String operator+(const String&);
void display()
{ cout<<The string is:<<name<<endl;}
};
static char *str;
String String::operator+(const String& a)
{ strcpy(str,name);
strcat(str,a.name);
return String(str);
}
void main()
{ str=new char[256];
String demo1( Visual c++ );
String demo2=( 6.0);
demo1.display(); demo2.display();
String demo3= demo1 + demo2; //demo1.operator+(demo2)
demo3.display();
String demo4= demo3 + Programming.;
demo4.display();
delete str;
}

The string is: Visual c++


The string is: 6.0
The string is: Visual c++ 6.0
The string is: Visual c++ 6.0 Programming.

#include <iostream.h>
#include <string.h>
class String
{ char name[256];

12
public:
String(char *str) //
{ strcpy(name, str);}
String(){ } //
~ String(){ } //
friend String operator+(const String& ,const String&);
void display()
{ cout<<The string is:<<name<<endl;}
};
static char *str;
String operator+(const String& a, const String& b)
{ strcpy(str,a.name);
strcat(str,b.name);
return String(str);
}
void main()
{ str=new char[256];
String demo1( Visual c++ );
String demo2=( 6.0);
demo1.display(); demo2.display();
String demo3= demo1 + demo2;// operator+(demo1, demo2)
demo3.display();
String demo4= demo3 + Programming.;
demo4.display();
delete str;
}

1

#include <iostream.h>
class vector
{ private:
double x,y;
public:
vector(double vx, double vy){x=vx;y=vy;}
vector(){x=0;y=0;}
vector operator+(vector v1);
vector operator-(vector v1);
vector operator-();
void print(){cout<<x<< <<y<<endl;}
};
vector vector::operator+(vector v1)
{ vector v;
v.x=x+v1.x;

13
v.y=y+v1.y;
return v;
}
vector vector::operator-(vector v1)
{ vector v;
v.x=x-v1.x;
v.y=y-v1.y;
return v;
}
vector vector::operator-()
{ vector v;
v.x=-x;
v.y=-y;
return v;
}
void main()
{ vector v1(4.5,-7.8),v2(-1.5,1.2);
v1.print();
v2.print();
v1=v1+v2;
v1.print();
v2=-v1;
v2.print();
}
4.5 -7.8
-1.5 1.2
3 -6.6
-3 6.6

#include <iostream.h>
class vector
{ private:
double x,y;
public:
vector(double vx, double vy){x=vx;y=vy;}
vector(){x=0;y=0;}
friend vector operator+(vector v1, vector v2);
friend vector operator-(vector v1, vector v2);
friend vector operator-(vector v1);
void print(){cout<<x<< <<y<<endl;}
};
vector operator+(vector v1, vector v2)
{ vector v;
v.x=v1.x+v2.x;

14
v.y=v1.y+v2.y;
return v;
}
vector operator-(vector v1, vector v2)
{ vector v;
v.x=v1.x-v2.x;
v.y=v1.y-v2.y;
return v;
}
vector operator-(vector v1)
{ vector v;
v.x=-v1.x;
v.y=-v1.y;
return v;
}
void main()
{ vector v1(4.5,-7.8),v2(-1.5,1.2);
v1.print();
v2.print();
v1=v1+v2;
v1.print();
v2=-v1;
v2.print();
}
2
operator
#include <iostream.h>
class F
{ public:
double operator()(double x,double y)const;
};
double F::operator()(double x,double y)const
{ return (x+5)*y;
}
void main()
{ F f;
cout<<f(1.5,2.2)<<endl;
}
3
.3
.3 + +
+ - -
-

C++ 1 1
a = b++a = ++ba = b++ b a
b

15
a = ++b b b a

++Obj1----> operator++(Obj1) //
{
}
++Obj1----> Obj1.operator++() //
{
}
++obj1
operator++(X &obj1); // obj1 X
++obj1
obj1.operator++(); //

Obj1++----> operator++(Obj1, int X) // int
{
}
Obj1++----> Obj1.operator++(int X) // int
{
}
obj1++
operator++(X &obj1,int); // obj1 X
obj1++
obj1.operator ++(int); //

int 0

1
#include <iostream.h>
class sample
{private: int counter;
public:
sample(){counter = 0;}
void operator ++() //
{ ++ counter; }
void operator ++(int) //
{ ++ counter; }
void display(){cout << counter << endl; }
};
void main()
{ sample obj1;
obj1.display();
++obj1; //
obj1.display();
obj1++; //
obj1.display();

16
}
++obj1; obj1++; obj1
obj2++;

#include <iostream.h>
class sample
{private:int counter;
public:
sample(){counter = 0;}
sample operator ++() // ++a
{ ++ counter;
return *this; //
}
sample operator ++(int) // a++
{ sample temp(*this); //
counter ++; //
return temp; //
}
void display(){ cout << counter << endl;}
};
void main()
{ sample obj1,obj2,obj3;
obj1.display();
obj2 = ++obj1; //
obj2.display();
obj3 = obj1++; //
obj3.display();
obj1.display();
}

2++
++
#include <iostream.h>
class X
{ private: int num;
public:
X(int j) { num=j; } //

17
int operator++(); // : ++n
int operator++(int); // : n++
void show(){ cout<<num=<<num<<endl; }
};
int X:: operator++()
{ num++; return num;
}
int X:: operator++(int)
{ int j=num; num++; return j;
}
void main()
{ X obj1(10);
int m=++obj1; // obj1.operator++()
cout<<m=<<m<<endl;
obj1.show();
m=obj1++; // obj1.operator++(int)
cout<<m=<<m<<endl;
obj1.show();
m=obj1.operator++(); // obj1.operator++()++obj1
cout<<m=<<m<<endl;
obj1.show();
m=obj1.operator++(0); // obj1.operator++(int) obj1++
cout<<m=<<m<<endl;
obj1.show();
}
++
#include <iostream.h>
class Y
{ private: int num;
public:
Y(int j) { num=j; }//
friend int operator++(Y &); // :++n
friend int operator++(Y &,int); // :n++
void show(){ cout<<num=<<num<<endl; }
};
int operator++(Y &a)
{ a.num++; return a.num;
}
int operator++(Y &a,int)
{ int j=a.num; a.num++; return j;
}
void main()
{ Y obj1(10);
int m=++obj1; // operator++(obj1)

18
cout<<m=<<m<<endl;
obj1.show();
m=obj1++; // operator++( obj1,int)
cout<<m=<<m<<endl;
obj1.show();
m=operator++(obj1);
// operator++(Y &)
++obj1
cout<<m=<<m<<endl;
obj1.show();
m= operator++(obj1,0); // operator++(Y &,int)
obj1++
cout<<m=<<m<<endl;
obj1.show();
}

.4
.4
C++/I/OC++/
I/O I/O C++
C++
I/O
C C
I/O
I/O

C++ C++

I/O

C++

/
/

cin >> name; //


cout << my name is << name << endl; //
C++

C++
C++

19
I/O

istrstream

ifstream
istream
istream_withassign strstream
cin I/O
fstream
iostream I/O
I/O stdiostream
ios
I/O I/O
ostrstream

ofstream
ostream
ostream_withassign
cout

ios istream ostream


I/O
ios istream ostream
>><<
iostream istream ostream

ios
istream
ostream
fstreambase
strstreambase
(ifstream)
(ofstream)
(istrstream)(ostrstream)
(iostream)
(fstream)
(strstream)
C++ >>
>> <<
<<
cincoutcerrclog I/O
iostream.h
iostream.h
c in istream

c out ostream

cerr ostream cerr

20
clog ostream clog


#include <iostream.h>
void main()
{ cout << "" <<endl;
cerr << " " <<endl;
clog << "" << endl;
}

1 ax2+bx+c=0
#include <iostream.h>
#include <math.h>
void main()
{ float a,b,c,disc;
cout<<"please input a,b,c:";
cin>>a>>b>>c;
if (a==0)
cerr<<"a is equal to zero,error!"<<endl;
else
if ((disc=b*b-4*a*c)<0)
cerr<<"disc=b*b-4*a*c<0"<<endl;
else
{ cout<<"x1="<<(-b+sqrt(disc))/(2*a)<<endl;
cout<<"x2="<<(-b-sqrt(disc))/(2*a)<<endl;
}
}
C++ I/O iostream.h,fstream.h strstrea.h

iostream.h ios, iostream, istream, ostream, ostream_withassign,


istream_withassign, ostream_withassign
fstream.h fstream, ifstream, ofstream fstreambase iostream.h

strstrea.h strstream, istrstream, ostrstream strstreambase
iostream.h

21
iomanip.h setw iostream.h



I/O

iostream.h
iostream.h

I/O

fstream.h
fstream.h I/O

strstrea.h
strstrea.h

#include<>
C++#include
C++
C++ iostream I/O iostream iostream.h
/ C++ iostream.h
cincoutcerrclog 4 I/O I/O

I/O iomanip.h

/ iostream.h
cin
cout
:
get
getline
>>
:
put
write n
<<
1
c out ostream <<


ostream& operator<<()
in istream
c in >>

istream& operator>>(&)

1#include <iostream.h>
void main( )
{
int x=56, y=10; float a;
char *str ="Windows";
cin >> a;
cout << x << y << endl;
cout << a << endl;
cout << str << endl;
}

22



<< put() write()

>> get() getline() read()
1 put
put
ostream put()
ostream& <>.put (char ch);
ostream& <>.putconst char c; //

ch ostream cout

cout.put('X'); // X
ostream

cout.put('G').put('o') .put('o') .put('d'); // Good

1#include <iostream.h>
void main( )
{
int x, y; char c;
cout << "Enter two integers:";
cin >> x >> y;
cout << x << (x==y? " is": " is not ") << " equal to " << y << endl;
c=cin.get( ); //

cout.put(c); //
// cout.put(c).put(\n);//(.)
}

2 put()

#include <iostream.h>
void main()
{ cout.put('M');
cout.put('\n');
cout.put('G').put('o') .put('o') .put('d');
cout.put('\n');
char str[10] = "Hello";
cout.put(str[0]);
cout.put('\n');
}

3 BASIC

23
#include <iostream.h>
void main()
{char *a="BASIC";
for(int i=4;i>=0;i--)
cout.put(*(a+i));
cout.put('\n');
}
2 write
write
ostream write()

ostream& < >.write (const char *str, int length);

str , length
length strlen(str)
put() write() ostream cout
ostream

1write
#include <iostream.h>
#include <string.h>
const int SIZE =80;
void main( )
{ char buffer[SIZE];
cout<<"Enter a sentence: "<<endl;
cin.getline(buffer,SIZE);
cout<<endl<<"The string read was :"<<endl<<buffer<<endl;
cout.write(buffer, 8);
cout << endl;
cout.write("Hello world!", 9);
cout.write("\n",1);
char *str="Hello world!\n";
cout.write(str, strlen(str));
}
Enter a sentence:
I am a teacher.

The string read was :


I am a teacher.
I am a t
Hello wor
Hello world!
3 get()
istream get()

24
1
int get();//
2
istream& get(char& ch); //
3
istream& get(char *str, int length, char delimiter = '\n'); //


1 length length-1
2 delimiter\n

3 EOF

1 get()
#include <iostream.h>
void main()
{ char ch;
cout << "" << endl;
ch = cin.get(); //
cout<<ch<<endl;
ch = cin.get(); //
char c1, c2, c3;
cout << "" << endl;
cin.get(c1).get(c2).get(c3);//
cout.put(c1).put(c2).put(c3).put('\n');
ch = cin.get(); //
char * str = new char;
cout << " 100 x" << endl;
cin.get(str, 100, 'x'); //
cout <<str<<endl;
}

2cin get
#include <iostream.h>
const int SIZE =80;
void main( )
{
char buffer1[SIZE], buffer2[SIZE];
cout << "Enter a sentence: " << endl;
cin >> buffer1;
cout << endl << "The string read with cin was :" << buffer1 << endl;
cin.get(buffer2,SIZE);
cout << "The string read with cin.get was: " << buffer2 << endl;
}

25
Enter a sentence:
Hello! Welcome. Yes

The string read with cin was :Hello!


The string read with cin.get was: Welcome. Yes
:
(1) cin
cin >> x >> y;
10 20
10 20
(2)
cin >> i >> x ; //i x
56.79 32.85
i=56, x=0.79
i=56, x=32.85
(3)
cin >> str;
We are students!
str=We, str=We are student!
4 getline()
istream getline()
istream& getline(char *str, int length, char delimiter = '\n');
get()
length-1 EOF delimiter
getline() get() delimiter
istream
5 read()
istream read()

istream& read(char * str, int length);

str length

getline() read()
#include <iostream.h>
void main()
{ char * str= new char;
cout << "" << endl;
cin.getline(str, 1000, '\n');
cout << str << endl;
char * ch= new char;
cout << " 7 " << endl;
cin.read(ch, 7);
cout << ch << endl;

26
char c1;
c1 = cin.get(); //
cout << " 1 " << endl;
cin.get(c1);
cout << c1 << endl;
}
. 5 << >>
C++>><<

C++>><<
1 <<
<<
ostream & operator <<( ostream & s, X a)
{
// X
return s;
}
2 >>
>>
istream & operator >>( istream & s, X a)
{
// X
return s;
}

<< <<
ostream &
>>
>>
istream &

1 <<

#include <iostream.h>
class Complex
{ public:Complex(int r=0, int i=0)
{ real=r ; image=i ; }
friend ostream & operator <<( ostream & s, Complex c)
{ s<<c.real<<+;
s<<c.image<<i<<endl;
return s;
}
private: int real, image ;
};
void main()
{ Complex a(3,4),b(5,6);
cout<<a<<b<<endl;
}

27

main() cout<<a operator<<(cout,a);

#include <iostream.h>
class Complex
{public:
friend ostream& operator << (ostream&,Complex&);
friend istream& operator >> (istream&,Complex&);
private:
double real; double imag;
};
ostream& operator << (ostream& output,Complex& c)
{ output<<"("<<c.real<<"+"<<c.imag<<"i)";
return output;
}
istream& operator >> (istream& input,Complex& c)
{ cout<<"input real part and imaginary part of complex number:";
input>>c.real>>c.imag;
return input;
}
void main()
{ Complex c1,c2;
cin>>c1>>c2;
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
}

2<< >>

#include <iostream.h>
class Date
{ public:
Date(int y,int m,int d){ Year=y;Month=m;Day=d; }//
friend ostream & operator <<(ostream &, Date &);
friend istream & operator >>(istream &, Date &);
private:
int Year, Month, Day;
};
ostream & operator <<(ostream & stream, Date &date)
{ stream<< date.Year<</<< date.Month<</ << date.Day<<endl;
return stream;
}
istream & operator >>( istream & stream, Date &date)
{ stream>> date.Year>> date.Month>> date.Day;
return stream;

28
}
void main()
{ Date Cdate(2006,1,1);
cout<<Current date:<< Cdate<<endl; // operator<<(cout, Cdate);
cout<<Enter new date: xxxx xx xx ;
cin>> Cdate; // operator>>(cin, Cdate);
cout<<New date:<< Cdate<<endl;
}

Current date:2006/1/1
Enter new date:2006 9 20
New date:2006/9/20

main() cin>>Cdate; operator>>(cin, Cdate);


main() cout<<Cdate operator<<(cout,Cdate);

3<<>>
#include <iostream.h>
class PhoneNumber
{ friend ostream & operator <<( ostream &, const PhoneNumber &);
friend istream & operator >>( istream &, PhoneNumber &);
private:
char areaCode [4]; //
char exchange [4]; //
char line [5]; //
};
// << ()
ostream & operator <<(ostream & output, const PhoneNumber &num)
{ output<<(<<num.areaCode<<) <<num.exchange<<-<<num.line;
return output; // cout<<a<<b<<c;
}
//>>
istream & operator >>( istream &input, PhoneNumber &num)
{ input.ignore(); //
input.getline (num.areaCode,4); //
input.ignore(2); //
input.getline (num. exchange,4); //
input.ignore(1); //-
input.getline (num.line,5); //
return input; // cout>>a>>b>>c;
}
void main()
{ PhoneNumber phone; // phone
cout<<Enter a phone number in the form (123) 456-7890:\n;

29
//cin>> phone operator >>( cin, phone)
cin>> phone;
//cout<<phone operator <<(cout, phone)
cout<<The phone numbern entered was:\n<< phone<<endl;
}
Enter a phone number in the form (123) 456-7890:
(800) 555-1212
The phone numbern entered was:
(800) 555-1212

main() cin>> phone operator>>(cin, phone);


main() cout<< phone operator<<(cout, phone);
istream getline
cout<<

.6
.6
C++ C C C++
ios
1 ios
iostream.h ios C++

ios
ios

I/O ios ios ios
/ cin cout
cout.width(4); cout.precision(5);


skipws
left
right
internal
dec
oct
hex
showbase
showpoint
uppercase A-F
showspos +
fixed
scientific
ios::flag() ios::setf()
iostream.h ios

30
long setflong


long flag
Long flag
long setflonglong
long setflong
long unsetflong
int widthint
int width 0
char fillchar
char fill
int precisionint
int precision

1
#include <iostream.h>
void main()
{ int x=30,y=300,z=1024;
cout<<x<< <<y<< <<z<<endl; //
cout.setf(ios::oct); //
cout<<x<< <<y<< <<z<<endl; //
cout.unsetf(ios::oct); //
cout.setf(ios::hex); //
cout<<x<< <<y<< <<z<<endl; //
cout.setf(ios::showbase|ios::uppercase); //
cout<<x<< <<y<< <<z<<endl;
cout.unsetf(ios::showbase|ios::uppercase); //
cout<<x<< <<y<< <<z<<endl;
cout.unsetf(ios::hex); //
cout<<x<< <<y<< <<z<<endl;
}
30 300 1024
36 454 2000 //
1e 12c 400 //
0X1E 0X12C 0X400
1e 12c 400
30 300 1024

2
#include <iostream.h>
void main()
{ int x=468; double y=-3.425648;
cout<<x=;
cout.width(10); // 10
cout<<x; //

31
cout<<y=;
cout.width(10); // 10
cout<<y<<endl;
cout.setf(ios::left); //
cout<<x=;cout.width(10);cout<<x;
cout<<y=;cout.width(10);cout<<y<<endl;
cout.fill(*); //*
cout.precision(3); // 3
cout.setf(ios::showpos); //
cout<<x=;cout.width(10);cout<<x;
cout<<y=;cout.width(10);cout<<y<<endl;
}
x= 468y= -3.425648
x=468 y=-3.425648
x=+468******y=-3.426****

3
#include <iostream.h>
void main()
{ float x=25, y=-4.762;
cout<<x<< <<y<<endl;
cout.setf(ios::showpoint); // 0
cout<<x<< <<y<<endl;
cout.unsetf(ios::showpoint); //
cout.setf(ios::scientific); //
cout<<x<< <<y<<endl;
cout.setf(ios::fixed); //
cout<<x<< <<y<<endl;
}
25 -4.762
25.000000 -4.762000
2.5e+01 -4.762e+00
25 -4.762
2

/
cout << setw(10) << hex << n << endl;
#include <iomanip.h>

32

setiosflags(ios::fixed)

setiosflags(ios::scientific)

setiosflags(ios::left)
setiosflags(ios::right)

setiosflags(ios::skipws)

setiosflags(ios::lowercase)

setiosflags(ios::uppercase)

#include<iostream.h>
#include<iomanip.h>
void main()
{ int n;
cout << " Enter a decimal number: ";
cin >> n;
cout << n << " in hexadecimal is: "
<< hex << n << endl
<< dec << n << " in octal is: "
<< oct << n << endl
<< setbase(10) << n << " in decimal is: " << n << endl;
}
Enter a decimal number: 12
12 in hexadecimal is: c
12 in octal is: 14
12 in decimal is: 12

11 ios 1

#include<iostream.h>
#include<iomanip.h>
void main()
{ int x=30,y=300,z=1024;
cout<<x<< <<y<< <<z<<endl; //

33
cout<<oct<<x<< <<y<< <<z<<endl; //
cout<<hex<<x<< <<y<< <<z<<endl; //
cout<<setiosflags(ios::showbase|ios::uppercase); //
cout<<x<< <<y<< <<z<<endl; //
cout<<resetiosflags(ios::showbase|ios::uppercase); //
cout<<x<< <<y<< <<z<<endl; //
cout<<dec<<x<< <<y<< <<z<<endl; //
}
30 300 1024
36 454 2000 //
1e 12c 400 //
0X1E 0X12C 0X400
1e 12c 400
30 300 1024

21 ios 2
#include<iostream.h>
#include<iomanip.h>
void main( )
{ int x=468; double y=-3.425648;
cout<<x=<<setw(10)<<x;
cout<<y=<<setw(10)<<y<<endl;
cout<<setiosflags(ios::left); //
cout<<x=<<setw(10)<<x;
cout<<y=<<setw(10)<<y<<endl;
cout<<setfill(*); //*
cout<<setprecision(3); // 3
cout<<setiosflags(ios::showpos); //
cout<<x=<<setw(10)<<x;
cout<<y=<<setw(10)<<y<<endl;
cout<<resetiosflags(ios::showpos|ios::left);
cout<<setfill( );
}
x= 468y= -3.425648
x=468 y=-3.425648
x=+468******y=-3.426****

31 ios 3
#include<iostream.h>
#include<iomanip.h>
void main( )
{ float x=25, y=-4.762;
cout<<x<< <<y<<endl;
cout<<setiosflags(ios::showpoint); // 0

34
cout<<x<< <<y<<endl;
cout<<resetiosflags(ios::showpoint); //
cout<<setiosflags(ios::scientific); //
cout<<x<< <<y<<endl;
cout<<setiosflags(ios::fixed); //
cout<<x<< <<y<<endl;
}
25 -4.762
25.000000 -4.762000
2.5e+01 -4.762e+00
25 -4.762

1
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
void main( )

{ double x=sqrt(2.0);
for(int index=0; index<=7; index++)
{ // ios

cout.precision(index);
cout << x << endl;
}
cout << endl;
for(index=0; index<=9; index++)
{ cout << setprecision(index)
<< x << endl; //
}
}

0 6

2
#include<iostream.h>
#include<iomanip.h>
void main( )
{ int x=12345;
cout << "Default is on right:" << endl
<< setw(10) << x << endl << endl; //
cout.setf(ios::left, ios::adjustfield); //

cout << setw(10) << x << endl << endl;

35
cout.unsetf(ios::left); //

cout << setw(10) << x << endl;


}

3
#include<iostream.h>
#include<iomanip.h>
void main( )

{ int x=12345;

cout << "Default is on right:" << endl


<< setw(10) << x << endl << endl; //
cout.setf(ios::right, ios::adjustfield); //

cout.fill('*');
cout << setw(10) << x << endl << endl;
cout.setf(ios::left, ios::adjustfield); //
cout << setw(10) << setfill('%') << x << endl;
}

4
#include<iostream.h>
void main( )
{ double x=0.001234567, y=1.946e9;
cout << "Displayed in default format:" << endl
<< x << '\t ' << y << endl << endl; //
cout.setf(ios::scientific, ios::floatfield); //

cout << "Displayed in scientific format:" << endl


<< x << '\t' << y << endl;

cout.setf(ios::fixed, ios::floatfield); //
cout << "Displayed in fixed format:" << endl
<< x << '\t' << y << endl;
}

5
#include <iostream.h>
#include <iomanip.h>
void main()
{ double amount=22.0/7;
cout<<amount<<endl;

36
cout<<setprecision(0)<<amount<<endl<<setprecision(1)<<amount<<endl
<<setprecision(2)<<amount<<endl<<setprecision(3)<<amount<<endl;
cout<<setiosflags(ios::scientific);
cout<<setprecision(8)<<amount<<endl;
cout<<setiosflags(ios::fixed);
cout<<setprecision(8)<<amount<<endl;
cout<<setprecision(6);
}

6
#include<iostream.h>
#include<iomanip.h>
void main( )
{ cout<<10.0/5<<endl;
cout<<setiosflags(ios::showpoint)
<<10/5<<endl;
cout<<setiosflags(ios::showpos)
<<10.0/5<<endl;
}
.7
.7
C++ h cpp obj exe
dat

txt ran
C++



ASCII ASCII




0 0 n-
1 n n
0
C++ ifstream
o fstream
fstream

/
ifstream istream ofstream ostream fstream iostream
fstream.h
#include<fstream.h>

37
ios

istream ostream

ifstream iostream ofstream

fstream


C++
1
2 #include "fstream.h"
3

/ :
1) fstream.h
2) ifstream myin; ofstream myout; fstream iofile;
3) open
4) /
5) close
4
1 << >>
>>;
<<; //
2 read()write()

1 open
ofstream myout; //
myout.open(, <>);//
myout.open(c:\\vc\\abc.txt,ios::ate);
c\vc abc.txt

ofstream*pmyfile=new ofstream; //
pmyfile-> open(, <>);//
2
ofstream <><>,<>

38
ofstream ofile(c:\\vc\\abc.txt,ios:: binary);
c\vc abc.txt

1\\
2


ios::in ifstream
ios::out ( ofstream )
ios::app
ios::trunc
ios::ate
ios::nocreate
ios::noreplace ios::ate ios::app
ios::binary
2
1 open()
open
<>.open<>,<>

2 write
write

3 put

cout<<put(A); //
cout<<A; //
4 close
close

ofstream
close

1
#include<fstream>
using namespace std;
void main( )
{ ofstream fout ; // fout
fout.open("d:\\text.txt",ios::out|ios::trunc) ;
fout << "I am a student!" <<endl ;
fout.close();
}

2
#include<fstream.h> //
void main( )
{ ofstream file1("d:\\test1.txt"); // file1 test1

39
// file1 0 ios::out
file1<< ; //

file1.close( ); //
}
3 :

1
/ <<>>
2
put write
get getline read

1
#include "iostream.h"
#include "fstream.h"
void main()
{ int i; char string[20];
ofstream myfout("d:\\keyword.txt",ios::out);
for (i = 0 ; i < 5 ; i++)
{ cin >> string;
myfout << string << endl;
}
myfout.close() ;
}

2
#include<iostream.h>
#include<fstream.h>
void main()
{ ofstream myfout;
char x;
myfout.open ("d:\\keyword.txt",ios::out);
cin.get(x);
for (;x!='#';)
{ myfout.put(x) ;
cin.get(x);
}
myfout.close( ) ;
}

3 test2.txt

#include<iostream.h>
#include<fstream.h> //

40
void main( )
{ ofstream myout("d:\\test2.txt"); // myout test
// ios::out
myout<<10<<" "<<123.45<<" "<<"object\n"; //

myout.close( ); //
}

#include<iostream.h>
#include<fstream.h> //
void main( )
{ ofstream myout("d:\\test2.txt"); // myout test
if( !myout) // myout=0 myout=1

{ cerr << "cannot open file: test2";


}

myout<<10 << " "<< 123.45 <<" "<<"object\n"; //

myout.close( ); //
}

4
#include <fstream.h>
struct Date
{ int mo,da,yr;
};
void main()
{ Date dt = {6,10,92};
ofstream tfile("d:\\date.dat",ios::binary);
tfile.write((char *) &dt,sizeof(dt));
}
write char

char

get() put() read()


write()

1 open
ifstream myin; //
myin.open(, <>);//

41
ifstream*pmyfile=new ifstream; //
pmyfile-> open(, <>);//
2
ifstream <><>,<>

1 open()
open
<>.open<>,<>


ios::in ifstream
ios::out ( ofstream )
ios::app
ios::trunc
ios::ate
ios::nocreate
ios::noreplace ios::ate ios::app
ios::binary
2 get
>> get

3 getline

4 read
read

5 close
close ifstream

close

1
#include<iostream.h>
#include<fstream.h> //
void main( )
{ char ch;
ifstream file2("d:\\test1.txt"); // file2 test1
// ios::in
while(file2.get(ch))
cout<<
ch;
file2.close( ); //

42
}

2 test2

#include<iostream.h>
#include<fstream.h> //
void main( )
{ int a; float b;
char *str=new char[80];
ifstream myin("d:\\test2.txt"); // myin test2
if( !myin) //myin=0 myin=1

{ cerr<<"cannot open file: test2";


}

myin >> a >> b >> str; //


cout << a << " " << b << " " << " " << str; <<endl
myin.close( ); //
}

3
#include<iostream.h>
#include <fstream.h>
struct Date
{
int mo,da,yr;
};
void main()
{ Date dt;
ifstream tfile("d:\\date.dat",ios::binary);
tfile.read((char *) &dt,sizeof(dt));
cout<<dt.mo<<,<<dt.da<<,<<dt.yr<<endl;
}

4
#include<fstream.h>
void main( )
{ ofstream file1("d:\\test1.txt",ios::app);
file1<<; //

file1.close( );
char ch;
ifstream file2 ("d:\\test1.txt");
while(file2.get(ch))
cout<<

43
ch;
file2.close( ); //
}

5 test1.txt test2.txt

#include<iostream.h>
#include<fstream.h> //
void main( )
{ char ch;
ifstream myin("test1.txt",ios::in); // myin text1
if( !myin) //myin=0 myin=1

{ cout << "cannot open file:test1";


}
ofstream myout("test2.txt"); // myout file_to

if( !myout) //myout=0 myout=1

{ cout << "cannot open file: test2";


}

while(myin.get(ch))
myout.put(ch); //
myin.close( ); //
myout.close( ); //
}

6
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h> //
void main(int argc,char *argv[])
{ if(argc!=3)
{ cout<<*********Text File Copyer Version1.0**********<<endl;
exit(1);
}
ifstream input;
ofstream output;
input.open(argv[1]);
if(!input)
{ cout<<Cant open source file:<< argv[1]<<for copy!<<endl;
exit(1);
}
output.open(argv[2]);

44
if(!output)
{ cout<<Cant open the file:<< argv[2]<<for the copy!<<endl;
exit(1);
}
char ch;
cout<<Copying<<endl;
while (input.get(ch))
output.put(ch);
cout<<Finished!<<endl;
input.close();output. close();
}
/
fstream fstream
fstream

1
#include<iostream.h>
#include<fstream.h> //
void main( )
{ char ch;
fstream myin("test1.txt", ios::in|ios::app);/// myin
if(!myin) //myin=0 myin=1

{ cout << "cannot open file: test1.txt ";


}
myin<< ;
myin.seekg(0);//
while(myin>>ch)
{ cout<<
ch; }
myin.close( ); //
}

get() put()
read() write()
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
void copyFile()
{ ifstream sorcefile;
ofstream targetfile;

45
sorcefile.open("myimage.jpg", ios::in|ios::binary);
targetfile.open("myimagebak.jpg",ios::out|ios::binary);
char ch;
if(!sorcefile)
{ cerr << "" << endl;
exit(0);
}
while( sorcefile.get(ch))
targetfile.put(ch);
sorcefile.close();
targetfile.close();
}
void main()
{ cout << ""<< endl ;
copyFile();
}


1
getgetlineread
>> putwrite <<

while (file1.get (ch)) //


cout.put (ch);

EOF

clear()
2
C++ seekg() seekp()






istream 3
istream&istream::seekg(long pos);
istream&istream::seekg(long off,dir);
streampos istream::tellg();
pos
off dir

ios::cur
ios::beg
ios::end
tellg() long

46
ostream 3
ostream&ostream::seekp(long pos);
ostream&ostream::seekp(long off,dir);
streampos ostream::tellp();
3 3

3
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include<stdlib.h>
void main()
{ int j;
fstream file("hello.txt",ios::in|ios::out|ios::app|ios::binary);
if(!file)
{ cerr << ""<<endl;
exit(-1);
}
for (int i = 0; i < 20 ; i++)
{ j = i * 2;
//
file.write((char *)&j,sizeof(int));
}
file.close();
file.open("hello.txt" , ios::in|ios::binary);
while(file.read((char *)&i , sizeof(int)))
cout << i << ' ';
file.clear();
file.seekg(0);
file.seekg(48);
file.read((char *)&i,sizeof(int));
cout << endl << " 13 " << i <<endl;
file.close();
}
file.write((char *)&j,sizeof(int));
file.read((char
*)&i,sizeof(int));
int 4 13
131*4=48
. 8
I/O
ios ios
ios

47

goodbit
eofbit
failbit I/O
badbit I/O
hardfail

ios

int rdstate()
int eof() 0
int fail() failbit 0 ,
int bad() badbit 0
int good() 0
int clear(int
flag=0)

hardfail clear()

1
#include<iostream.h>
void main( )
{ char ch;
if(cin.good())
{ cin>>ch;
}
if(ch!=()
{ cin.clear(cin.rdstate()|ios::badbit); // badbit
cout<<please input ch:<<endl;
cin.clear();
}
cin>>ch;
cout<<ch=<<ch<<endl;
}

48

You might also like