标签:
1 c++继承经典例子 2 #include <iostream.h> 3 class Base 4 { 5 private: 6 int b_number; 7 public: 8 Base( ){} 9 Base(int i) : b_number (i) { } 10 int get_number( ) {return b_number;} 11 void print( ) {cout << b_number << endl;} 12 }; 13 14 class Derived : public Base 15 { 16 private: 17 int d_number; 18 public: 19 // constructor, initializer used to initialize the base part of a Derived object. 20 Derived( int i, int j ) : Base(i), d_number(j) { }; 21 // a new member function that overrides the print( ) function in Base 22 void print( ) 23 { 24 cout << get_number( ) << " "; 25 // access number through get_number( ) 26 cout << d_number << endl; 27 } 28 }; 29 int main( ) 30 { 31 Base a(2); 32 Derived b(3, 4); 33 cout << "a is "; 34 a.print( ); // print( ) in Base 35 cout << "b is "; 36 b.print( ); // print( ) in Derived 37 cout << "base part of b is "; 38 b.Base::print( ); // print( ) in Base 39 return 0; 40 } 41 42 43 没有虚析构函数,继承类没有析构 44 //Example: non- virtual destructors for dynamically allocated objects. 45 46 #include <iostream.h> 47 #include <string.h> 48 class Thing 49 { public: 50 virtual void what_Am_I( ) {cout << "I am a Thing./n";} 51 ~Thing(){cout<<"Thing destructor"<<endl;} 52 }; 53 class Animal : public Thing 54 { 55 public: 56 virtual void what_Am_I( ) {cout << "I am an Animal./n";} 57 ~Animal(){cout<<"Animal destructor"<<endl;} 58 }; 59 void main( ) 60 { 61 Thing *t =new Thing; 62 Animal*x = new Animal; 63 Thing* array[2]; 64 array[0] = t; // base pointer 65 array[1] = x; 66 for (int i=0; i<2; i++) array->what_Am_I( ) ; 67 delete array[0]; 68 delete array[1]; 69 return ; 70 } 71 72 73 纯虚函数,多态 74 #include <iostream.h> 75 #include <math.h> 76 class Point 77 { 78 private: 79 double x; 80 double y; 81 public: 82 Point(double i, double j) : x(i), y(j) { } 83 void print( ) const 84 { cout << "(" << x << ", " << y << ")"; } 85 }; 86 class Figure 87 { 88 private: 89 Point center; 90 public: 91 Figure (double i = 0, double j = 0) : center(i, j) { } 92 93 Point& location( ) 94 { 95 return center; 96 } // return an lvalue 97 void move(Point p) 98 { 99 center = p; 100 draw( ); 101 } 102 virtual void draw( ) = 0; // draw the figure 103 virtual void rotate(double) = 0; 104 // rotate the figure by an angle 105 }; 106 class Circle : public Figure 107 { 108 private: 109 double radius; 110 public: 111 Circle(double i = 0, double j = 0, double r = 0) : Figure(i, j), radius(r) { } 112 void draw( ) 113 { 114 cout << "A circle with center "; 115 location( ).print( ); 116 cout << " and radius " << radius << endl; 117 } 118 void rotate(double) 119 { 120 cout << "no effect./n"; 121 } // must be defined 122 }; 123 class Square : public Figure 124 { 125 private: 126 double side; // length of the side 127 double angle; // the angle between a side and the x-axis 128 public: 129 Square(double i = 0, double j = 0, double d = 0, double a = 0) : Figure(i, j), side(d), angle(a) { } 130 void draw( ) 131 { 132 cout << "A square with center "; 133 location( ).print( ); 134 cout << " side length " << side << "./n" 135 << "The angle between one side and the X-axis is " << angle << endl; 136 } 137 void rotate(double a) 138 { 139 angle += a; 140 cout << "The angle between one side and the X-axis is " << angle << endl; 141 } 142 void vertices( ) 143 { 144 cout << "The vertices of the square are:/n"; 145 // calculate coordinates of the vertices of the square 146 } 147 }; 148 int main( ) 149 { 150 Circle c(1, 2, 3); 151 Square s(4, 5, 6); 152 Figure *f = &c, &g = s; 153 f -> draw( ); 154 f -> move(Point(2, 2)); 155 g.draw( ); 156 g.rotate(1); 157 158 s.vertices( ); 159 // Cannot use g here since vertices( ) is not a member of Figure. 160 return 0; 161 } 162 //////////////////////////////////////////////////////////////////// 163 #include <iostream.h> 164 #include <string.h> 165 class Thing 166 { 167 public: 168 virtual void what_Am_I( ) {cout << "I am a Thing./n";} 169 ~Thing(){cout<<"Thing destructor"<<endl;} 170 }; 171 class Animal : public Thing 172 { 173 public: 174 virtual void what_Am_I( ) {cout << "I am an Animal./n";} 175 ~Animal(){cout<<"Animal destructor"<<endl;} 176 }; 177 void main( ) 178 { 179 Thing t ; 180 Animal x ; 181 Thing* array[2]; 182 array[0] = &t; // base pointer 183 array[1] = &x; 184 for (int i=0; i<2; i++) array->what_Am_I( ) ; 185 return ; 186 } 187 188 189 多继承 190 #include <iostream.h> 191 class A 192 { 193 private: 194 int a; 195 public: 196 A(int i) : a(i) { } 197 virtual void print( ) {cout << a << endl;} 198 int get_a( ) {return a;} 199 }; 200 class B 201 { 202 private: 203 int b; 204 public: 205 B(int j) : b(j) { } 206 void print( ) {cout << b << endl;} 207 int get_b( ) {return b;} 208 }; 209 class C : public A, public B 210 { 211 int c; 212 public: 213 C(int i, int j, int k) : A(i), B(j), c(k) { } 214 void print( ) {A::print( ); B::print( );} 215 // use print( ) with scope resolution 216 void get_ab( ) {cout << get_a( ) << " " << get_b( ) << endl;} 217 // use get_a( ) and get_b( ) without scope resolution 218 }; 219 int main( ) 220 { 221 C x(5, 8, 10); 222 A* ap = &x; 223 B* bp = &x; 224 ap -> print( ); // use C::print( ); 225 bp -> print( ); // use B::print( ); 226 // bp -> A::print( ); // as if x is inherited from B only, 227 // cannot access A::print( ); 228 x.A::print( ); // use A::print( ); 229 x.get_ab( ); 230 return 0; 231 } 232 233 234 共同基类的多继承 235 #include <iostream.h> 236 class R 237 {int r; 238 public: 239 R(int anInt){ r = anInt;}; 240 printOn(){ cout<<"r="<<r<<endl;} ; }; 241 class A : public R 242 { 243 int a; 244 public: 245 A(int int1,int int2):R(int2){ a = int1;};}; 246 class B : public R 247 { 248 int b; 249 public: 250 B(int int1,int int2):R(int2){ b = int1;};}; 251 class C : public A, public B 252 { 253 int c; 254 public: 255 C(int int1,int int2, int int3):A(int2,int3), B(int2,int3){ c = int1;} 256 }; 257 258 int main( ) 259 { 260 int i; 261 R rr(10); 262 A aa(20,30); 263 B bb (40,50); 264 C cc(5, 7, 9); 265 rr.printOn(); 266 aa.printOn(); //inherits R printOn 267 bb.printOn(); //inherits R printOn 268 //cc.printOn(); //would give error 269 return 0;} 270 271 272 虚基类 273 274 #include <iostream.h> 275 class R 276 { int r; 277 public: 278 R (int x = 0) : r(x) { } // constructor in R 279 void f( ){ cout<<"r="<<r<<endl;} 280 void printOn(){cout<<"printOn R="<<r<<endl;} 281 }; 282 class A : public virtual R 283 { int a; 284 public: 285 A (int x, int y) : R(x), a(y) { } // constructor in A 286 void f( ){ cout<<"a="<<a<<endl;R::f();} 287 }; 288 class B : public virtual R 289 {int b; 290 public: 291 B(int x, int z) : R(x), b(z) { }// constructor in B 292 void f( ){ cout<<"b="<<b<<endl;R::f();} 293 }; 294 class C : public A, public B 295 { int c; 296 public: 297 // constructor in C, which constructs an R object first 298 C(int x, int y, int z, int w) : R(x), A(x, y), B(x, z), c(w) { } 299 300 void f( ){ cout<<"c="<<c<<endl;A::f(); B::f();} 301 }; 302 void main() 303 { R rr(1000); 304 A aa(2222,444); 305 B bb(3333,111); 306 C cc(1212,345,123,45); 307 cc.printOn(); //uses R printOn but only 1 R..no ambiguity 308 cc.f(); // shows multiple call of the R::f() 309 } 310 311 //////////////////////////////////////// 312 #include <iostream.h> 313 class R 314 { int r; 315 public: 316 R (int x = 0) : r(x) { } // constructor in R 317 void f( ){ cout<<"r="<<r<<endl;} 318 }; 319 class A : virtual public R 320 { int a ; 321 protected: 322 void fA( ){cout<<"a="<<a<<endl;}; 323 public: 324 A (int x, int y) : R(x), a(y) { } // constructor in A 325 void f( ) {fA( ); R::f( );} 326 }; 327 class B : virtual public R 328 { int b; 329 protected: 330 void fB( ){cout<<"b="<<b<<endl;}; 331 public: 332 B (int x, int y) : R(x), b(y) { } // constructor in A 333 void f( ) {fB( ); R::f( );} 334 }; 335 336 class C : public A, public B 337 { int c; 338 protected: 339 void fC( ){ cout<<"c="<<c<<endl;}; 340 public: 341 C(int x, int y, int z, int w) : R(x), A(x, y), B(x, z), c(w) { } 342 void f( ) 343 { 344 R::f( ); // acts on R stuff only 345 A::fA( ); //acts on A stuff only 346 B::fB( ); // acts on B stuff only 347 fC( ); // acts on C stuff only 348 } 349 }; 350 void main() 351 { R rr(1000); 352 A aa(2222,444); 353 B bb(3333,111); 354 C cc(1212,345,123,45); 355 cc.f(); 356 } 357 358 359 私有继承 360 361 // Access levels 362 #include <iostream.h> 363 class Base 364 { 365 private: 366 int priv; 367 protected: 368 int prot; 369 int get_priv( ) {return priv;} 370 public: 371 int publ; 372 Base( ); 373 Base(int a, int b, int c) : priv(a), prot(b), publ(c) { } 374 int get_prot( ) {return prot;} 375 int get_publ( ) {return publ;} 376 }; 377 class Derived1 : private Base // private inheritance 378 { 379 public: 380 Derived1 (int a, int b, int c) : Base(a, b, c) { } 381 int get1_priv( ) {return get_priv( );} 382 // priv not accessible directly 383 int get1_prot( ) {return prot;} 384 int get1_publ( ) {return publ;} 385 }; 386 class Leaf1 : public Derived1 387 { 388 public: 389 Leaf1(int a, int b, int c) : Derived1(a, b, c) { } 390 void print( ) 391 { 392 cout << "Leaf1 members: " << get1_priv( ) << " " 393 // << get_priv( ) // not accessible 394 << get1_prot( ) << " " 395 // << get_prot( ) // not accessible 396 // << publ // not accessible 397 << get1_publ( ) << endl; 398 } // data members not accessible. get_ functions in Base not accessible 399 }; 400 class Derived2 : protected Base // protected inheritance 401 { 402 public: 403 Derived2 (int a, int b, int c) : Base(a, b, c) { } 404 }; 405 class Leaf2 : public Derived2 406 { 407 public: 408 Leaf2(int a, int b, int c) : Derived2(a, b, c) { } 409 void print( ) 410 { 411 cout << "Leaf2 members: " << get_priv( ) << " " 412 // << priv // not accessible 413 << prot << " " 414 << publ << endl; 415 } // public and protected data members accessible. get_ functions in Base accessible. 416 }; 417 class Derived3 : public Base // public inheritance 418 { 419 public: 420 Derived3 (int a, int b, int c) : Base(a, b, c) { } 421 }; 422 class Leaf3 : public Derived3 423 { 424 public: 425 Leaf3(int a, int b, int c) : Derived3(a, b, c) { } 426 void print( ) 427 { 428 cout << "Leaf3 members: " << get_priv( ) << " " 429 << prot << " " 430 << publ << endl; 431 } // public and protected data members accessible. get_ functions in Base accessible 432 }; 433 int main( ) 434 { 435 Derived1 d1(1, 2, 3); 436 Derived2 d2(4, 5, 6); 437 Derived3 d3(7, 8, 9); 438 // cout << d1.publ; // not accessible 439 // cout << d1.get_priv( ); // not accessible 440 // cout << d2.publ; // not accessible 441 // cout << d2.get_priv( ); // not accessible 442 cout << d3.publ; // OK 443 cout << d3.get_prot( ); // OK 444 Leaf1 lf1(1, 2, 3); 445 Leaf2 lf2(4, 5, 6); 446 Leaf3 lf3(7, 8, 9); 447 // cout << lf1.publ << endl; // not accessible 448 // cout << lf2.publ << endl; // not accessible 449 cout << lf3.publ << endl; // OK 450 return 0; 451 } 452 453 454 多级继承 455 // Point-Circle-Cylinder 456 #include <iostream.h> 457 // THE POINT CLASS 458 class Point 459 { 460 friend ostream & operator<<(ostream &,Point &); 461 public: 462 463 // constructor 464 Point (double xval =0, double yval=0 ) 465 { x=xval; y=yval;}; 466 protected: // accessed by derived class 467 double x; 468 double y; 469 }; 470 ostream & operator << (ostream & os, 471 Point & apoint) 472 { 473 cout <<" Point:X:Y: "<<apoint.x << "," 474 << apoint.y<< "/n"; 475 return os; 476 } 477 //The Circle class inherits from class Point 478 class Circle : public Point 479 { 480 friend ostream & operator<<(ostream &,Circle&); 481 public: 482 Circle (double r=0,double xval=0,double yval=0) 483 :Point(xval,yval), radius(r) 484 { 485 //radius = r; 486 } 487 double area() 488 { 489 return (3.14159* radius *radius); 490 } 491 protected: 492 double radius; 493 }; 494 495 //note casting circle to point 496 ostream & operator <<(ostream & os, Circle & aCircle) 497 { 498 cout<< "Circle:radius:" << aCircle.radius; 499 os<< aCircle.x << "/n"; 500 os<< aCircle.y << "/n"; 501 return os; 502 } 503 // THE CYLINDER CLASS 504 class Cylinder : public Circle 505 { 506 friend ostream & operator << (ostream & ,Cylinder &); 507 public: 508 Cylinder (double hv=0,double rv=0, 509 double xv=0,double yv=0 ) 510 : Circle( xv,yv,rv) 511 { 512 height = hv; 513 } 514 double area ( ); 515 protected: // may have derived classes 516 double height; 517 }; 518 double Cylinder :: area ( ) 519 { // Note that cylinder area uses Circle area 520 return 2.0* Circle::area() + 2.0*3.14159* radius*height; 521 } 522 ostream & operator << (ostream & os, 523 Cylinder & acylinder) 524 { 525 cout << "cylinder dimensions: "; 526 cout << "x: " <<acylinder.x; 527 cout << " y: " <<acylinder.y ; 528 cout << " radius: " <<acylinder.radius ; 529 cout << " height: " <<acylinder.height 530 << endl; 531 return os; 532 } 533 int main(void) 534 { 535 Point p(2,3); 536 Circle c(7,6,5); 537 Cylinder cyl(10,11,12,13); 538 cout << p; 539 cout << c; 540 cout << "area of cirle:" << c.area() << endl; 541 cout<< cyl; 542 cout<<"area of cylinder:"<< cyl.area()<<endl ; 543 cout<<"area of cylinder base is " 544 << cyl.Circle::area() << endl; 545 return 0; 546 } 547 548 549 protected 访问控制属性在继承的意义 550 551 //Example of treating derived class object as base class objects. Point------Circle 552 #include <iostream.h> 553 // THE POINT CLASS 554 class Point 555 { 556 friend ostream & operator<<(ostream &,Circle&); 557 public: 558 Point (double xval =0, double yval=0 ) { x=xval; y=yval;}; 559 public: 560 void print() 561 { 562 cout <<" Point:X:Y: "<<x << "," <<y<< "/n"; 563 } 564 protected: // accessed by derived class 565 double x; double y; 566 }; 567 ostream & operator << (ostream & os, Point & apoint) 568 { 569 cout <<" Point:X:Y: "<<apoint.x << ","<< apoint.y<< "/n"; 570 return os; 571 } 572 573 //The Circle class inherits from class Point 574 class Circle : public Point 575 { 576 friend ostream & operator<<(ostream &,Circle&); 577 public: 578 Circle (double r=0,double xval=0,double yval=0):Point(xval,yval) 579 { radius = r;}; 580 void print() 581 { 582 cout<< "Circle:radius:" <<radius<<endl; 583 cout <<" Point:X:Y: "<<x << "," <<y<< "/n"; 584 } 585 double area() 586 { return (3.14159* radius *radius);}; 587 protected: 588 double radius; 589 }; 590 //note casting circle to point 591 ostream & operator <<(ostream & os, Circle & aCircle) 592 { 593 cout<< "Circle:radius:" << aCircle.radius; 594 cout<< (Point) aCircle << "/n"; 595 return os; 596 } 597 598 //We will look at a few main programs based on previous class definitions. Casting and assignments 599 void main (void ) 600 { 601 Point p(2,3); cout <<"Point P= "<< p; 602 Point pp(0,0); cout <<"Point PP= "<< pp; 603 Circle c(7,6,5); cout <<"Circle c= "<< c; //radius =7 604 pp = p; cout <<"Point PP= "<< pp; //built in assign = 605 // a circle is a member of the point class so assign a circle to a point. 606 pp = c; //legal; also assignment O.K. 607 cout <<"Point PP= "<< pp; 608 pp= (Point) c; // but better use the cast 609 cout <<"Point PP= "<< pp; //note we get only the point part of the Circle 610 //c = (Circle) pp; // illegal Cannot convert ‘class Point‘ to ‘class Circle‘ 611 //c=pp; //illegal assignment not defined 612 Point* p; 613 p = &c; 614 P->print(); //call base class print 615 ((Circle*)p)->print(); 616 Point& r = c; 617 r.print(); 618 ((Circle&)r).print(); 619 } 620 621 622 类的兼容性规则 623 624 625 #include <iostream.h> 626 class Base 627 { 628 public: 629 void func( ) 630 {cout << "Base class function./n";} 631 }; 632 class Derived : public Base 633 { 634 public: 635 void func( ) 636 {cout << "Derived class function./n";} 637 }; 638 void foo(Base b) 639 { b.func( ); } 640 int main( ) 641 { 642 Derived d; 643 Base b; 644 Base * p = &d; 645 Base& br = d; 646 b = d; 647 b.func( ); 648 d.func( ); 649 p -> func( ); 650 foo(d); 651 br.func( ); 652 return 0; 653 } 654 655 656 657 658 虚析构函数,防止内存泄露 659 660 661 #include <iostream.h> 662 #include <string.h> 663 class Base 664 { 665 protected: 666 int id; 667 char * name; 668 public: 669 // default constructor 670 Base(int a = 0, char * s = "") : id(a) 671 { 672 if (!s) 673 { 674 name = NULL; 675 } 676 else 677 { 678 name = new char[strlen(s) + 1]; 679 strcpy(name, s); 680 } 681 cout << "base default constructor/n"; 682 } 683 // copy constructor 684 Base(const Base& b) : id(b.id) 685 { 686 if (!b.name) { name = NULL; } 687 else 688 { 689 name = new char[strlen(b.name) + 1]; 690 strcpy(name, b.name); 691 } 692 cout << "base copy constructor/n"; 693 } 694 // destructor 695 ~Base( ) 696 { 697 if( name != NULL ) delete [ ] name; 698 cout << "base destructor/n"; 699 } 700 const Base& operator= (const Base& b); 701 friend ostream& operator << (ostream&, const Base&); 702 }; 703 const Base& Base:perator= (const Base& b) 704 { 705 if (this != &b) // Check if an object is assigned to itself. 706 { 707 id = b.id; 708 delete [ ] name; // Destroy the old object. 709 if (!b.name) { name = NULL; } 710 else 711 { 712 name = new char[strlen(b.name) + 1]; 713 strcpy(name, b.name); 714 } 715 } 716 cout << "base assignment operator/n"; 717 return *this; 718 } 719 ostream& operator << (ostream& out, const Base& b) 720 { 721 out << "Base member id = " << b.id << endl; 722 out << "Base member name = " << b.name << endl; 723 724 return out; 725 } 726 class Derived : public Base 727 { 728 private: 729 float f; 730 char * label; 731 public: 732 // default constructor 733 Derived(int a = 0, char * s = "", float x = 0, char * t = "") : Base(a, s), f(x) 734 { 735 if (!t) { label = NULL; } 736 else 737 { 738 label = new char [strlen(t) + 1]; 739 strcpy(label, t); 740 } 741 cout << "derived default constructor/n"; 742 } 743 // copy constructor 744 Derived(const Derived& d) : Base(d), f(d.f) 745 // d used as an instance of Base 746 { 747 if(!d.label) { label = NULL; } 748 else 749 { 750 label = new char [strlen(d.label) + 1]; 751 strcpy(label, d.label); 752 } 753 cout << "derived copy constructor/n"; 754 } 755 // destructor 756 ~Derived( ) 757 { 758 delete [ ] label; 759 cout << "derived destructor/n"; 760 } 761 const Derived& operator= (const Derived& d); 762 friend ostream& operator << (ostream&, const Derived&); 763 }; 764 const Derived& Derived:perator= (const Derived& d) 765 { 766 if (this != &d) 767 { 768 delete [ ] label; 769 Base:perator=(d); // Assign the Base part of d to the Base 770 // part of the object that calls this operator; 771 f = d.f; 772 if (!d.label) { label = NULL; } 773 else 774 { 775 label = new char [strlen(d.label) + 1]; 776 strcpy(label, d.label); 777 } 778 cout << "derived assignment operator/n"; 779 } 780 return *this; 781 } 782 ostream& operator << (ostream& out, const Derived& d) 783 { 784 out << (Base)d; // Convert d to Base object to output Base members. 785 out << "Derived member f = " << d.f << endl; 786 out << "Derived member label = " << d.label << endl; 787 return out; 788 } 789 int main( ) 790 { 791 Derived d1; 792 Derived d2(d1); 793 return 0; 794 }
http://blog.csdn.net/zhaori/article/details/1700356
标签:
原文地址:http://www.cnblogs.com/forcheryl/p/4204663.html