📜  C++中Containership和继承的区别

📅  最后修改于: 2022-05-13 01:55:05.607000             🧑  作者: Mango

C++中Containership和继承的区别

Containership 当一个类的对象被创建到另一个类中时,该对象将成为该类的成员,类之间的这种关系称为containershiphas_a关系,因为一个类包含另一个类的对象。

在这种关系中包含对象和另一个类的成员的类称为容器类,属于另一个对象的对象称为包含对象,而包含另一个对象作为其一部分或属性的对象称为容器类。容器对象

集装箱船的语法

C++
// Class that is to be contained
class first {
  
    // Class Definition
};
  
// Container class
class second {
  
    // Creating object of first
    first f;
    .
        .
};


C++
// C++ program to illustrate the
// concept of containership
#include 
using namespace std;
  
class first {
public:
    void showf()
    {
        cout << "Hello from first class\n";
    }
};
  
// Container class
class second {
  
    // Create object of the first-class
    first f;
  
public:
    // Define Constructor
    second()
    {
        // Call function of first-class
        f.showf();
    }
};
  
// Driver Code
int main()
{
    // Create object of second class
    second s;
}


C++
class subclass_name : access_mode base_class_name {
    // body of subclass
};


C++
// C++ program to demonstrate the
// concept of inheritance
  
#include 
using namespace std;
  
// Base class
class Parent {
public:
    int id_p;
};
  
// Sub class inheriting from the
// Base Class(Parent)
class Child : public Parent {
public:
    int id_c;
};
  
// Driver Code
int main()
{
  
    Child obj1;
  
    // An object of class child has
    // all data members and member
    // functions of class parent
    obj1.id_c = 7;
    obj1.id_p = 91;
  
    cout << "Child ID is "
         << obj1.id_c << endl;
  
    cout << "Parent ID is "
         << obj1.id_p << endl;
  
    return 0;
}


方案一:



下面是解释 C++ 中容器概念的程序:

C++

// C++ program to illustrate the
// concept of containership
#include 
using namespace std;
  
class first {
public:
    void showf()
    {
        cout << "Hello from first class\n";
    }
};
  
// Container class
class second {
  
    // Create object of the first-class
    first f;
  
public:
    // Define Constructor
    second()
    {
        // Call function of first-class
        f.showf();
    }
};
  
// Driver Code
int main()
{
    // Create object of second class
    second s;
}
输出:
Hello from first class

说明:在上面的第二个类中,有一个类first的对象。这种类型的继承被称为has_a关系作为第二类具有类的对象作为第一成员其。从对象f首先调用类的函数。

继承是一个类从另一个类(称为基类)派生属性和特征的能力。它是面向对象编程最重要的特性之一。从另一个类继承属性的类称为子类派生类。其属性由子类继承的类称为Base ClassSuper Class

句法:

C++

class subclass_name : access_mode base_class_name {
    // body of subclass
};

说明: subclass_name子类的名称, access_mode是要继承这个子类的模式。例如, publicprivate等。 base_class_name是想要继承子类的基类的名称。

注意:派生类不继承对私有数据成员的访问。但是,它确实继承了一个完整的父对象,其中包含该类声明的任何私有成员。

方案二:

下面是在 C++ 中解释继承的程序:

C++

// C++ program to demonstrate the
// concept of inheritance
  
#include 
using namespace std;
  
// Base class
class Parent {
public:
    int id_p;
};
  
// Sub class inheriting from the
// Base Class(Parent)
class Child : public Parent {
public:
    int id_c;
};
  
// Driver Code
int main()
{
  
    Child obj1;
  
    // An object of class child has
    // all data members and member
    // functions of class parent
    obj1.id_c = 7;
    obj1.id_p = 91;
  
    cout << "Child ID is "
         << obj1.id_c << endl;
  
    cout << "Parent ID is "
         << obj1.id_p << endl;
  
    return 0;
}
输出:
Child ID is 7
Parent ID is 91

说明:在上面的程序中,子类是从父类公开继承的,所以父类的公共数据成员也会被子类继承。

继承和容器之间的区别

InheritanceContainership
It enables a class to inherit data and functions from a base classIt enables a class to contain objects of different classes as its data member.
The derived class may override the functionality of the base class.The container class can’t override the functionality of the contained class.
The derived class may add data or functions to the base class.The container class can’t add anything to the contained class.
Inheritance represents a “is-a” relationship.Containership represents a “has-a” relationship.

想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程