Write a program to demonstrate the use of 'Composition' in C++

Answers were Sorted based on User's Feedback



Write a program to demonstrate the use of 'Composition' in C++..

Answer / mahendra

Find the program below to demonstrate the composition.
first define the class called the DataBirth.
class DateOfBirth
{
public:
void UpdateDMY();
void GetDMY();
private:
int date,month,year;
};
define the Employee class.
class Employee
{
public:
void GetDetails();
void UpdateDetails();


private:
DateOfBirth BirthDate;
}
Here the EMployee class is having the object of the
DateOfBirth class as data member. With this the Employee
class is achieving the "has-a" relation with the
DateOfBorth class. In this way the Employee class can have
objects of the many calsses as members.

Is This Answer Correct ?    14 Yes 8 No

Write a program to demonstrate the use of 'Composition' in C++..

Answer / jojo

#ifndef POINT2D_H
#define POINT2D_H

#include <iostream>

class Point2D
{
private:
int m_nX;
int m_nY;

public:
// A default constructor
Point2D()
: m_nX(0), m_nY(0)
{
}

// A specific constructor
Point2D(int nX, int nY)
: m_nX(nX), m_nY(nY)
{
}

// An overloaded output operator
friend std::ostream& operator<<(std::ostream& out, const
Point2D &cPoint)
{
out << "(" << cPoint.GetX() << ", " << cPoint.GetY()
<< ")";
return out;
}

// Access functions
void SetPoint(int nX, int nY)
{
m_nX = nX;
m_nY = nY;
}

int GetX() const { return m_nX; }
int GetY() const { return m_nY; }
};

#endif

Is This Answer Correct ?    1 Yes 3 No

Post New Answer

More OOPS Interview Questions

what is the function of 'this' operator ?

7 Answers   Wipro,


INSTANCE FIELDS DECLARED private ARE ACCESSIBLE BY THE METHODS ONLY.CAN WE CHANGE THE private FIELD OF AN OBJECT IN A METHOD OF SOME OTHER OBJECT OF THE SAME CLASS?

0 Answers  


What is variable example?

0 Answers  


What is the difference between an object and a class?

3 Answers  


What type of loop is a for loop?

0 Answers  






how to create thread in java?

17 Answers   IBM, Infosys, Wipro,


write a program to print * * * * * *

2 Answers  


20% of a 6 litre solution and 60% of 4 litre solution are mixed what the % of mixture of solution it is resulted into?

5 Answers   IonIdea,


What is the problem with multiple inheritance?

0 Answers  


what is the difference b/w abstract and interface?

2 Answers   Merrill Lynch, Schneider, Scio Healthcare,


Write a program to multiply 3x3 matrics

1 Answers  


define oops concept with example

1 Answers   Cap Gemini,


Categories