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 do you mean by abstraction?

0 Answers  


If a=5, b=6, c=7, b+=a%c*2. What is the final value of b?

0 Answers  


How is exception handling carried out in c++?

3 Answers  


what is diff between .net 1.1 and .net 2.0

4 Answers  


what is ltti

1 Answers   Unisys,






WHAT IS THE DIFFERENCE BETWEEN ABSTRUCTION AND ENCAPSULATION? PLEASE EXPLAIN IT.

7 Answers   ETH,


Hi friends I have experience of 6 months in website design and maintanence. Now i am looking for other IT jobs.. to switch platform. please post any interview you know in chennai.

0 Answers  


Can a destructor be called directly?

0 Answers  


What is virtual destructor? Why?

3 Answers   Agile Software, College School Exams Tests, CSC,


How to deploy web appliction in web logic ?

1 Answers   Unisys,


what is the basic concept of inheritance?

9 Answers  


why to use operator overloading

3 Answers  


Categories