C++——继承 – 作者:Johnson666

1.定义一个父类,写个子类继承并输出对象的面积

代码:

#include<iostream>
using namespace std;
class Shape
{
public:
	void setWidth(int w)
	{
		width = w;
	}
	void setHeight(int h)
	{
		height = h;
	}
protected:
	int width;
	int height;
};

class Rectangle: public Shape
{
public:
	int getArea()
	{
		return (width * height);
	}
};

int main()
{
	Rectangle Rect;
	Rect.setWidth(5);
	Rect.setHeight(7);
	cout << "Total area:" << Rect.getArea() << endl;
	return 0;
}

结果:
image.png

2.访问C++类对象中私有成员变量的方法

类的对象不能直接访问类声明的私有成员变量,否则破坏了信息隐藏的目的。
在C++中,为了防止某些数据成员或成员函数从外部被直接访问,可以将它们声明为private,这样编译器会阻止任何来自外部非友元的直接访问。

(1)通过公共函数为私有成员赋值

代码:

#include<iostream>
using namespace std;
class A
{
public:
	int i;
	int getJ(int j)
	{
		return j;
	}
private:
	int j;
};

int main()
{
	A a;
	cout << "j=" <<a.getJ(6) << endl;
	return 0;
}

结果:
image.png

代码:

#include<iostream>
using namespace std;

class Test
{
private:
	int x,y;
    
public:
	void setX(int a)
	{
		x = a;
	}
	void setY(int b)
	{
		y = b;
	}
	void print()
	{
		cout << "x=" << x << endl;
		cout << "y=" << y << endl;
	}
};

int main()
{
	Test a;
	a.setX(1);
	a.setY(9);
	a.print();
	return 0;
}

结果:
image.png

(2)利用函数访问私有数据成员

代码:

#include<iostream>
using namespace std;

class Test
{
	int x,y;

public:
	int X(int a)
	{
		x = a;
		return x;
	}
	int Y(int b)
	{
		y = b;
		return y;
	}
};

int main()
{
	Test Jo;
	int a,b;
	a = Jo.X(4);
	b = Jo.Y(6);
	cout << "x=" << a << endl;
	cout << "y=" << b << endl;
	return 0;
}

结果:
image.png

(3)利用指针访问私有数据成员

代码:

#include<iostream>
using namespace std;

class Test
{
	int x,y;

public:
	void setX(int a)
	{
		x = a;
	}
	void setY(int b)
	{
		y = b;
	}
	void getXY(int *px,int *py)
	{
		*px = x;
		*py = y;
	}
};

int main()
{
	Test Jo;
	Jo.setX(4);
	Jo.setY(6);
	int a,b;
	Jo.getXY(&a,&b);
	cout << "x=" << a << endl;
	cout << "y=" << b << endl;
	return 0;
}

结果:
image.png

(4)利用引用访问私有数据成员

代码:

#include<iostream>
using namespace std;

class Test
{
	int x,y;
public:
	void setX(int a)
	{
		x = a;
	}
	void setY(int b)
	{
		y = b;
	}
	void getXY(int &px,int &py)
	{
		px = x;
		py = y;
	}
};

int main()
{
	Test Jo;
	Jo.setX(6);
	Jo.setY(4);
	int a,b;
	Jo.getXY(a,b);
	cout << "x=" << a << endl;
	cout << "y=" << b << endl;
	return 0;
}

结果:
image.png

3.子类继承父类的三种继承方式

在C++中,子类继承父类有三种继承方式,分别是:public, protected和private。子类继承的方式不同,那么子类对象访问父类中的参数访问权限也不同。
1.public 方式继承:
基类的private成员派生类不可直接访问,基类中的protected和public成员在派生类中可以直接使用,并且变成了派生类中相应的protected成员和public成员,但是只有public成员可以在派生类外使用。

2.protected 方式继承:
基类的private成员派生类不可直接访问,基类的protected成员和public成员变成派生类的protected成员,这时在派生类外不能使用基类的所有成员,包括public成员。

3.private 方式继承:
基类的private成员仍然为私有的,派生类不可直接访问,基类中的protected成员和public成员成为了派生类中的private成员,在派生类外不能使用基类中的任何成员。

那么虽然基类的private成员不可直接访问,但是我们可以间接访问它,比如用函数来。下面我就举个例子:
代码:

#include<iostream>
using namespace std;

class Test
{
	int x,y;

public:
	void setX(int a)
	{
		x = a;
	}
	void setY(int b)
	{
		y = b;
	}
	void getXY(int &px,int &py)
	{
		px = x;
		py = y;
	}
};

class Test2: public Test
{
public:
	int addXY(int a,int b)
	{
		int c;
		c = a + b;
		return c;
	}
};

int main()
{
	Test Jo;
	Test2 John;
	Jo.setX(6);
	Jo.setY(4);
	int a,b;
	John.getXY(a,b); //用父类的函数来间接访问
	cout << "x=" << a << endl;
	cout << "y=" << b << endl;
	cout << "sum=" << John.addXY(a,b) << endl; 
	return 0;
}

结果:
image.png

来源:freebuf.com 2021-05-19 17:14:09 by: Johnson666

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发

请登录后发表评论