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;}
阅读全文
0 0
- c
- c
- c
- c
- C
- c
- c
- c
- C+
- c
- C
- c
- c
- c
- C
- C
- c
- C
- java 学习 第二天 核心类
- Java-json解析:GSON解析,java bean 编写
- U-boot引导流程
- Bootstrap表格示例
- Python3 pip3 list 出现 DEPRECATION 警告
- 053、054、055-C++
- tar && split
- 稀疏表示与匹配追踪
- C语言实现 单向链表
- php调用java webservice接口
- 比特币与区块链概述
- Android studio 2.2 2.3 2.4 如何使用kotlin插件
- Spring和MyBatis环境整合
- chrome插件开发:content、background和popup三者之间的通信