Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

Containership ใน C++


ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อเข้าใจ containership (การรวมคลาส) ใน C++

เมื่อคลาสหนึ่งมีคลาสอีกคลาสหนึ่งเป็นสมาชิก เรียกว่า containership คลาสภายในเรียกว่า contained class ส่วนคลาสที่ครอบคลุมเรียกว่า container class

ตัวอย่างโค้ด

#include <iostream> 
using namespace std; 

class first {
public:
    first() {
        cout << "Hello from first class\n";
    }
};

// container class
class second {
    first f;
public:
    // constructor
    second() {
        cout << "Hello from second class\n";
    }
};

int main() {
    second s;
    return 0;
}

เอาต์พุต

Hello from first class
Hello from second class