C++_访问(权限)控制和继承
前言:
pritive:外界不可见,不能直接访问
protected:外界不可见,不能直接访问;子类可以访问
public:外界可以直接访问
1、学生身份继承一个人的类之后他就有一些基本属性可以做一些事情了
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
static int cnt;
char *name;
int age;
public:
static int getCount(void);
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
cnt++;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
cnt++;
}
Person(char *name, int age)
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
cnt++;
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
cnt++;
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
}
void setName(char *name)
{
if (this->name) {
delete this->name;
}
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
cout<<"name = "<<name<<", age = "<<age<<endl;
}
};
int Person::cnt = 0; /* 定义和初始化 */
int Person::getCount(void)
{
return cnt;
}
class Student : public Person { //声明一个学生继承人这个类
};
int main(int argc, char **argv)
{
Student s;
s.setName("zhangsan");
s.setAge(16);
s.printInfo();
return 0;
}
2、举例父亲和儿子的关系
①派生类不能直接访问父类的私有成员变量
②作为儿子继承了父亲可以访问父亲的保护成员(但必须在儿子类内部才行,其他地方不行比如在main函数中)
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
protected:
int room_key;
public:
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son : public Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son paly game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
/* 外人不能拿父亲的房间钥匙
* 儿子可以
*/
room_key = 1;
}
};
int main(int argc, char **argv)
{
Son s;
s.setMoney(10);
cout << s.getMoney()<<endl;
s.it_skill();
s.play_game();
//s.room_key = 1;
return 0;
}
3、通过using这个关键字可以调整继承变量或函数的权限
①可以调整的前提条件是儿子有权限访问到的变量
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
protected:
int room_key;
public:
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son : public Father {
private:
int toy;
//using Father::it_skill; //把继承父亲的技能私藏起来
public:
using Father::room_key;
//using Father::money; //不能操作继承来的私有成员变量
void play_game(void)
{
int m;
cout<<"son paly game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
int main(int argc, char **argv)
{
Son s;
s.setMoney(10);
cout << s.getMoney()<<endl;
s.it_skill();
s.play_game();
s.room_key = 1;
return 0;
}
4、派生类继承基类的属性后,他们的访问属性就会变成这样的
①无论哪种继承方式,在派生类内部使用父类时并没有差别
②不同的继承方式,会影响两个方便,外部代码对派生类的使用和派生类的子类使用
注意:
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
protected:
int room_key;
public:
int address;
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son_pub : public Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
class Son_pro : protected Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
class Son_pri : private Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
int main(int argc, char **argv)
{
Son_pub s_pub;
Son_pro s_pro;
Son_pri s_pri;
s_pub.play_game();
s_pro.play_game();
s_pri.play_game();
s_pub.it_skill();
//s_pro.it_skill(); // error
//s_pri.it_skill(); // error
return 0;
}
5、爷爷、爸爸和孙子三代继承继承关系后,各种变量的访问权限
注意:着重注意继承关系中各个成员变量的访问权限
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
protected:
int room_key;
public:
int address;
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son_pub : public Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
class Son_pro : protected Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
class Son_pri : private Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
class Grandson_pub : public Son_pub {
public:
void test(void) {
room_key = 1; /* address is protected */
address = 2; /* address is public */
}
};
class Grandson_pro : public Son_pro {
public:
void test(void) {
room_key = 1; /* address is protected */
address = 2; /* address is protected */
}
};
class Grandson_pri : public Son_pri {
public:
void test(void) {
//room_key = 1; // error
//address = 2; // error
}
};
int main(int argc, char **argv)
{
Son_pub s_pub;
Son_pro s_pro;
Son_pri s_pri;
s_pub.play_game();
s_pro.play_game();
s_pri.play_game();
s_pub.it_skill();
//s_pro.it_skill(); // error
//s_pri.it_skill(); // error
Grandson_pub gs_pub;
Grandson_pro gs_pro;
gs_pub.address = 2;
//gs_pro.address = 2; // error
return 0;
}
6、覆写操作,扩展技能时候实现
注意:这个例子是儿子扩展实现父亲的it_skill技能
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
protected:
int room_key;
public:
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son : public Father {
private:
int toy;
//using Father::it_skill;
public:
using Father::room_key;
//using Father::money;
void play_game(void)
{
int m;
cout<<"son paly game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
/* 覆写 override */
void it_skill(void)
{
cout<<"son's it skill"<<endl;
}
};
int main(int argc, char **argv)
{
Son s;
s.setMoney(10);
cout << s.getMoney()<<endl;
s.it_skill();
s.play_game();
s.room_key = 1;
return 0;
}
7、派生类的空间分布
注意:
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
public:
int address;
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
Person(char *name, int age)
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
}
void setName(char *name)
{
if (this->name) {
delete this->name;
}
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
cout<<"name = "<<name<<", age = "<<age<<endl;
}
};
class Student : public Person {
private:
int grade;
void setGrade(int grade) {this->grade = grade;}
int getGrade(void) {return grade;}
public:
void printInfo(void)
{
cout<<"Student ";
Person::printInfo();
}
};
void test_func(Person &p)
{
p.printInfo();
}
int main(int argc, char **argv)
{
Person p("lisi", 16);
Student s;
s.setName("zhangsan");
s.setAge(16);
test_func(p);
test_func(s); /* Person &p = s里面的Person部分;
* p引用的是"s里面的Person部分"
*/
s.printInfo();
return 0;
}