Chinaunix首页 | 论坛 | 博客
  • 博客访问: 758045
  • 博文数量: 231
  • 博客积分: 3217
  • 博客等级: 中校
  • 技术积分: 2053
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-04 12:01
文章分类

全部博文(231)

文章存档

2015年(1)

2013年(10)

2012年(92)

2011年(128)

分类: LINUX

2012-07-18 13:45:44

公用继承:
公用基类的成员                                    在公用派生类中的访问属性
私有成员                                          不可访问
公用成员                                          公用
保护成员                                          保护
 
私有继承:
私有基类中的成员                                  在私有派生类中的访问属性
私有成员                                          不可访问
公用成员                                          私有
保护成员                                          私有
 
保护成员和保护继承:
基类中的成员    在公用派生类中的访问属性   在私有派生类中的访问属性  在保护派生类中的访问属性
私有成员        不可访问                   不可访问                  不可访问
公用成员        公用                        私有                      保护
保护成员        保护                        私有                      保护
 
派生类中的成员的访问属性:
派生类中的成员                   在派生中           在派生类外部          在下层公用派生类中
派生类中访问属性为公用的成员     可以               可以                  可以
派生类访问属性为受保护的成员     可以               不可以                可以
派生类访问属性为私有成员         不可以             不可以                 不可以
在派生类中不可以访问的成员       不可以             不可以                 不可以

点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. class Stud{
  5.     public:
  6.         Stud(){num=12;name="logo";***='M';}
  7.         void disp();
  8.     protected:
  9.         int num;
  10.         string name;
  11.         char ***;
  12. };
  13. void Stud::disp()
  14. {
  15. cout<<"num:"<<num<<endl;
  16. cout<<"name:"<<name<<endl;
  17. cout<<"***:"<<***<<endl;
  18. }

  19. class Std1:protected Stud
  20. {
  21.     public:
  22.         Std1(){age=40;addr="longpengcheng";}
  23.         void disp1();
  24.     private:
  25.         int age;
  26.         string addr;
  27. };

  28. void Std1::disp1()
  29. {
  30. cout<<"num:"<<num<<endl;
  31. cout<<"name:"<<name<<endl;
  32. cout<<"***:"<<***<<endl;
  33. cout<<"age:"<<age<<endl;
  34. cout<<"address:"<<addr<<endl;
  35. }

  36. int main()
  37. {
  38. Std1 s;
  39. s.disp1();
  40. return 0;
  41. }
基类的构造函数和析构函数是不能继承的。继承过来的基类的成员初始化工作也要由派生类的构造函数承担,在执行派生类的构造函数时,调用基类的构造函数。
一般形式: 派生类构造函数名(总参数列表):基类构造函数名(参数列表)
{派生类中新增加的数据成员初始化语句}

点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. class Student{
  5.     public:
  6.         Student(int n,string nam,char s)
  7.         {        
  8.         num=n;
  9.         name=nam;
  10.         ***=s;        
  11.                 }
  12.         ~Student(){} //基类中到析构函数
  13.     protected:
  14.         int num;
  15.         string name;
  16.         char ***;
  17. };

  18. class Student1:public Student
  19. {
  20.     public:
  21.         Student1(int n,string nam,char s,int a,string ad):Student(n,nam,s){
  22.             age=a;
  23.             addr=ad;
  24. }//等效表达法: Student1(int n,string nam,char s,int a,string ad):Student(n,nam,s),age(a),addr(ad){}
  25.         void show()
  26.             {cout<<"num:"<<num<<endl;
  27.             cout<<"name:"<<name<<endl;
  28.             cout<<"***:"<<***<<endl;
  29.             cout<<"age:"<<age<<endl;
  30.             cout<<"address:"<<addr<<endl;
  31.                 }
  32.         ~Student1(){} //派生类到析构函数
  33.     private:
  34.         int age;
  35.         string addr;
  36. };
  37. int main()
  38. {
  39. Student1 std1(10010,"long",'M',12,"zhongguancun");
  40. Student1 std2(10011,"peng",'W',13,"guangdianyuan");
  41. std1.show();
  42. std2.show();
  43. return 0;
  44. }
在建立一个对象四,执行构造函数的顺利时:首先派生类中的构造函数先调用基类构造函数,在执行派生类的构造函数本身。
在派生类对象释放时,先执行派生类析构函数~Student1(),再执行其基类析构函数~Student().

多重继承引起的二义问题:

点击(此处)折叠或打开

  1. #include<iostream>
  2. using namespace std;
  3. class N
  4. {
  5. public:
  6.     int a;
  7.     void display(){cout<<"A:a= "<<a<<endl;}
  8. };

  9. class A:public N
  10. {
  11. public:
  12. int a1;
  13. };

  14. class B:public N
  15. {
  16. public:
  17.      int a2;
  18. };

  19. class C:public A,public B
  20. {
  21. public:
  22. int a3;
  23. void show(){cout<<"a3= "<<a3<<endl;}
  24. };
  25. int main()
  26. {
  27. C c1;
  28. c1.A::a=3;
  29. c1.A::display();
  30. c1.B::a=5;
  31. c1.B::display();
  32. c1.a3=50;
  33. c1.show();
  34. return 0;
  35. }
C c1;
c1.a3=3;
c1.display();
访问的是派生类C中的成员。规则是:基类的同名成员在派生类中被屏蔽,成为不可见的,或者说派生类中的新增加的同名成员覆盖积累中的同名成员。
c1.A::a=3;c1.A::display();这表示访问的是类N的派生类A中的基类成员。
 
虚基类:c++提供虚基类使得在继承间接共同基类时只保留一份成员。虚基类是在声明派生类时指定继承方式时声明的,因为一个派生类可以再生成一个派生类时作为虚基类,而在生成另一个派生不作为虚基类。
class 派生类名:virtual 继承方式 基类名
虚基类的初始化:在最后的派生类中不仅要负责对其派生类的初始化还要负责对虚基类的初始化。

点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. class Person
  5. {
  6. public:
  7.     Person(string nam,char s,int a){name=nam;***=s;age=a;}
  8. protected:
  9.     string name;
  10.     char ***;
  11.     int age;
  12. };

  13. class Teacher:virtual public Person
  14. {
  15. public:
  16.     Teacher(string nam,char s,int a,string t):Person(nam,s,a){title=t;}
  17. protected:
  18.     string title;
  19. };

  20. class Student:virtual public Person
  21. {
  22. public:
  23.     Student(string nam,char s,int a,float sco):Person(nam,s,a){score=sco;}
  24. protected:
  25.     float score;
  26. };

  27. class Graduate:public Teacher,public Student
  28. {
  29. public:
  30.     Graduate(string nam,char s,int a,string t,float sco,float w):Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco){wage=w;}
  31.     void show()
  32.     {cout<<"name:"<<name<<endl;
  33.     cout<<"***:"<<***<<endl;
  34.     cout<<"age:"<<age<<endl;
  35.     cout<<"title:"<<title<<endl;
  36.     cout<<"score:"<<score<<endl;
  37.     cout<<"wage:"<<wage<<endl;
  38.     }
  39. private:
  40.     float wage;
  41. };

  42. int main()
  43. {
  44. Graduate s("lpc",'M',24,"boshi",89,5555);
  45. s.show();
  46. return 0;
  47. }

点击(此处)折叠或打开

  1. 先建立一个Point(点)类,包含数据成员x,y(坐标点),以它为基类,派生一个Circle(圆)类,再增加数据成员r,再以Circle类为直接基类,派生一个Cylinder(圆柱体)类,再增加数据成员h。要求编写程序,重载运算符“<<”和“>>

  2. #include<iostream>
  3. using namespace std;
  4. class Point{
  5.     public:
  6.         Point(float x=0,float y=0);
  7.         void setPoint(float,float);
  8.         float getx(){return x;}
  9.         float gety(){return y;}
  10.         friend ostream & operator <<(ostream &,const Point &);    
  11.     protected:
  12.         float x;
  13.         float y;
  14. };

  15. Point::Point(float a,float b){x=a;y=b;}
  16. void Point::setPoint(float x1,float y1)
  17. {
  18. x=x1;y=y1;
  19. }

  20. ostream & operator<<(ostream &output,const Point &p)
  21. { output<<"["<<p.x<<","<<p.y<<"]"<<endl;
  22.     return output;
  23. }

  24. class Circle:public Point
  25. {
  26.     public:
  27.         Circle(float a,float b,float c):Point(a,b),r(c){}
  28.         void setr(float);
  29.         float getr(){return r;}
  30.         float area() const {return (3.14*r*r);}
  31.         friend ostream & operator <<(ostream &,const Circle &);
  32.     protected:
  33.         float r;
  34. };

  35. ostream & operator <<(ostream &output,const Circle &c)
  36. {
  37. output<<"cent:"<<"["<<c.x<<","<<c.y<<"],r="<<c.r<<",area="<<c.area()<<endl;
  38. return output;
  39. }

  40. void Circle::setr(float d)
  41. {r=d;}


  42. class Cylinder:public Circle
  43. {
  44.     public:
  45.         Cylinder(float a,float b,float c,float d):Circle(a,b,c),h(d){}
  46.         void seth(float d){h=d;}
  47.         float geth(){return h;}
  48.         float tiji() const;
  49.         friend ostream & operator <<(ostream &,const Cylinder &);
  50.     private:
  51.         float h;
  52. };

  53. ostream & operator<<(ostream &output,const Cylinder &cy)
  54. {
  55. output<<"cent:"<<"["<<cy.x<<","<<cy.y<<"],r="<<cy.r<<",area="<<cy.area()<<"h="<<cy.h<<"tiji:"<<cy.tiji()<<endl;
  56. }
  57. float Cylinder::tiji() const
  58. {
  59. return Circle::area()*h;
  60. }

  61. int main()
  62. {
  63. Cylinder c1(3.5,6.4,2.5,5.5);
  64. cout<<"cent:"<<"["<<c1.getx()<<","<<c1.gety()<<"],r="<<c1.getr()<<",area="<<c1.area()<<",h="<<c1.geth()<<",tiji="<<c1.tiji()<<endl;
  65. c1.setPoint(1.2,5.6);
  66. c1.setr(6);
  67. c1.seth(8);
  68. cout<<"cent:"<<"["<<c1.getx()<<","<<c1.gety()<<"],r="<<c1.getr()<<",area="<<c1.area()<<",h="<<c1.geth()<<",tiji="<<c1.tiji()<<endl;
  69. return 0;
  70. }


阅读(525) | 评论(0) | 转发(0) |
0

上一篇:C++学习笔记

下一篇:C++学习(五)

给主人留下些什么吧!~~