十一、C++ 访问控制和继承

1.封装

a. 抽象出数据成员、成员函数

b. 访问控制

2.继承

private:外界不可见,不能直接访问

protected:外接不可见,不能直接访问;子类可以

public:外接可以直接访问

#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;
}


执行结果:
name = zhangsan, age = 16
~Person
name = zhangsan

 public继承:

1.不能直接拿父亲的私房钱:派生类不能访问基类的私有成员

2.可以问父亲要钱:通过protected/public 的成员函数

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Father {
private:
	int money;
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);
	}
};


int main(int argc, char **argv)
{
	Son s;

	s.setMoney(10);
	cout << s.getMoney()<<endl;

	s.it_skill();
	s.play_game();
	
	return 0;
}

2.儿子比外人要亲:派生类可以访问 protected成员,其他代码不可以

可以访问保护成员,不能在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;    //不能在main函数里面访问
	
	return 0;
}

 3.调整访问控制

儿子继承来的财产,他可以捐给别人,也可以私藏

Son 可见到的成员,Son可以修改它的权限,

#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;  //可以,把它设为私有成员
    //using Father::room_key;  //可以,把它设为私有成员
public:
	using Father::room_key;   //可以,把它设为公有成员  
	//using Father::money;    //err
	
	void play_game(void)
	{
		int m;
		
		cout<<"son paly game"<<endl;


		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.三种继承方式的差别

a.无论哪一种继承方式,在派生类内部使用父类时并无差别

b.不同的继承方式,会影响这两方面:外部代码对派生类的使用、派生类的子类怎么使用派生类的成员

 

 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;

		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;


		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;


		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;
}

6.派生类的子类

#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; /* room_key is protected */ 
		address  = 2; /* address is public */ 
	}
};

class Grandson_pro : public Son_pro {

public:
	void test(void) {
		room_key = 1; /* room_key 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;
}

7.覆写

#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();       //用的儿子的skill
	s.play_game();

	s.room_key = 1;
	
	return 0;
}

#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;
}


执行结果:
Person(char*, int), name = lisi, age = 16
name = lisi, age = 16
name = zhangsan, age = 16    //调用的是person里面的函数
Student name = zhangsan, age = 16 
~Person()
name = zhangsan
~Person()
name = lisi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值