//I need to change this program from using inheritance to using composition.
//Could someone please show me how? I have two other programs that I can surely do myself.
//I'm really in search of a sample.

// Fig. 9.17: point3.h
// Point3 class definition represents an x-y coordinate pair.

#ifndef POINT3_H
#define POINT3_H
class Point3 {
public:
Point3( int = 0, int = 0 ); // default constructor
void setX( int ); // set x in coordinate pair
int getX() const; // return x from coordinate pair
 
void setY( int ); // set y in coordinate pair
int getY() const; // return y from coordinate pair
 
void print() const; // output Point3 object
private: 
int x; // x part of coordinate pair
int y; // y part of coordinate pair
}; // end class Point3
#endif

Recommended Answers

All 8 Replies

I recommend you read the Wikipedia article and see if that clears things up. ;)

Thanks, Been there, Done that. Just more confused.

//I need to change this program from using inheritance to using composition.
//Could someone please show me how? I have two other programs that I can surely do myself.
//I'm really in search of a sample.

I don't see any inheritance in your program, just a single point class. And I think that for inheritance you need to have more than one class ;)

Have you posted the entire code or is this just a part of it ?

Sorry, about that. I posted incorrect file. Hope this makes more sense.

#ifndef CIRCLE4_H
#define CIRCLE4_H
#include "point3.h" // Point3 class definition
class Circle4 : public Point3 {
public:
// default constructor
Circle4( int = 0, int = 0, double = 0.0 ); 
void setRadius( double ); // set radius
double getRadius() const; // return radius
double getDiameter() const; // return diameter
double getCircumference() const; // return circumference
double getArea() const; // return area
void print() const; // output Circle4 object
private: 
double radius; // Circle4's radius
}; // end class Circle4
#endif

You can drop the inheritance and put in the Circle class a private data member center of type point. This way you would be able to use all the methods of the Point class on the center of the circle while keeping the design of Circle class intact.

class Circle:public Point3d
{
    // all the members
} ;

// with composition
class Circle
{
    Point3d* pt ;   // or Point3d pt, your call
  
   // other circle members 
} ;

Also read this and this.

You can drop the inheritance and put in the Circle class a private data member center of type point. This way you would be able to use all the methods of the Point class on the center of the circle while keeping the design of Circle class intact.

class Circle:public Point3d
{
    // all the members
} ;
 
// with composition
class Circle
{
    Point3d* pt ;   // or Point3d pt, your call
 
   // other circle members 
} ;

Also read this and this.

Thanks for the help and he links thatwas good info. Merry Christmas

commented: Merry Christmas, I like your manners - ~s.o.s~ +9

Thanks for the help and he links thatwas good info. Merry Christmas

AH the Christmas spirit...Merry Christmas to you too my good friend.

i want to know how to use composition while doing a program that also operates polymorphically

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.