改错
# include <iostream>
using namespace std;
class BaseClass {
public:
int x;
} ;
class ClassA: public BaseClass
{ protected:
int y;
} ;
class ClassB: public BaseClass {
protected:
int z;
} ;
class Derived: public ClassA, public ClassB
{ public:
int x;
Derived ( ) {
x= 1 , y= 2 , z= 3 ;
ClassA:: x= 4 ; ClassB:: x= 5 ;
}
void Display ( ) {
cout<< ClassA:: x<< ClassB:: x<< x<< y<< z<< endl;
}
} ;
int main ( ) {
Derived d;
d. Display ( ) ;
d. ClassA:: x= 6 ;
d. Display ( ) ;
return 0 ;
}
2
# include <iostream>
using namespace std;
class base {
public:
void virtual f1 ( )
{ cout<< "f1 function of base" << endl; }
void f2 ( ) { cout<< "f2 function of base " << endl; }
} ;
class derive: public base
{ public:
void f1 ( ) { cout<< "f1 function of derive" << endl; }
void f2 ( ) { cout<< "f2 function of derive " << endl; }
} ;
int main ( ) {
base * p;
base obj1;
derive obj2;
p= & obj1; p-> f1 ( ) ; p-> f2 ( ) ;
p= & obj2; p-> f1 ( ) ; p-> f2 ( ) ;
return 0 ;
}
程序设计
1
# include <iostream>
using namespace std;
class Base1 {
public:
Base1 ( int x) { b1= x; cout<< "A" ; }
int b1;
} ;
class Base2 {
public:
Base2 ( ) { b2+= 2 ; cout<< "B" ; }
static int b2 ;
} ;
int Base2:: b2= 0 ;
class Base3 {
public:
Base3 ( int y) : b3 ( y) { cout<< "C" ; }
int b3;
} ;
class Derived: public Base2, public Base3, public Base1{
public:
Derived ( int x, int y) : Base1 ( x) , Base3 ( y) { }
void Display ( ) const {
cout<< b1<< ", " << b2<< ", " << b3<< endl; }
} ;
int main ( ) {
const Derived d ( 4 , 6 ) ;
cout<< endl;
d. Display ( ) ;
cout<< d. b1<< "; " << d. b2<< "; " << d. b3<< endl;
return 0 ;
}
2
# include <iostream>
using namespace std;
class fract {
int num;
int den;
public:
fract ( int n= 0 , int d= 1 ) : num ( n) , den ( d) { }
fract & operator += ( const fract& ) ;
void show ( ) const { cout<< num<< '/' << den<< endl; }
friend istream & operator >> ( istream & , fract & ) ;
} ;
fract & fract:: operator += ( const fract& f)
{
this-> num= this-> num* f. den+ this-> den* f. num;
this-> den*= f. den;
return * this;
}
istream & operator >> ( istream & in, fract & f) {
in>> f. num>> f. den;
return in;
}
int main ( ) {
int n, d;
cin>> n>> d;
fract fr1 ( n, d) , fr2;
cin>> fr2;
fr1+= fr2;
cout<< "fr1: " ; fr1. show ( ) ;
cout<< "fr2: " ; fr2. show ( ) ;
return 0 ;
}
3
# include <iostream>
using namespace std;
class Base {
public:
void virtual display ( ) {
cout<< "Base::display" << endl;
}
} ;
class Derive: public Base {
public:
void display ( ) const ;
void display ( ) { cout<< "Derive::display" << endl; }
} ;
void Derive:: display ( ) const {
cout<< "Derive::display const" << endl; }
void Display ( Base & p)
{ p. display ( ) ; }
int main ( ) {
Base b1; Display ( b1) ;
Derive d1; Display ( d1) ;
const Derive d2;
d2. display ( ) ;
return 0 ;
}
设计
1
# include <iostream>
# include <fstream>
using namespace std;
class Complex {
public:
Complex ( int r = 0 , int i = 0 ) : real ( r) , imag ( i) { }
bool operator== ( const Complex & c2) const ;
friend Complex& operator++ ( Complex & c) ;
friend ostream& operator<< ( ostream & , const Complex & ) ;
private:
int real;
int imag;
} ;
Complex& operator++ ( Complex & c) {
c. real++ ;
c. imag++ ;
return c;
}
bool Complex:: operator== ( const Complex & c2) const {
if ( this-> real== c2. real&& this-> imag== c2. imag) return true;
return false;
}
ostream & operator << ( ostream & out, const Complex & c) {
out << "(" << c. real << ", " << c. imag << ")" ;
return out;
}
int main ( ) {
int r1, i1, r2, i2;
cin>> r1>> i1>> r2>> i2;
Complex c1 ( r1, i1) , c2 ( r2, i2) , c3;
cout<< endl;
cout<< "c1的值为:" << c1<< endl;
cout<< "c2的值为:" << c2<< endl;
cout<< "c3的值为:" << c3<< endl;
if ( c1== c2) cout<< "c1==c2" << endl;
else cout<< "c1!=c2" << endl;
cout<< endl;
c3= ++ c1;
cout<< "c3=++c1的值为:" << c3<< endl;
if ( c1== c2) cout<< "c1==c2" << endl;
else cout<< "c1!=c2" << endl;
ifstream in1 ( "8.2.1.1-s3.in" ) ;
ofstream out1 ( "8.2.1.1-s3.out" ) ;
streambuf * cinbackup;
streambuf * coutbackup;
cinbackup= cin. rdbuf ( in1. rdbuf ( ) ) ;
coutbackup= cout. rdbuf ( out1. rdbuf ( ) ) ;
while ( cin>> r1>> i1>> r2>> i2) {
Complex c1 ( r1, i1) , c2 ( r2, i2) , c3;
cout<< "c1的值为:" << c1<< endl;
cout<< "c2的值为:" << c2<< endl;
cout<< "c3的值为:" << c3<< endl;
if ( c1== c2) cout<< "c1==c2" << endl;
else cout<< "c1!=c2" << endl;
c3= operator ++ ( c1) ;
cout<< "c3=++c1的值为:" << c3<< endl;
if ( c1. operator == ( c2) ) cout<< "c1==c2" << endl;
else cout<< "c1!=c2" << endl;
cout<< endl;
}
cin. rdbuf ( cinbackup) ;
cout. rdbuf ( coutbackup) ;
in1. close ( ) ;
out1. close ( ) ;
return 0 ;
}
2
# include <iostream>
# include <fstream>
using namespace std;
class Clock {
public:
Clock ( int h= 0 , int m= 0 , int s= 0 ) {
this-> hour= h; this-> minute= m; this-> second= s;
}
void setTime ( int newH, int newM, int newS) {
this-> hour= newH; this-> minute- newM; this-> second= newS;
}
Clock& operator++ ( ) ;
friend bool operator== ( const Clock & , const Clock & ) ;
friend ostream & operator<< ( ostream& , const Clock & ) ;
protected:
int hour, minute, second;
} ;
Clock & Clock:: operator++ ( ) {
second++ ;
if ( second>= 60 ) {
second-= 60 ;
minute++ ;
if ( minute>= 60 ) {
minute-= 60 ;
hour= ( hour+ 1 ) % 24 ;
}
}
return * this;
}
bool operator== ( const Clock & c1, const Clock & c2) {
if ( c1. hour== c2. hour&& c1. minute== c2. minute&& c1. second== c2. second) return true;
return false;
}
ostream & operator << ( ostream & out, const Clock & c) {
out << c. hour << ":" << c. minute << ":" << c. second;
return out;
}
int main ( )
{
int h, m, s;
cin>> h>> m>> s;
Clock c1 ( h, m, s) , c2= ++ c1;
cout<< "时钟c1:" << c1<< endl;
cout<< "时钟c2:" << c2<< endl;
if ( operator == ( c1, c2) ) cout<< "时钟c1与c2相等!" << endl;
else cout<< "时钟c1与c2不相等!" << endl;
cin>> h>> m>> s;
c2. setTime ( h, m, s) ;
cout<< "重置后时钟c2:" << c2<< endl;
if ( c1== c2) cout<< "时钟c1与c2相等!" << endl;
else cout<< "时钟c1与c2不相等!" << endl;
ifstream in1 ( "8.2.2.5_1-s1_in.dat" ) ;
ofstream out1 ( "8.2.2.5_1-s1_out.dat" ) ;
streambuf * cinbackup;
streambuf * coutbackup;
cinbackup= cin. rdbuf ( in1. rdbuf ( ) ) ;
coutbackup= cout. rdbuf ( out1. rdbuf ( ) ) ;
while ( cin>> h>> m>> s) {
Clock c1 ( h, m, s) , c2= c1. operator ++ ( ) ;
cout<< "时钟c1:" << c1<< endl;
cout<< "时钟c2:" << c2<< endl;
if ( operator == ( c1, c2) )
cout<< "时钟c1与c2相等!" << endl;
else cout<< "时钟c1与c2不相等!" << endl;
cin>> h>> m>> s;
c2. setTime ( h, m, s) ;
cout<< "重置后时钟c2:" << c2<< endl;
if ( c1== c2) cout<< "时钟c1与c2相等!" << endl;
else cout<< "时钟c1与c2不相等!" << endl;
cout<< endl;
}
cin. rdbuf ( cinbackup) ;
cout. rdbuf ( coutbackup) ;
in1. close ( ) ;
out1. close ( ) ;
return 0 ;
}