053、054、055-C++

来源:互联网 发布:大数据技术书籍推荐 编辑:程序博客网 时间:2024/05/17 08:41

053

#include<iostream>#include<assert.h>using namespace std;class A{public:  int a;  A(){    a1 = 1;    a2 = 2;    a3 = 3;    a = 4;  }  void fun(){    cout << a << endl;    //正确    cout << a1 << endl;   //正确    cout << a2 << endl;   //正确    cout << a3 << endl;   //正确  }public:  int a1;protected:  int a2;private:  int a3;};class B : protected A{public:  int a;  B(int i){    A();    a = i;  }  void fun(){    cout << a << endl;       //正确,public成员。    cout << a1 << endl;       //正确,基类的public成员,在派生类中变成了protected,可以被派生类访问。    cout << a2 << endl;       //正确,基类的protected成员,在派生类中还是protected,可以被派生类访问。    cout << a3 << endl;       //错误,基类的private成员不能被派生类访问。  }};int main(){  B b(10);  cout << b.a << endl;       //正确。public成员  cout << b.a1 << endl;      //错误,protected成员不能在类外访问。  cout << b.a2 << endl;      //错误,protected成员不能在类外访问。  cout << b.a3 << endl;      //错误,private成员不能在类外访问。  system("pause");  return 0;}

054

#include<iostream>#include<assert.h>using namespace std;class A{public:  int a;  A(){    a1 = 1;    a2 = 2;    a3 = 3;    a = 4;  }  void fun(){    cout << a << endl;    //正确    cout << a1 << endl;   //正确    cout << a2 << endl;   //正确    cout << a3 << endl;   //正确  }public:  int a1;protected:  int a2;private:  int a3;};class B : private A{public:  int a;  B(int i){    A();    a = i;  }  void fun(){    cout << a << endl;       //正确,public成员。    cout << a1 << endl;       //正确,基类public成员,在派生类中变成了private,可以被派生类访问。    cout << a2 << endl;       //正确,基类的protected成员,在派生类中变成了private,可以被派生类访问。    cout << a3 << endl;       //错误,基类的private成员不能被派生类访问。  }};int main(){  B b(10);  cout << b.a << endl;       //正确。public成员  cout << b.a1 << endl;      //错误,private成员不能在类外访问。  cout << b.a2 << endl;      //错误, private成员不能在类外访问。  cout << b.a3 << endl;      //错误,private成员不能在类外访问。  system("pause");  return 0;}

055

#include<iostream>usingnamespacestd;classLine{public:voidsetLength(doublelen);doublegetLength(void);Line();// 这是构造函数private:doublelength;};// 成员函数定义,包括构造函数Line::Line(void){cout << "Object is being created" << endl;}voidLine::setLength(doublelen){length = len;}doubleLine::getLength(void){returnlength;}// 程序的主函数intmain(){Lineline;// 设置长度line.setLength(6.0);cout << "Length of line : " << line.getLength() <<endl;return0;}