Demonstrating composition--an object with member objects : class combination « Class « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Class » class combination 
9.15.3.Demonstrating composition--an object with member objects
#include <iostream>
using std::cout;
using std::endl;

#include <cstring> 
using std::strlen;
using std::strncpy;



class Date 
{
public:
   Dateint 1int 1int 1900 );
   void print() const
   ~Date()
private:
   int month; 
   int day; 
   int year;
};

Date::Dateint mn, int dy, int yr )
{
   month = mn;
   year = yr;
   day = dy );
}
void Date::print() const
{
   cout << month << '/' << day << '/' << year; 
}
Date::~Date()

   cout << "Date object destructor for date ";
   cout << endl;
}

class Employee
{
public:
   Employeeconst char * const, const char * const, 
      const Date &, const Date & );
   void print() const;
   ~Employee();
private:
   char firstName25 ];
   char lastName25 ];
   const Date birthDate; 
   const Date hireDate; 
};
Employee::Employeeconst char const first, const char const last,const Date &dateOfBirth, const Date &dateOfHire )
   : birthDatedateOfBirth ), hireDatedateOfHire 
{
   strcpyfirstName, first);
   strcpylastName, last);
}
void Employee::print() const
{
   cout << lastName << ", " << firstName << "  Hired: ";
   hireDate.print();
   cout << "  Birthday: ";
   birthDate.print();
   cout << endl;
}
Employee::~Employee()

   cout << "Employee destructor: " << lastName << ", " << firstName << endl;
}

int main()
{
   Date birth7241949 );
   Date hire3121989 );
   Employee manager"A""B", birth, hire );

   cout << endl;
   manager.print();

   return 0;
}
B, A  Hired: 3/12/1989  Birthday: 7/24/1949
Employee destructor: B, A
Date object destructor for date
Date object destructor for date
Date object destructor for date
Date object destructor for date
9.15.class combination
9.15.1.Use class as a member field
9.15.2.Initialize inner object using initialization syntax
9.15.3.Demonstrating composition--an object with member objects
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.