C++继承学习记录

一、继承的特征:

1.0 将特性从一个类,传递到另一个类;
2.0 基类引用可以指向派生类对象;
3.0 继承基类的构造函数---

提要:
// 使用基类的构造函数,来初始化基类的数据成员
// 谁的数据成员用谁来初始化:
// color(aColor), filled(aFilled) 无法使用:因为color和filled是Shape的私有成员,出了Shape,没人可以用。

// 基类,最好要有默认构造函数,一维派生类很可能会调用。
// 派生类的构造函数一定会调用基类的构造函数;派生类初始化自己的部分,然后基类的构造调用基类构造函数
// 一旦显示的声明了构造函数,那么编译器不会再自动帮我们生成默认构造函数;如果没有了默认的构造函数,派生类如果需要用到,那么就会出错:
Shape() = default; // 显式的让编译器生成默认的构造函数。

// 基类和派生类 同名性隐藏的问题: 如果派生类的成员函数和基类中的成员函数同名,那么离得‘远’的会被隐藏 ;类似局部变量隐藏全局变量(用作用域解析符::访问);那么对于基类和派生类如何解决呢?
解决方法类似:把基类中的同名函数,显式的在派生类中声明:using Shape::f();
// 正是由于同名隐藏,所以才会有重定义函数:
1.0 重定义函数(redefine函数)和重载函数的区别:

在这里插入图片描述
2.0 重定义函数的使用:
在这里插入图片描述

二、继承的优缺点:

继承的优点:

1.提高了代码的复用性

2.提高了维护性

3.让类与类之间产生关系

4.多态的前提就是继承

继承的缺点:

1.增强了类之间的耦合
    
2.软件开发的原则是高内聚,低耦合

三、继承的规则:

3.1 子类并不会继承父类的 	:友元函数 和 析构函数;
3.2 继承构造函数,会全部继承,不能挑挑拣拣;
3.3 继承构造函数的方法:using A::A ;
3.4 见下图:

在这里插入图片描述
3.5 解析图片:派生类B继承A的构造函数之后,使用B b(1), 会调用B类的构造函数B(int i),但是B(int i)未定义,或者说没有显式定义;编译期会在 派生类B继承基类A的构造函数时,自动变成B(int i); 然后执行下面的初始化:(在构造函数中调用了基类的构造函数)

B(int i)
	:A{i}
{

}

由下图可知,派生类B在继承父类A的构造函数时,如果派生类B有相同的构造函数,那么不会再继承父类A的构造函数。
在这里插入图片描述

main() 函数文件:

#include <iostream>
#include "Shape.h"
#include "Circle.h"

int main()
{
	using std::cout;
	using std::endl;

	Shape obj1(color::blue, false);
	// 这里还没有使用继承:只能这么定义
	Circle obj2(1.0f);

	cout << obj1.toString() << endl;
	cout << obj2.toString() << endl;

	// 使用继承之后:
	cout << "使用继承之后:" << endl;
	Circle obj3(color::black, true, 5.0);
	cout << obj3.toString() << endl;


	return 0;
}

Shape 类:(基类)

#pragma once
#include <string>
#include <array>

enum class color{white,black,red,blue,orange,yellow,green,pink};

class Shape
{
private:
	color Color{color::red};
	bool filled{false};

public:
	Shape() = default;
	Shape(color aColor, bool aFilled);

public:
	bool isFilled();
	void setColor(color aColor);
	void setFilled(bool aFilled);

	color getColor()const;

	// 不知道干嘛的:
	std::string toString();
};
#include "Shape.h"

Shape::Shape(color aColor, bool aFilled)
	:Color{aColor}, filled{aFilled}
{
}

bool Shape::isFilled()
{
	return filled;
}

void Shape::setColor(color aColor)
{
	this->Color = aColor;
}

void Shape::setFilled(bool aFilled)
{
	this->filled = true;
}

color Shape::getColor() const
{
	return this->Color;
}

std::string Shape::toString()
{
	std::array<std::string, 8>colorArray = { "white","black","red","blue","orange","yellow","green","pink" };
	std::string display =  "Shape: " + colorArray[static_cast<int>(Color)] + " is " + (filled ? "filed" : "not filled");
	return display;
}

Circle类:(派生类)

#pragma once
#include "Shape.h"

class Circle : public Shape
{
private:
	double radius{1.0f};

public:
	Circle() = default;
	Circle(color aColor,bool aFilled, double aRadius);
	Circle(double aRadius);

public:
	void setRadius(double aRadius);
	double getRadius()const;
	double getArea()const;

};


#include "Circle.h"

// 使用基类的构造函数,来初始化基类的数据成员
// 谁的数据成员用谁来初始化:
Circle::Circle(color aColor, bool aFilled, double aRadius)
	:Shape{aColor, aRadius}, radius{aRadius}
	// color(aColor), filled(aFilled) 无法使用:因为color和filled是Shape的私有成员,出了Shape,没人可以用。
{

}


Circle::Circle(double aRadius)
	:radius(aRadius)
{
	
}

void Circle::setRadius(double aRadius)
{
	radius = aRadius;
}

double Circle::getRadius() const
{
	return this->radius;
}

double Circle::getArea() const
{
	return radius * radius*3.14;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值