C++ 49: Containership
- It is also called as container classes or composition or nested classes or member classes for aggregation or collection.
- Container ship represents has a relationship.
- Contains object of data member such a mechanism is called as containership.
Example:
Use the concept of container ship to design a class student with data paper roll number name date of birth department. define Member function get info and show in.
#include <iostream>#include <conio.h>using namespace std;class Date{private:int D, M, Y;public:void readData(){cout << "Enter Date (DD/MM/YYYY): ";cin >> D;cin.ignore();cin >> M;cin.ignore();cin >> Y;}void showData(){cout << "Date: " << D << " : " << M << " : " << Y << endl;}};class Student{private:int RN;char N[20], DEP[10];Date DOB;public:void getInfo(){cout << "Enter student ROll: " << endl;cin >> RN;cout << "Enter student Name: " << endl;cin.sync();cin >> N;cout << "Enter Dept: ";cin >> DEP;DOB.readData();}void showData(){cout << "\nDisplaying Student Data\n"<< endl;cout << "Roll No: " << RN << endl;cout << "Name: " << N << endl;cout << "Dept: " << DEP << endl;DOB.showData();}};main(){Student a;a.getInfo();a.showData();return 0;}
Output:
Enter student ROll:
1022
Enter student Name:
Jack
Enter Dept: CSE
Enter Date (DD/MM/YYYY): 24/03/2002
Displaying Student Data
Roll No: 1022
Name: Jack
Dept: CSE
Date: 24 : 3 : 2002
Comments
Post a Comment