Aggregation is a special type of composition.
In this, contained object is designed behind container object.
This contained object is later sent to container object via member function.
In this approach contained object exists independent container object.
-Aggregation composes various abstractions into a one defining a class.
-Objects can share other objects.
class alpha { int x; public: void read-x() { cin>>x; } void print.x() { cout<<x; } }; class beta { int y; alpha *a; public: beta(alpha*p) } a=p; } void main() { alpha a1; beta b1(&a1); }
Example
# include<iostream.h> class Marks { int rno; int sub1,sub2; public: void read-marks() { cout<<”\n Input Two subjects”; cin>>sub1>>sub2; } void compare(Marks s) { if (sub1 ==s,sub1&& sub2==s.sub2) cout<<”\n Marks are not equal”; } }; void Main() { Marks stud1,stud2; stud2.read-marks(); stud1.compare(stud2); }
# include <iostream.h> # include <string.h> # include <conio.h> class string { char str[10]; public: void read-string() { cout<<”\n Input any string”; cin>>str; } void print-string() { cout<<”\n String is “<<str; } void compare(string s) { if strcmp(str,s.str)==0) cout<<”\n Strings are equal”; else cout<<”\n strings are not equal”; }}; void main() { string str1,str2; str1.read-string(); str2.read-string(); str1.compare(string2); str1.print-string(); str2.print-string(); }
if member function is having return type of class, it returns object.
int fun1[] float fun2[] double fun3[] string fun4[]
# include<iostream.h> class Marix { inta[2][2]; public: void read-Matrix() { for(int i=0;i<2;i++) for(intj=0;j<2;j++) cin>>a[i][j]; } void print-Matrix() { for(inti=0;i<2;i++) { for(int i=0;i<2;i++) { for(int j=0;j<2;j++) cout<<a[i][j]<<”\t”; cout<<endl: } } Matirx add-Matrix(MatrixM2) { MatrixM3; for (int i=0;i<2;i++) for(int j=0;j<2;j++) M3.a[i][j]=a[i][j]+M2.a[i][j]; return M3; } }; void main() { Matrix Matrix1,Matrix2;Matrix3; Matrix1.read-Matrix(); Matrix2.read-Matrix(); Matrix3=Matrix1.add-Matrix(Matrix2); Matrix1.print-Matrix(); Matrix2.print-Matrix(); Matrix3.print-Matrix(); }
E.g.:
(Return type as class)
#include <iostream.h> class complex { float read; float img; public: void read_complex() { cout<<”\n Input real”; cin>>real; cout<<”\n Input img”; cin>>img; } void print-complex() { cout<<”\n real=”<<real; cout<<”\n img=”<<img; } complex add_complex(complex c2) { Complex c3; c3.real=real+c2.real; c3.img=img+c2.img; return c3; }}; void main() { complex comp1,comp2 comp3; comp1.read-complex(); comp3=comp1.add-complex comp2; comp1.print-complex(); comp2.print-complex(); comp3.print-complex(); }
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.