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

IN PROGRAMING LANGAUGE A C++ IS PURELY OBJECT ORIENTED OR NOT?

2 Answers  


What is namespace?

15 Answers  


This program numbers the lines found in a text file. Write a program that reads text from a file and outputs each line preceded by a line number. Print the line number right-adjusted in a field of 3 spaces. Follow the line number with a colon, then one space, then the text of the line. You should get a character at a time and write code to ignore leading blanks on each line. You may assume that the lines are short enough to fit within a line on the screen. Otherwise, allow default printer or screen output behavior if the line is too long (i.e., wrap or truncate). A somewhat harder version determines the number of spaces needed in the field for the line numbers by counting lines before processing the lines of the file. This version of the program should insert a new line after the last complete word that will fit within a 72-character line.

0 Answers  


how to write a java program for an output ****0 ***01 **012 *0123 01234

1 Answers  


What is a class?

32 Answers   Infosys, TCS, Thylak,


What is coupling in oop?

0 Answers  


What is basic concept of oop?

0 Answers  


Can we define a class within the interface?

0 Answers  


Why do we use class in oops?

0 Answers  


to find out the minimum of two integer number of two different classes using friend function

0 Answers  


What is virtual constructors/destructors?

4 Answers   IBS,


Please tell me the oops concept with detailed answer

9 Answers   EEE,


Categories