-2
#include <iostream>
#include <cstring>
using namespace std;

class assistantnurse{
private:
    char* id;
    char dutytime;

public:

    assistantnurse(char[] ="", char =' ');
    void setid(char*);
    void setdutytime(char);
    char* getid()const;
    char getdutytime()const;
    void print()const;
    void inputinfo();
    ~assistantnurse();
};

assistantnurse::~assistantnurse()
{
    delete[] id;
    id=0; 

}

assistantnurse::assistantnurse(char* i, char t)
{
    setid(i);
    setdutytime(t);
}

void assistantnurse::setid(char *i)
{
    int l=strlen(i);
    id=new char[l+1];
    strncpy(id,i,l);
    id[l]='\0';
}

void assistantnurse::setdutytime(char t)
{
    dutytime=t;
}

char* assistantnurse::getid()const{return id;}
char assistantnurse::getdutytime()const{return dutytime;}

void assistantnurse::print()const
{
    cout<<"ID: "<<id<<endl;
    cout<<"Duty time: "<<dutytime<<endl<<endl;
}


void assistantnurse::inputinfo()
{
    char nurseid[20];
    char time;

    cout<<"Enter nurse ID: ";
    cin>>nurseid;

    do
    {
    cout<<"Enter nurse Duty time(d for day/n for night): ";
    cin>>time;
    }while(time!='n' && time!='d');

    setid(nurseid);
    setdutytime(time);
}

class treatingphysician{

private:
    char* id;
    char* phonenumber;
    assistantnurse assistant[2]; //3 assistant nurses through composition from assistantnurse class

public:
    void setid(char*);
    void setphonenumber(char*);
    char* getid()const;
    char* getphonenumber()const;
    void setnurse();
    void getnurse()const;
    treatingphysician(char[] =" ", char[] =" ", assistantnurse[]);
    ~treatingphysician();
    void print()const;
    void inputinfo();
};

void treatingphysician::inputinfo()
{
    char i[20];
    char p[15];

    cout<<"Enter physician ID: ";
    cin>>i;
    setid(i);
    cout<<"Enter physican phone number: ";
    cin>>p;
    setphonenumber(p);



    setnurse();


}

void treatingphysician::print()const
{
    cout<<"Physician ID: "<<id<<endl;
    cout<<"Physician phone number: "<<phonenumber<<endl<<endl;
    getnurse();
}

treatingphysician::~treatingphysician()
{
    delete[] id;
    id=0;
    delete[] phonenumber;
    phonenumber=0;
}


 treatingphysician::treatingphysician(char*i, char*p, assistantnurse k[2]):assistant(k)

{
    setid(i);
    setphonenumber(p);

}

void treatingphysician::setid(char*i)
{
    int l=strlen(i);
    id=new char[l+1];
    strncpy(id,i,l);
    id[l]='\0';
}

void treatingphysician::setphonenumber(char*p)
{
    int l=strlen(p);
    phonenumber=new char[l+1];
    strncpy(phonenumber,p,l);
    phonenumber[l]='\0';
}

char* treatingphysician::getid()const{return id;}
char* treatingphysician::getphonenumber()const{return phonenumber;}

void treatingphysician::setnurse()
{
    for(int i=0;i<3;i++)
    {
        cout<<"Enter info for nurse #"<<i+1<<endl;
        assistant[i].inputinfo();
        cout<<"\n";
    }
}

void treatingphysician::getnurse()const
{
    for(int i=0;i<3;i++)
    {
        cout<<"Nurse #"<<i+1<<" ";
        assistant[i].print();

    }
}


int main()
{
    treatingphysician e;

    e.inputinfo();
    e.print();


    system("pause");
    return 0;
}

two classes; A nurse class and a physician class; The physician has 3 nurses and all his attributes. Using composition to fix this, but i'm stuck on the array of objects for the 3 nurses.

In my second constructor i faced a problem i used to work on composition before with the same method but the only difference is that it wasn't an array of objects used in composition. now I'm stuck.. Please Help

Short example...

`class A{

public: A(); };

class B{ private: A a[2];

public: B(A[]); };

B::B(A c[2]):a(c){}`

HadyZn
  • 17
  • 5
  • This example is too big. Can you produce a small example? Also, what exactly is your error? – QuestionC May 09 '14 at 16:43
  • Your code is not compilable. – R Sahu May 09 '14 at 16:45
  • You should consider using a debugger if your code compiles. – πάντα ῥεῖ May 09 '14 at 16:47
  • I posted the wrong code on purpose; i have the correct code without adding the second class to my constructor. I'm trying to figure out how to send an array of objects from another class to my other constructor. Any example will suffice. – HadyZn May 09 '14 at 16:53
  • @HadyZn _'I posted the wrong code on purpose'_ Then for heaven's sake reduce that wall of code to the really relevant stuff! – πάντα ῥεῖ May 09 '14 at 16:59
  • @HadyZn ... **in your question post** of course, dude! Go edit ... – πάντα ῥεῖ May 09 '14 at 17:05
  • You can copy the array of nurses in your constructor in such a way: `std::copy(begin(k), end(k), assistant)`; Does it solve your problem? Also note that `assistantnurse assistant[2]` defines an array of 2 elements but you need 3 elements. – Alex Antonov May 09 '14 at 17:05
  • i will check if it will solve my problem. And @AlexAntonov what about the 0? 3 nurses {0,1,2} no? because i ran that and it worked my only problem is adding that array into the constructor. – HadyZn May 09 '14 at 17:07
  • No, `assistantnurse assistant[3]` contains 3 elements with indices 0, 1, 2. `assistantnurse assistant[2]` contains 2 elements only with indices 0, 1. – Alex Antonov May 09 '14 at 17:10
  • Yes you're right; i just noticed that mistake. – HadyZn May 09 '14 at 17:12
  • @AlexAntonov The 2/3 mistake was creating a big problem for me after finishing executing my program! :) – HadyZn May 09 '14 at 17:22

1 Answers1

0

Use std::vector instead of raw arrays.

class A
{

    public: 
        A(); 
};
typedef std::vector< A > A_vector;


class B
{ 
    private: 
    A_vector m_a;    

    public: 
    B( A_vector av ) : m_a( av )
    {
    }
};
Rob K
  • 8,757
  • 2
  • 32
  • 36