C++学习日志36-----继承与构造、控制访问属性


一、继承与构造

#include<iostream>
using std::cout;
using std::endl;
//任务1:继承构造函数
// 创建基类B及构造函数B(int),B( char)和派生类D; D中不继承/继承B的ctor时的效果
//任务2:派生类中调用基类构造函数
//D中增加成员double x; 及D(double), 在D(double)初始化列表调用B(i)并初始化x
//任务3:派生类先调基类ctor,再构造内嵌对象
// 增加类E及E(int),并再D中加入E的两个对象;创建D对象观察E ctor和B ctor次序

//构造时先调用基类构造函数
class B
{
    
    
public:
	B() {
    
     cout << "B()" << endl; }
	B(int i) {
    
     cout << "B(" << i << ")" << endl; }
	B(char c) {
    
     cout << "B(" << c << ")" << endl; }
};
class E
{
    
    
public:
	E() {
    
     cout << "E()" << endl; }

};
class D :public B {
    
    
public:
	using B::B;
	D() = default;
	//D(int i):B(i){}
	//D(char c){}
	D(double x) :e1{
    
    }, e2{
    
    },B(static_cast<int>(x)){
    
    cout << "D" << x << ")" << endl; }
private:
	E e1, e2;
};
int main()
{
    
    
	B b;
	D d;
	D d2{
    
     3.03 };
	std::cin.get();

}

在这里插入图片描述
结果如上图所示。

二、控制访问属性

#include<iostream>
using std::cout;
using std::endl;
class A
{
    
    
public:
	int i{
    
     0 };
protected:
	int j{
    
     0 };
private:
	int k{
    
     0 };
};

class Pub :public A
{
    
    
public:
	void foo() {
    
     i++; j++; }

};

class Pro :protected A
{
    
    
public:
	void foo() {
    
     i++; j++; }
};

class Pri :private A
{
    
    
public:
	void foo()
	{
    
    
		i++; j++;
	}
};

int main()
{
    
    
	Pub pub;
	Pro pro;
	Pri pri;

	pub.i++; //唯一有效
//	pub.i++; pub.j++; pub.k++;  可取消注释 查看报错
//	pro.i++; pro.j++; pro.k++;
//	pri.i++; pri.j++; pri.k++;

	std::cin.get();

}

protected属性,我的理解是不允许外部访问,但允许子类访问(开小灶)。

猜你喜欢

转载自blog.csdn.net/taiyuezyh/article/details/124271573