954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

composition

//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
CurtisBridges
Light Poster
38 posts since Sep 2006
Reputation Points: 79
Solved Threads: 1
 

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

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 
I recommend you read the Wikipedia article and see if that clears things up. ;)


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

CurtisBridges
Light Poster
38 posts since Sep 2006
Reputation Points: 79
Solved Threads: 1
 
//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 ?

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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
CurtisBridges
Light Poster
38 posts since Sep 2006
Reputation Points: 79
Solved Threads: 1
 

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 .

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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

CurtisBridges
Light Poster
38 posts since Sep 2006
Reputation Points: 79
Solved Threads: 1
 
Thanks for the help and he links thatwas good info. Merry Christmas


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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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

wayne_b
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You