C++ 继承与多态(一) 访问修饰符public private protected

C++ 继承与多态(一) 访问修饰符public private protected


一.修饰访问符修饰成员变量

1.public修饰成员变量

由于成员变量在类中是由public修饰的,所以既可以通过直接访问,也可以通过函数间接访问。

#include <iostream>

using namespace std;

class Base {
public:
	Base() {}
	~Base() {}
	int getWidth();
	void setWidth(int width);
	int _width;
};
void Base::setWidth(int width) {
	this->_width = width;
}
int Base::getWidth() {
	return _width;
}


int main() {
	Base base;
	//由于_width在类中时public修饰的,可以直接随心所以的访问
	base._width = 10;
	cout << base._width << endl;
	return 0;
}


在这里插入图片描述

2.protected 修饰成员变量

若是由protected修饰成员变量,那么只能通过函数进行间接访问,不能再通过对象直接访问。

#include <iostream>

using namespace std;

class Base {
public:
	Base() {}
	~Base() {}
	int getWidth();
	void setWidth(int width);
	//将_width改为protected修饰
protected:
	int _width;
};
void Base::setWidth(int width) {
	this->_width = width;
}
int Base::getWidth() {
	return _width;
}


int main() {
	Base base;
//	base._width = 10;//Error
	//由于是protected修饰的成员变量,所以只能通过函数进行间接访问
	base.setWidth(10);
	cout << base.getWidth() << endl;
	return 0;
}



3.private修饰成员变量

同理,private修饰的成员变量也不能够直接被访问,只能通过调用函数进行访问

#include <iostream>

using namespace std;

class Base {
public:
	Base() {}
	Base(int width):_width(width){}
	~Base() {}
	int getWidth();
	void setWidth(int width);
	//将_width改为private修饰
private:
	int _width;
};

void Base::setWidth(int width) {
	this->_width = width;
}
int Base::getWidth() {
	return _width;
}


int main() {
	Base base(1);
	//	base._width = 10;//Error
	//由于是private修饰的成员变量,所以只能通过函数进行间接访问
	base.setWidth(10);
	cout << base.getWidth() << endl;
	return 0;
}

二.修饰访问符继承

1.public

本类中public修饰的成员,什么时候能否访问?

在本类中能否被访问?在派生类中能否被访问?在实例化对象中能否被直接访问?

派生类继承之后,基类中成员修饰符如何变化?

基类中成员修饰符public继承后派生类中修饰符
publicpublic
protectedprotected
private基类中private修饰的成员派生类均不能访问
#include <iostream>
#include "help.h"
using namespace std;

class A {
public:
	A() {};
	~A() {};
	int a;

	void set() {
		a = 0;
		a1 = 1;
		a2 = 2;
		a3 = 3;
	}

	void show() {
		cout << "a = " << a << endl;
		cout << "a1 = " << a1 << endl;
		cout << "a2 = " << a2 << endl;
		cout << "a3 = " << a3 << endl;
	}

public:
	int a1;
protected:
	int a2;
private:
	int a3;
};

class B :public A {
public:
	B() {};
	~B() {};
	void show() {
		cout << "a = " << a << endl;
		cout << "a1 = " << a1 << endl;
		cout << "a2 = " << a2 << endl;
		cout << "a3 = " << a3 << endl;//ERROR! private修饰的成员只能在本类中访问
	}
	
};


int main() {
	B b;
	cout << b.a << endl;
	cout << b.a1 << endl;
	cout << b.a2 << endl;//ERROR! protected修饰的成员只能在本类中或派生类中进行访问
	cout << b.a3 << endl;//ERROR! private修饰的成员只能再本类中访问

	return 0;
}

2.protected继承

protected修饰的成员,什么时候能否访问?

在本类中能否被访问?在派生类中能否被访问?在实例化对象中能否被直接访问?
不能

派生类继承之后,基类中成员修饰符如何变化?

基类中成员修饰符protected继承后派生类中修饰符
publicprotected
protectedprotected
private基类中private修饰的成员派生类均不能访问
#include <iostream>
#include "help.h"
using namespace std;

class A {
public:
	A() {};
	~A() {};
	int a;

	void set() {
		a = 0;
		a1 = 1;
		a2 = 2;
		a3 = 3;
	}

	void show() {
		cout << "a = " << a << endl;
		cout << "a1 = " << a1 << endl;
		cout << "a2 = " << a2 << endl;
		cout << "a3 = " << a3 << endl;
	}

public:
	int a1;
protected:
	int a2;
private:
	int a3;
};

class B: protected A {
public:
	B() {};
	~B() {};
	void show() {
		cout << "a = " << a << endl;
		cout << "a1 = " << a1 << endl;
		cout << "a2 = " << a2 << endl;
		cout << "a3 = " << a3 << endl;//ERROR! private修饰的成员只能在本类中访问
	}
	
};


int main() {
	B b;
	cout << b.a << endl;//ERROR! protected修饰的成员只能在本类或派生类中访问
	cout << b.a1 << endl;//ERROR! protected修饰的成员只能在本类或派生类中访问
	cout << b.a2 << endl;//ERROR! protected修饰的成员只能在本类中或派生类中进行访问
	cout << b.a3 << endl;//ERROR! private修饰的成员只能再本类中访问

	return 0;
}

3.private继承

private修饰的成员,什么时候能否访问?

在本类中能否被访问?在派生类中能否被访问?在实例化对象中能否被直接访问?
不能不能

派生类继承之后,基类中成员修饰符如何变化?

基类中成员修饰符private继承后派生类中修饰符
publicprivate
protectedprivate
private基类中private修饰的成员派生类均不能访问
#include <iostream>
#include "help.h"
using namespace std;

class A {
public:
	A() {};
	~A() {};
	int a;

	void set() {
		a = 0;
		a1 = 1;
		a2 = 2;
		a3 = 3;
	}

	void show() {
		cout << "a = " << a << endl;
		cout << "a1 = " << a1 << endl;
		cout << "a2 = " << a2 << endl;
		cout << "a3 = " << a3 << endl;
	}

public:
	int a1;
protected:
	int a2;
private:
	int a3;
};


//注意,像这样没有直接标明继承类型的都是private继承
class B:A {
public:
	B() {};
	~B() {};
	void show() {
		cout << "a = " << a << endl;
		cout << "a1 = " << a1 << endl;
		cout << "a2 = " << a2 << endl;
		cout << "a3 = " << a3 << endl;//ERROR! private修饰的成员只能在本类中访问
	}
	
};

class C :B {
public:
	C() {};
	~C() {};
	void show() {
		//ERROR! 由于B类从A类是private继承,所以 a, a1, a2 都已经全部变为private修饰了,所以在C类中不能进行访问
		cout << "a = " << a << endl;
		cout << "a1 = " << a1 << endl;
		cout << "a2 = " << a2 << endl;
		cout << "a3 = " << a3 << endl;
	}

};



int main() {
	B b;
	cout << b.a << endl;//ERROR! private修饰的成员只能再本类中访问
	cout << b.a1 << endl;//ERROR! private修饰的成员只能再本类中访问
	cout << b.a2 << endl;//ERROR! private修饰的成员只能再本类中访问
	cout << b.a3 << endl;//ERROR! private修饰的成员只能再本类中访问

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值