半年代码搬运工Jine的入学博客:子类构造函数对基类构造函数的声明方式

第一次在CSDN上写博客,小媛Jine代码之旅开始啦(๑╹◡╹)ノ"""
ps:为什么标题不能发颜文字>_<


一开始是这个例子:

#include<iostream>
using namespace std;

class Point
{
private: 
	int x,y;
public:
	Point(int xx,int yy){
		x=xx;
		y=yy;
		cout<<"Point Object Constructed."<<endl;
	}
	virtual ~Point(){
		cout<<"Point Object Destructed."<<endl;
	}
	void Show(){
		cout<<"x="<<x<<","<<"y="<<y<<endl;
		
	}
};
//StudybarCommentBegin
class Circle: public Point{
	private:
		double radius;
	public:
		Circle(double x=0,double y=0,double radius=0):Point(x,y){
			this->radius=radius;
			cout<<"Circle Object Constructed."<<endl;
		}
		~Circle(){
			cout<<"Circle Object Destructed."<<endl;
		}
		void Show(){Point::Show();cout<<"radius="<<radius<<endl;}
};  

Point *shape(){
	Circle shape1(1,2,3);
	shape1.Show();
	
	Point *shape2=new Circle(4,5,6);
	shape2->Show();
	return shape2;
}

int main()
{
	Point *p=shape();
	delete p;
	return 0;
}
//StudybarCommentEnd

我们知道,构造方法是用来初始化类对象的。如果在类中没有显式地声明构造函数,那么编译器会自动创建一个默认的构造函数;并且这个默认的构造函数仅仅在没有显式地声明构造函数的情况下才会被创建创建。

构造函数与父类的其它成员(成员变量和成员方法)不同,它不能被子类继承。因此,在创建子类对象时,为了初始化从父类中继承来的成员变量,编译器需要调用其父类的构造函数。如果子类的构造函数没有显示地调用父类的构造函数,则默认调用父类的无参构造函数,至于什么事显式调用,在下面会详细说明!关于子类中构造函数的构造原则,总结如下,欢迎大家指导与批评。

1.父类没有声明构造函数

(1)子类也没有声明自己的构造函数,则父类和子类均由编译器生成默认的构造函数。
  (2)子类中声明了构造函数(无参或者带参),则子类的构造函数可以写成任何形式,不用顾忌父类的构造函数。在创建子类对象时,先调用父类默认的构造函数(编译器自动生成),再调用子类的构造函数。

2.父类只声明了无参构造函数

如果子类的构造函数没有显式地调用父类的构造,则将会调用父类的无参构造函数。也就是说,父类的无参构造函数将会被隐式地调用。

3.父类只声明了带参构造函数

在这种情况下,要特别注意。因为父类只有带参的构造函数,所以如果子类中的构造函数没有显示地调用父类的带参构造函数,则会报错,所以必需显示地调用。关于构造函数的显示调用,参见下例。

class animal
{
protected:       //成员变量,声明为protected或者public,这里选择protected
	int height;  //若声明为private,则不能被子类继承访问,会报错
	int weight;
public:
	animal(int height,int weight)   //带参的构造函数
	{
		this->height=height;
		this->weight=weight;
		cout<<"animal的带参构造函数被调用"<<endl;
	}
	virtual ~animal()
	{
		cout<<"animal的析构函数被调用"<<endl;
	}
};
//子类
class fish:public animal
{
public:
	fish():animal(height,weight) //显示调用父类的构造函数
	{
		cout<<"fish的构造函数被调用"<<endl;
	}
	virtual ~fish()
	{
		cout<<"fish的析构函数被调用"<<endl;
	}
};

在子类fish的构造函数中,加上一个冒号(😃,然后加上父类的带参构造函数,这就是父类构造函数的显式调用。这样,在子类的构造函数被调用时,系统就会去调用父类的带参构造函数,从而实现初始化父类的成员变量。运行结果如下:

4.父类同时声明了无参和带参构造函数

在这种情况下,子类只需要实现父类的一个构造函数即可,不管是无参的还是带参的构造函数。如果子类的构造函数没有显示地调用父类的构造函数(无参或带参),则默认调用父类的无参构造函数。

//父类
class animal
{
protected:       //成员变量,声明为protected或者public,这里选择protected
	int height;  //若声明为private,则不能被子类继承访问,会报错
	int weight;
public:	
	animal()
	{
		height=0;
		weight=0;
		cout<<"animal的无参构造函数被调用"<<endl;
	}
	animal(int height,int weight)   //带参的构造函数
	{
		this->height=height;
		this->weight=weight;
		cout<<"animal的带参构造函数被调用"<<endl;
	}
	virtual ~animal()
	{
		cout<<"animal的析构函数被调用"<<endl;
	}
};
//子类
class fish:public animal
{
public:
	fish()     //没有显示地调用父类的构造函数(无参或带参),则默认调用父类的无参构造函数
	{
		cout<<"fish的构造函数被调用"<<endl;
	}
	virtual ~fish()
	{
		cout<<"fish的析构函数被调用"<<endl;
	}
};
int main()
{
	fish f;
	return 0;
}

运行结果如下:
在这里插入图片描述

总结以上几条,可以归纳出C++中子类继承父类时构造函数的写法的规律:当父类有显式地声明了构造函数时,子类最低限度的实现父类中的一个;当父类没有声明构造函数时,子类可以不声明构造函数或者任意地书写构造函数。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值