• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Aggregation in C++

Aggregation

Aggregation is a special type of composition.

In this contained object is created outside container object.

This contained object is send to container object using member function or constructor.

In this approach contained object is exist independent container object.

Aggregation in C++

-Aggregation is a way of composing different abstractions together in defining a class. For example, a car class can be defined to contain other classes such as engine class, seat class, wheels class etc. The car class can define an engine class as one of its attributes. A concrete instance of a car class will have its attribute as engine which will hold a concrete instance of engine class. For example, two instances of a car class fiat and ford will have their engine attributes which will hold a ford engine and fiat engine objects respectively.

-Objects could share other objects. When ford and fiat are using the same engine design, it is possible for them to share one engine design object. In such a case, engine design object is said to be nominally part of car design object.

Example of Aggregation in C++

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);
}
  • Member function with parameters of type class it receive object.
  • Member function can have parameters of sum class or different class.
  • This member function performs operation on more than one object.

Example

//WAP to compare marks of two students

# 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);
}

//WAP to compare two  strings:

# 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();
}

Member Function with return type of class in C++

if member function having return type of class it returns object.

int fun1[]
float fun2[]
double fun3[]
string fun4[]

// WAP to add two matrices

# 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)

//Adding two complex numbers

#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();
}

For Indepth details on C++ click on

History of C++ & Advantages & Disadvantages

Data Types in C++ Language

C++ compilers & Structure of C++ Program

Inheritance in C++ Language

Functions in C++ – Part II

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Aggregation in C++"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.