Dynamic Binding

This is a discussion on Dynamic Binding within the C++ Programming forums, part of the General Programming Boards category; I'm having some trouble figuring this out. I have a class heirarchy Project --> SimpleProject --> MinorProject --> MajorProject Now ...

  1. #1
    Registered User gpr1me's Avatar
    Join Date
    Mar 2006
    Posts
    14

    Dynamic Binding

    I'm having some trouble figuring this out.

    I have a class heirarchy

    Project
    --> SimpleProject
    --> MinorProject
    --> MajorProject

    Now a simple project and a minor project can be merged to create a simple project, a simple project and a major project can be merged to create a major project. Basically any project can be merged with any other project which creates a new project.

    A simple project contains one and only one task. A Minor project contains a list of tasks, so merging a simple and minor project would create a new minor project with the list of the previous minor project and with the task from the simple project added to it. Also a major project is just like a minor project but with dependency's between tasks. So merging a major and a minor gives a major. Does this all make sense?

    So basically i want to write a function which does this. I have been told it can be done using Dynamic Binding but im not seeing how...

    Here are my class definition files if that helps you picture this more

    Project.h:
    Code:
    #ifndef PROJECT_H
    #define PROJECT_H
    
    // forward declaration
    class Task;
    
    class Project
    {
    
    public:
    	Project();
    	Project(const Project &);	
    	virtual ~Project();
    
    	// some pure virtual functions
    	virtual void AddTask(Task *) = 0;
    	virtual void DoTask() = 0;
    	virtual bool HasTasks() = 0;
    
    private:
    	// still to add...
    };
    
    #endif
    SimpleProject.h:
    Code:
    #ifndef SIMPLEPROJECT_H
    #define SIMPLEPROJECT_H
    
    #include "Project.h"
    #include "Task.h"
    
    class SimpleProject : public Project
    {
    
    public:
    	SimpleProject();
    	SimpleProject(const SimpleProject &);
    	~SimpleProject();
    	void AddTask(Task *);
    	void DoTask();
    	bool HasTasks();
    	SimpleProject & operator=(const SimpleProject & other);
    	friend std::ostream & operator<<(std::ostream & os,
    									 const SimpleProject & sp);
    	friend bool operator==(const SimpleProject & left,
    						   const SimpleProject & right);
    	friend bool operator!=(const SimpleProject & left,
    						   const SimpleProject & right);
    
    private:
    	Task *task;
    	// the total number of tasks
    	int numTasks;
    };
    
    #endif
    MinorProject.h
    Code:
    #ifndef MINORPROJECT_H
    #define MINORPROJECT_H
    
    #include <list>
    #include "Project.h"
    #include "Task.h"
    
    class MinorProject : public Project
    {
    
    public:
    	MinorProject();
    	MinorProject(const MinorProject &);	
    	~MinorProject();
    	void AddTask(Task *);
    	void DoTask();
    	bool HasTasks();
    	MinorProject & operator=(const MinorProject &);
    	friend std::ostream & operator<<(std::ostream &, const MinorProject &);
    	friend bool operator==(const MinorProject &, const MinorProject &);
    	friend bool operator!=(const MinorProject &, const MinorProject &);
    
    private:
    	list<Task> *task_list;
    };
    
    #endif
    MajorProject.h (this one is not yet competed...):
    Code:
    #ifndef MAJORPROJECT_H
    #define MAJORPROJECT_H
    
    #include "Project.h"
    #include "Task.h"
    #include <list>
    #include <vector>
    
    class MajorProject : public Project
    {
    
    public:
    	MajorProject();
    	MajorProject(const MajorProject &);
    	//MajorProject(list<Task> *, vector<int *> *);
    	~MajorProject();
    	void AddTask(Task *);
    	void AddDependency(const vector<int> &);
    	void DoTask();
    	bool HasTasks();
    	MajorProject & operator=(const MajorProject & other);
    	friend std::ostream & operator<<(std::ostream & os, const MajorProject & mp);
    	friend bool operator==(const MajorProject & left, const MajorProject & right);
    	friend bool operator!=(const MajorProject & left, const MajorProject & right);
    
    private:
    	list<Task> *task_list;
    	vector<vector<int>> *depend_vec;
    };
    
    #endif
    If anyone can point me int he rigth direction i would appreciate that.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by gpr1me
    A simple project contains one and only one task. A Minor project contains a list of tasks, so merging a simple and minor project would create a new minor project with the list of the previous minor project and with the task from the simple project added to it. Also a major project is just like a minor project but with dependency's between tasks. So merging a major and a minor gives a major. Does this all make sense?

    So basically i want to write a function which does this. I have been told it can be done using Dynamic Binding but im not seeing how...
    As far as I understand your problem (Correct me if I'm wrong), A Major project has a "collection" of minor projects.. and a Minor project has a "collection" of simple projects.

    Representing the "has-a" relationship is best done with a class member rather than dynamic binding IMHO.

    eg,
    Code:
    class SimpleProject
    {
       Task t;
    };
    
    class MinorProject
    {
       std::list<SimpleProject> lsSimple;
    };
    
    class MajorProject
    {
       std::list<MinorProject> lsMinor;
    };
    Of course, Dynamic binding may come in useful for other aspects of the program, so I wouldn't rule it out completely (There may be some common accessor methods which are true to all types of projects) - but I don't believe inheritance represents the relationship you're looking for.
    Last edited by Bench82; 03-24-2006 at 08:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boost::shared_ptr and dynamic binding
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 03:50 PM
  2. Dynamic binding
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2006, 04:37 PM
  3. dynamic binding
    By freethenet in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-26-2004, 03:31 PM
  4. dynamic or static binding
    By noob2c in forum C++ Programming
    Replies: 1
    Last Post: 08-06-2003, 01:43 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21