Group Discounts available for 3+ students and Corporate Clients

Aggregation in C++

Aggregation

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 in C++

-Aggregation composes various abstractions into a one defining a class.

-Objects can share other objects.

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 operates 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 is 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 *

    Support


    Please Enter Your Details and Query.
    Three + 6