ya_zay4eg

//46_47_Composition

Jun 26th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. //46_47_Composition
  2. #include <iostream>
  3. #include "Birthday.h"
  4. #include "People.h"
  5. using namespace std;
  6.  
  7. /* 1. CLASS can have an objects of another class
  8. as it's member
  9.    2. We'll store in People's class object from Birthday class
  10.  */
  11.  
  12.  
  13. int main()
  14. {
  15. Birthday biOb (06,30,1984);
  16. People buckyOb ("Roma the King", biOb);
  17. buckyOb.printInfo();
  18. }
  19. //------------------------------------------------------------------------
  20. #ifndef BIRTHDAY_H
  21. #define BIRTHDAY_H
  22.  
  23.  
  24. class Birthday
  25. {
  26.     public:
  27.         Birthday(int m, int d, int y);//we'll passing info to constructor
  28.         /*we'll set a birthday when create a Birthday
  29.          object*/
  30.          void printDate();
  31.     private:
  32.         int month;
  33.         int day;
  34.         int year;
  35. };
  36.  
  37. #endif // BIRTHDAY_H
  38. //----------------------------------------------------------------------------
  39. #include "Birthday.h"
  40. #include <iostream>
  41. using namespace std;
  42. Birthday::Birthday(int m, int d, int y)
  43. {
  44.     month=m;
  45.     day=d;
  46.     year=y;
  47. }
  48. void Birthday::printDate(){
  49. cout<<month<<"/*"<<day<<"/*"<<year;
  50. }
  51. //----------------------------------------------------------------------------------
  52. #ifndef PEOPLE_H
  53. #define PEOPLE_H
  54. #include <string>//we gonna passing string variables
  55. /*but i can't understand we set it's #include's here*/
  56. #include "Birthday.h"/*as one of the members we gonna
  57.                        use a Birthday object in this class*/
  58. using namespace std;//why is it here?
  59. class People
  60. {
  61.     public:
  62.         People(string x, Birthday bo);//constructor create an object
  63.         void printInfo();//print out information
  64.     private:
  65.         string name;
  66.         Birthday dayOfBirth;//passing date of birth OBJECT not INT
  67. };
  68. #endif // PEOPLE_H
  69. //-------------------------------------------------------------------
  70. #include "People.h"
  71. #include <iostream>
  72. #include "Birthday.h"
  73. using namespace std;
  74.  
  75. People::People(string x, Birthday bo)//we need to assign a name and the birth
  76. /* every time you work with class
  77. inside another class you need to use
  78. a member initializer list*/
  79. :name (x), dayOfBirth(bo)//will take a string and set it equal to name
  80.                           //and take birthday obj and set it equal to dateOfBirth
  81. {
  82. }
  83.  
  84. void People::printInfo(){
  85. cout<<"Name: "<<name;
  86. dayOfBirth.printDate();
  87. }
Advertisement
Add Comment
Please, Sign In to add comment