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继承后派生类中修饰符 |
---|---|
public | public |
protected | protected |
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继承后派生类中修饰符 |
---|---|
public | protected |
protected | protected |
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继承后派生类中修饰符 |
---|---|
public | private |
protected | private |
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;
}