19. Container Classes ( Nesting of Classes) class one{ public: void display(void) { cout<<"Class A"<<endl; } }; class two{ public: void display(void) { cout<<"Class B"<<endl; } }; class three { one obj1; two obj2; public: void display(void) { obj1.display(); obj2.display(); cout<<"Class C"<<endl; } }; int main() { three obj3;obj3.display(); return(0); } 20. Need for Class Template const size=3; class array { int *p; public: array() { p=new int[size]; for(int i=0;i<size;i++) { p[i]=0; } } array(int *a) { p=new int[size]; for(int i=0;i<size;i++) p[i]=a[i]; } int operator +(array &b) { int sum=0; for(int i=0;i<size;i++) sum+=this->p[i]+b.p[i]; return(sum); } void display(void) { for(int i=0;i<size;i++) }; int main() { int x[size]={2,4,6}; int y[size]={1,3,5};
cout<<"\t"<
1
array a1; array a2; a1.display(); a2.display(); a1=x; a2=y; a1.display(); a2.display(); int r=a1+a2; cout<<"r="<
class array { T *p; public: array() { p=new T [size]; for(T i=0;i<size;i++) { p[i]=0; } } array(T *a) { p=new T[size]; for(T i=0;i<size;i++) p[i]=a[i]; } T operator +(array &b) { T sum=0; for(T i=0;i<size;i++) sum+=this->p[i]+b.p[i]; return(sum); } void display(void) { for(T i=0;i<size;i++) };
cout<<"\t"<
2
int main() { int x[size]={2,4,6}; float y[size]={1.5,3.4,5.2}; array a1; array a2; a1.display(); cout<<"\n"; a2.display(); cout<<"\n"; a1=x; a2=y; a1.display(); cout<<"\n"; a2.display(); cout<<"\n"; return(0); } 22. Class Template with multiple parameters template class sample { T1 a; T2 b; public: sample(T1 x, T2 y) { a=x; b=y; } void display(void) { cout<<"a="< s1(1.23,123); sample s2(10,'a'); s1.display(); cout<<"\n"; s2.display(); cout<<"\n"; return(0); } 23.Function Template #include #include #include<stdio.h> template void swap(T &x,T &y) { T temp=x;
3
x=y; y=temp; } void fun(int m,int n,float x,float y) { cout<<"m and n before swap"<<m<<" "< void display(T1 x,T2 y) { cout<<"x ="<<x<<"y= "< class array { T *p; public: array(); array(T *a); void display(void); }; template array :: array() { p=new T[size]; for(int i=0;i<size;i++) {
4
p[i]=0; } } template array ::array(T *a) { p=new T[size]; for(int i=0;i<size;i++) p[i]=a[i]; } template void array ::display(void) { for(int i=0;i<size;i++) cout<<"\t"< a1; array a2; a1.display(); cout<<"\n"; a2.display(); cout<<"\n"; a1=x; a2=y; a1.display(); cout<<"\n"; a2.display(); return(0); } 26. Non –Type Templates template class array { T a[size]; public: array(); void display(void); }; template array :: array()
5
{ cout<<"Constructor Called \n"; for(int i=0;i<size;i++) { cout<<"Enter "<<<"Value"; cin>>a[i]; } } template void array ::display(void) { for(int i=0;i<size;i++) cout<<"\t"< a1; // array of 5 integers a1.display(); cout<<"\n\n"; array a2; // array of 5 floats. a2.display(); cout<<"\n\n"; array a3; a3.display(); cout<<"\n\n"; return(0); } /* In addition to the type argument T, we can also use other arguments such as strings, function names, constant expressions and built-in types. Size parameter specify the size of array and is known to compiler at the compile time it self. */
6