C++继承

基类&&派生类
       面向对象程序设计中最重要的一个概念是继承。继承允许我们依据另一个类来定义一个类,这使得创建和维护一个应用程序变得更容易。这样做,也达到了重用代码功能和提高执行时间的效果。现实世界中的继承(例如儿子继承父亲财产)类似。
当创建一个类时,您不需要重新编写新的数据成员和成员函数,只需指定新建的类继承了一个已有的类的成员即可。这个已有的类称为基类,新建的类称为派生类。例如:猫科动物有:狮子、老虎、豹子;他们都继承了猫科动物的特征。
   
#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(void)
{
	Rectangle Rect;

	Rect.setWidth(5);
	Rect.setHeight(7);

	// 输出对象的面积
	cout << "Total area: " << Rect.getArea() << endl;
	getchar();
	return 0;
}
     执行结果为35;
     
     派生类对于基类的访问也是有权限限制的,如果没有确定继承基类的某一块成员,默认是继承private(私有的),也就是这时候派生类访问基类所有数据都默认是私有的,不可被外包括对象,只能派生类内访问。下面有一张表更形象去理解这一些:
       基于访问权限,可以多次尝试理解。
#include<iostream>
using namespace std;
class Fu
{
public:
	/*Fu(){
		X = 0;
		Y = 0;
	}*/
	void initP(int x = 0, int y = 0){
		X = x; Y = y;
	}
	void Move(int xx,int yy){
		X += xx; Y += yy;
	}
	int getX(){ return X; }
	int getY(){ return Y; }
private:
	int X ;
	int Y ;
};

class Zi :public Fu
{
public:
	void la(int x, int y,int w ,int h  ){
	
		initP(x, y);
		W = w; H = h;
	};
	/*int getX(){ return Fu:: getX() }
	int getY(){ return Fu:: getY() }*/
	int getW(){ return W; }
	int getH(){ return H; }
private:
	int W;
	int H;
};

int main(){
	Zi jisuan1;
	jisuan1.la(10,20,300,400);
	jisuan1.Move(50,60);
	cout << jisuan1.getX() << "\n" << jisuan1.getY() << "\n" << jisuan1.getW() << "\n" << jisuan1.getH() << endl;
	getchar();
	return 0;
}
运行结果:60,80,300,400
     细节的注重对于这个为数不多的知识点的学习是重点,比如这里上面基类的析构函数没有对X,Y赋初值,就会导致出错,不然就是派生类中调用出来中的initP中的函数让XY初始化,否则结果无法达到预期结果。
     更形象的实例如下:
#include<iostream>
using namespace std;
class A
{
public:
	void showA(){
		cout << "我是A" << endl;
	}
};

class B
{
public:
	void showB(){
		cout << "我是B" << endl;
	}
};

class C :public A,private B
{
public:
	void showC(){
		showB();//这里声明才能调用基类中的B私有化的
		cout << "我是C"<<endl;
	}
};

int main(){
	C c;
	c.showA();
	//c.showB();	//这里用对象访问基类中的是不允许的,因为C继承了私有的B,那么B的所有成员都会私有化
	c.showC();
	system("pause");
	return 0;
}
这里的注释便能非常直白的看出访问权限,以及继承多个基类时的不同结果。
#include<iostream>
using namespace std;

class B1
{
public:
	B1(int i){ cout << "第一个类" <<i<< endl; };
};

class B2
{
public:
	B2(int j){ cout << "第二个类"<<j<<endl; }
};

class B3
{
public:
	B3(){ cout << "********" << endl; }
};

class C :public B1, public B2, public B3			//从左到右的继承 逐一调用构造函数 ,与上面写的父类顺序无关~~
{
public:
	C(int a, int b, int c, int d) :B1(a), memberB1(c), memberB2(d), B2(b){}
private:
	B1 memberB1;
	B2 memberB2;
	B3 memberB3;
};

int main(){
	C xx(7,8,9,10);
	system("pause");
	return 0;
}


重载函数:
在同一个作用域内,可以声明几个功能类似的同名函数,但是这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同。您不能仅通过返回类型的不同来重载函数。
实例:
#include<iostream>
using namespace std;

class A
{
public:
	int add(int a,int b);
	double add(double a,double b);
private:
};

int A::add(int a,int b){
	return a + b;
}
double A::add(double a, double b){
	return a + b;
}

void main(){
	A a;
	a.add(103.7,65.6);			//函数重载的实例,参数类型可以决定进入哪个函数进行运算~~
	cout << a.add(103.7, 65.6) << endl;
	system("pause");
}
可以通过打断点可以看出进入了哪个函数中计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值