网站开发的大致流程,网站开发的学校,纵横天下网站开发,wordpress创建自定义页面模板目录
特点
基本用法
实例
基类#xff1a;Animal
派生类#xff1a;Lion
派生类#xff1a;Elephant
派生类#xff1a;Bird
使用这些类
权限对继承的影响
示例
基类构造函数
示例 继承是面向对象编程#xff08;OOP#xff09;中的一个核心概念#xff0c;…目录
特点
基本用法
实例
基类Animal
派生类Lion
派生类Elephant
派生类Bird
使用这些类
权限对继承的影响
示例
基类构造函数
示例 继承是面向对象编程OOP中的一个核心概念特别是在C中。它允许一个类称为派生类或子 类继承另一个类称为基类或父类的属性和方法。继承的主要目的是实现代码重用以及建立一种 类型之间的层次关系。
特点
代码重用子类继承了父类的属性和方法减少了代码的重复编写。扩展性子类可以扩展父类的功能添加新的属性和方法或者重写覆盖现有的方法。多态性通过继承和虚函数C支持多态允许在运行时决定调用哪个函数。
基本用法
继承可以是公有public、保护protected或私有private的这决定了基类成员在 派生类中的访问权限
#include iostream
using namespace std;
//基类父类
class Vehicle{ //交通工具车,抽象的概念
public:string type;string contry;string color;double price;int numOfWheel;void run(){cout 车跑起来了 endl;}void stop();
};
//派生类子类
class Bickle : public Vehicle{};
//派生类子类
class Roadster : public Vehicle{ //跑车也是抽象比父类感觉上范围缩小了点
public:int stateOfTop;void openTopped();void pdrifting();
};
int main()
{Roadster ftype;ftype.type 捷豹Ftype;ftype.run();Bickle bike;bike.type 死飞;bike.run();return 0;
}
Vehicle 类公有地继承自 Vehicle 类这意味着所有 Vehicle 类的公有成员在 Vehicle 类中也是公有的
实例
我们有一个基类 Animal 它定义了所有动物共有的特性和行为。然 后我们可以创建几个派生类如 Lion 、 Elephant 和 Bird 这些类继承自 Animal 类并添加或 修改特定于它们自己的特性和行为
基类Animal
#include iostream
#include string
class Animal {
protected:std::string name;int age;
public:Animal(std::string n, int a) : name(n), age(a) {}virtual void makeSound() {std::cout name makes a sound. std::endl;}virtual void display() {std::cout Animal: name , Age: age std::endl;}
};派生类Lion
class Lion : public Animal {
public:Lion(std::string n, int a) : Animal(n, a) {}void makeSound() override {std::cout name roars. std::endl;}void display() override {std::cout Lion: name , Age: age std::endl;}
};派生类Elephant
class Elephant : public Animal {
public:Elephant(std::string n, int a) : Animal(n, a) {}void makeSound() override {std::cout name trumpets. std::endl;}void display() override {std::cout Elephant: name , Age: age std::endl;}
};派生类Bird
class Bird : public Animal {
public:Bird(std::string n, int a) : Animal(n, a) {}void makeSound() override {std::cout name sings. std::endl;}void display() override {std::cout Bird: name , Age: age std::endl;}
};
使用这些类
int main() {Lion lion(Leo, 5);Elephant elephant(Ella, 10);Bird bird(Bella, 2);lion.display();lion.makeSound();elephant.display();elephant.makeSound();bird.display();bird.makeSound();return 0;
}Animal 是基类定义了所有动物共有的属性如 name 和 age 和方法如 makeSound 和 display 。Lion 、 Elephant 和 Bird 是派生类它们继承了 Animal 的特性并根据自身的特性重写了 makeSound 和 display 方法。在 main 函数中创建了各种动物的实例并展示了它们的行为。
权限对继承的影响
public 继承基类的 public 成员在派生类中仍然是 public 的 protected 成员仍然是 protected 的。基类的 private 成员在派生类中不可访问。protected 继承基类的 public 和 protected 成员在派生类中都变成 protected 的。基类 的 private 成员在派生类中不可访问。private 继承基类的 public 和 protected 成员在派生类中都变成 private 的。基类的 private 成员在派生类中不可访问。
示例
#include iostream
using namespace std;
//基类父类
class Vehicle{ //交通工具车,抽象的概念
public:string type;string contry;string color;double price;int numOfWheel;
protected:int protectedData;
private:int privateData;
public:void run(){cout 车跑起来了 endl;}void stop();
};
//私有继承测试
class TestClass : private Vehicle{
public:void tsetFunc(){price 10; //基类的公有数据被私有继承后在派生类中权限编程私有只限在类内部使用}
};
//公有继承测试
class Truck : protected Vehicle{
public:void testFunc(){type 数据测试; //编程了公有权限protectedData 10; //保持公有权限privateData 10; //报错了基类的私有成员不管哪种方式的继承都是不可访问的。}
};
//公有继承基类的公有权限和保护权限不变私有成员不能访问
class Bickle : public Vehicle{
public:void testFunc(){protectedData 10;}
};
//派生类子类
class Roadster : public Vehicle{ //跑车也是抽象比父类感觉上范围缩小了点
public:int stateOfTop;void openTopped();void pdrifting();
};
int main()
{TestClass test;test.price 3.3; //报错了基类的公有成员被私有继承后降为私有权限Truck t;t.type 测试; //报错了基类的公有成员被保护继承后降为保护权限t.protectedData 10; //从报错信息看出保护继承造成基类的保护成员还是保持保护权限Roadster ftype;ftype.type 捷豹Ftype;ftype.run();Bickle bike;bike.type 死飞;bike.run();return 0;
}基类构造函数
派生类可以通过其构造函数的初始化列表来调用基类的构造函数。这是在构造派生类对象时初 始化基类部分的标准做法
当创建派生类的对象时基类的构造函数总是在派生类的构造函数之前被调用。如果没有明确指定将 调用基类的默认构造函数。如果基类没有默认构造函数或者你需要调用一个特定的基类构造函数就 需要在派生类构造函数的初始化列表中明确指定
示例
假设我们有一个基类 Base 和一个派生自 Base 的类 Derived
class Base {
public:int data;Base(int x) {std::cout Base constructor with x x std::endl;}
};
class Derived : public Base {
public:double ydata;Derived(int x, double y) : Base(x) { // 调用 Base 类的构造函数std::cout Derived constructor with y y std::endl;}
};
int main() {Derived obj(10, 3.14); // 首先调用 Base(10)然后调用 Derived 的构造函数return 0;
}Base 类有一个接受一个整数参数的构造函数。Derived 类继承自 Base 它的构造函数接受一个整数和一个双精度浮点数。在其初始化列表中 它调用 Base 类的构造函数并传递整数参数。当 Derived 类的对象被创建时首先调用 Base 类的构造函数然后调用 Derived 类的构造函 数。