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

Is data hiding and abstraction same?

0 Answers  


Why do we use oop?

0 Answers  


i got a backdoor offer in process global,Bangalore..Can i work with it?

0 Answers  


Is react oop?

0 Answers  


what is virtual destructor

7 Answers   L&T, TCS,


Write a program to compute for numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the following format: Each line contains a student's first name, then one space, then ten quiz scores all on one line. The quiz scores are in whole number and are separated by one space. Your program will take it input from this file and sends it output to a second file. The data in the output file will be exactly the same as the data in the input file except that there will be one additional number (of type double) at the end of each line. This number will be the average of the student's ten quiz scores. Use at least one function that has file streams as all or some of its arguments.

0 Answers  


What is abstraction encapsulation?

0 Answers  


Why is object oriented programming so hard?

0 Answers  


What is variable example?

0 Answers  


OOP'S advantages of inheritance include:

1 Answers   Infosys,


features of OOPS

22 Answers   Ness Technologies, Satyam,


How to use CMutex, CSemaphore in VC++ MFC

0 Answers   Persistent, TCS,


Categories