继承与多态
继承
父子出现同名的成员问题
# include <iostream> using namespace std;
class Father {
public : string name;
protected : int pwd;
private : int money;
public : Father ( ) { cout<< "Father::构造" << endl; } Father ( string n, int p, int m) : name ( n) , pwd ( p) , money ( m) { cout<< "Father::有参" << endl; } } ;
class Son : public Father {
public : int name;
} ;
class Test {
public : Father f;
} ; int main ( )
{ Father father; Son son; Test test; cout<< "&son=" << & son. name<< endl; cout<< "&father=" << & son. Father:: name<< endl; cout<< "&test=" << & test<< endl; cout<< "&test.f=" << & test. f<< endl; return 0 ;
}
关于继承中特殊成员函数
# include <iostream> using namespace std;
class Father {
public : string name;
protected : int pwd;
private : int money;
public : Father ( ) { cout<< "Father::构造" << endl; } Father ( string n, int p, int m) : name ( n) , pwd ( p) , money ( m) { cout<< "Father::有参" << endl; } ~ Father ( ) { cout<< "析构" << endl; } Father ( const Father & other) : name ( other. name) , pwd ( other. pwd) , money ( other. money) { cout<< "Father::拷贝构造" << endl; } Father& operator = ( const Father& other) { if ( this != & other) { this -> name= other. name; this -> pwd= other. pwd; this -> money= other. money; } cout<< "Father::拷贝赋值函数" << endl; return * this ; } void show ( ) { cout<< "Father::name = " << name<< endl; cout<< "Father::pwd = " << pwd<< endl; cout<< "Father::money = " << money<< endl; } } ;
class Son : public Father { private : string toy;
public : Son ( ) { cout<< "Son::无参构造" << endl; } ~ Son ( ) { cout<< "Son::析构函数" << endl; } Son ( const Son& other) : Father ( other) , toy ( other. toy) { cout<< "Son::拷贝构造函数" << endl; } Son ( string n, int p, int m, string t) : Father ( n, p, m) , toy ( t) { cout<< "Son::有参构造" << endl; } void show ( ) { Father :: show ( ) ; cout<< "toy = " << toy<< endl; } } ; int main ( )
{ Son s1 ( "xx" , 123 , 5 , "a" ) ; s1. show ( ) ; Son s2 ( s1) ; s2. show ( ) ; return 0 ;
}
练习:将图形类的获取周长和获取面积函数设置成虚函数,完成多态 再定义一个全局函数,能够在该函数中实现:无论传递任何图形,都可以输出传递的图形的周长和面积
# include <iostream> using namespace std; class Shape { double perimeter; double area;
public : Shape ( ) { cout<< "Shape::无参" << endl; } Shape ( const Shape & other) : perimeter ( other. perimeter) , area ( other. area) { cout<< "拷贝构造函数" << endl; } Shape & operator = ( const Shape & other) { if ( this == & other) { return * this ; } perimeter= other. perimeter; area= other. area; return * this ; } ~ Shape ( ) { cout<< "shape::析构" << endl; } virtual void show ( ) { cout<< "perimeter" << perimeter<< endl; cout<< "area" << area<< endl; } double getPer ( ) { return perimeter; } double getArea ( ) { return area; } void setPer ( double p) { this -> perimeter= p; } void setArea ( double a) { this -> area= a; } } ; class Rectangle : public Shape
{
private : int wide; int high;
public : Rectangle ( ) { } Rectangle ( int w, int h) : wide ( w) , high ( h) { } ~ Rectangle ( ) { } Rectangle ( const Rectangle & other) : Shape ( other) , wide ( other. wide) , high ( other. high) { } Rectangle & operator = ( const Rectangle & other) { if ( this != & other) { Shape:: operator = ( other) ; this -> wide = other. wide; this -> high = other. high; } return * this ; } void show ( ) { cout<< "矩形 长:" << wide<< " 宽:" << high<< endl; cout<< "perimeter" << get_perimeter ( ) << endl; cout<< "area" << get_area ( ) << endl; } int get_perimeter ( ) { this -> setPer ( 2.0 * ( wide+ high) ) ; return Shape :: getPer ( ) ; } int get_area ( ) { this -> setArea ( 1.0 * wide* high) ; return Shape :: getArea ( ) ; }
} ;
class Circle : public Shape
{
private : int radius;
public : Circle ( ) { } Circle ( int r) : radius ( r) { } ~ Circle ( ) { } Circle ( const Circle & other) : Shape ( other) , radius ( other. radius) { } Circle & operator = ( const Circle & other) { if ( this != & other) { Shape:: operator = ( other) ; this -> radius = other. radius; } return * this ; } void show ( ) { cout<< "圆形 半径:" << radius<< endl; cout<< "perimeter" << get_perimeter ( ) << endl; cout<< "area" << get_area ( ) << endl; } int get_perimeter ( ) { this -> setPer ( 2 * 3.14 * radius) ; return Shape :: getPer ( ) ; } int get_area ( ) { this -> setArea ( 3.14 * radius* radius) ; return Shape :: getArea ( ) ; } } ;
void display ( Shape & shape) { shape. show ( ) ;
}
int main ( )
{ Rectangle rec ( 10 , 20 ) ; Circle cir ( 10 ) ; display ( rec) ; display ( cir) ; return 0 ;
}
手动实现一个循环顺序队列
# include <iostream>
using namespace std;
class myqueue {
int front;
int rear;
int maxSize;
int * data; public :
myqueue ( ) : front ( 0 ) , rear ( 0 ) , maxSize ( 0 ) , data ( NULL ) { }
myqueue ( int k) : front ( 0 ) , rear ( 0 ) , maxSize ( k+ 1 ) , data ( new int [ k+ 1 ] ) {
cout<< "有参构造" << endl;
}
myqueue ( const myqueue & other) : front ( other. front) , rear ( other. rear) , maxSize ( other. maxSize) , data ( new int [ other. maxSize] ) { memcpy ( data, other. data, sizeof ( int ) * maxSize) ;
}
myqueue & operator = ( const myqueue & other) { if ( this == & other) return * this ; front= other. front; rear= other. rear; maxSize= other. maxSize; int * temp= new int [ maxSize] ; delete [ ] data; data= temp; return * this ;
} int myfront ( ) { return data[ front] ;
}
int back ( ) { if ( empty ( ) ) { return - 1 ; } return data[ ( rear- 1 + maxSize) % maxSize] ;
}
bool empty ( ) { return rear== front;
}
bool full ( ) { return ( rear+ 1 ) % maxSize== front;
}
int size ( ) { return ( front- rear+ maxSize) % maxSize;
}
bool push ( int val) {
if ( full ( ) ) return false ;
data[ rear] = val;
rear= ( rear+ 1 ) % maxSize;
return true ;
}
int pop ( ) { if ( empty ( ) ) return - 1 ; int res= data[ front] ; front= ( front+ 1 ) % maxSize; return res;
}
void show ( ) { for ( int i= front; i!= rear; i= ( i+ 1 ) % maxSize) { cout<< data[ i] << " " ; } cout<< endl;
}
} ; int main ( ) { myqueue a ( 5 ) ; a. push ( 1 ) ; a. push ( 2 ) ; a. push ( 3 ) ; a. push ( 4 ) ; a. push ( 5 ) ; a. pop ( ) ; a. push ( 6 ) ; a. show ( ) ;
}