ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อทำความเข้าใจคอนเทนเนอร์ใน C++
พารามิเตอร์ถ้าคลาสใดคลาสหนึ่งมีคลาสอื่นเรียกว่าเป็นคอนเทนเนอร์ คลาสภายในเรียกว่าคลาสที่บรรจุ ในขณะที่คลาสที่มีอยู่นั้นเรียกว่าคลาสคอนเทนเนอร์
ตัวอย่าง
#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