0% found this document useful (0 votes)
152 views3 pages

Programming Fundamentals (Composition Relationship)

1. The document discusses composition in C++ where one class is composed of objects of another class. 2. It provides an example of two classes - PointType and CircleType where CircleType has an object of PointType to store coordinates and uses it to calculate area. 3. The main purpose is to demonstrate how to pass arguments to the constructors of member objects and manipulate data between composed classes in an organized way.

Uploaded by

Arslan Sohail
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
152 views3 pages

Programming Fundamentals (Composition Relationship)

1. The document discusses composition in C++ where one class is composed of objects of another class. 2. It provides an example of two classes - PointType and CircleType where CircleType has an object of PointType to store coordinates and uses it to calculate area. 3. The main purpose is to demonstrate how to pass arguments to the constructors of member objects and manipulate data between composed classes in an organized way.

Uploaded by

Arslan Sohail
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

Introduction:

In composition one or more members of a class are objects of another class.


Composition is a ‘has a’ relationship.
class DOB {
// member variables/ functions
}; class person {
DOB date; // Declare object of date of birth
// member variable/ functions
} student;
Member of DOB can be accessed through object of class person using dot
operator. The following statements illustrate how to pass arguments to the
constructors of the member objects DOB:
person::person(string f_n, string l_n, int m, int d, int y)
:date(m, d, y)
{
// Definition of Constructor person
}
Objective:
 Composition
Design/Procedure:
Question# 1
We declared two classes ‘PointType’ and ‘CircleType’. Class PointType has two
variables ‘int x’, ‘int y’ stores x,y coordinated, two member functions ‘void cent()’
to store x,y co-ordinates and ‘void print()’ to print x,y co-ordinates.
Class CircleType has two variables ‘double radius’, ‘char * color’, an object of

‘PointType center’, ‘void print()’ to print radius and color, ‘double calc_area()’ to
calculate area, ‘void setparam(int,int,double,char*) to set values of coordinates,

radius and color. We calculate area by formula A=π*r2

Main function and definition of class was provided and we have to implement class
member function and parameterized constructor.
Issue:
There was no issue in completion of task.
Application:
We can use a class object in another class and creating an aggregation between two
classes.
Conclusion:
Composition help us to reduce code length and gives hold on written code. We can
manipulate data in an organized way.
POST LAB:
#include<iostream>
using namespace std;

class course {
int courseNumber;
int creditHours;
public:
void setCourse(int x, int y);
void printcourse();
};

class section {
int secNumber;
course c;//composition
public:
void setSection(int, int, int);
void printsection();
};

void section::setSection(int cn, int ch, int sect)


{
c.setCourse(cn, ch);
secNumber = sect;
}

void course::setCourse(int x, int y)


{
courseNumber = x;
creditHours = y;
}
void section::printsection()
{
cout << "Section No: " << secNumber;
c.printcourse();
}
void course::printcourse()
{
cout << "\nCourse No: " << courseNumber << "\nCredit Hour: " << creditHours <<
endl << endl;
}
int main()
{
int sectNo = 0;
section sect[7];

for (int i = 0; i < 7; i++)


{
cout << "Assign Section A Unique Number From 1-7" << endl;
cin >> sectNo;

sect[i].setSection(117, 3, sectNo);
}
system("cls");

for (int i = 0; i < 7; i++)


{
sect[i].printsection();
}
system("pause");
return 0;
}

You might also like