• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Dealing With Multiple Classes and Constructors

I keep getting the following error:
'Date' : no appropriate default constructor available
'Person' : no appropriate default constructor available

I've already created constructors in both of my indvidual classes and can't seem to figure out why it won't work.

Here's my code:

class Date
{    
private:
     int month;
     int day;
     int year;

public:
     Date( int mo, int dy, int yr) : month(mo), day(dy), year(yr) {}
     void Display()
     {
          cout << month << "/" << day << "/" << year << endl;
     }
};

class Person
{
private:
     char last[30];
     char first[30];
     char zip[6];
     
public:
     Person( char * la, char * fi, char * z )
     {
          strcpy( last, la );
          strcpy( first, fi );
          strcpy( zip, z);
     }
     void Display( void )
     {
          cout << "First Name: " << first << endl;
          cout << "Last Name:  " << last << endl;
          cout << "Zip Code: " << zip << endl;
     }
};

class MagazineSubscription
{
private:
     Date SubDate;
     Person SubInfo;
public:
     MagazineSubscription(Date da, Person pe )
     {
          SubDate= da;
          SubInfo = pe;
     }
         
     void Display( void )
     {
          SubInfo.Display();
          SubDate.Display();
     }
};

void main (void)
{
     Date da1(1,1,2000);
     Person pe1("X", "X", "X");
     MagazineSubscription p1(da1, pe1);
     p1.Display();
}

0
eccentricbeats
Asked:
eccentricbeats
1 Solution
 
ExceterCommented:
You need null constructors. I.E. constructors that take no arguments. Try this,

class Date
{    
private:
    int month;
    int day;
    int year;

public:
    Date(){}
    Date( int mo, int dy, int yr) : month(mo), day(dy), year(yr) {}
    void Display()
    {
         cout << month << "/" << day << "/" << year << endl;
    }
};

class Person
{
private:
    char last[30];
    char first[30];
    char zip[6];
   
public:
    Person(){}
    Person( char * la, char * fi, char * z )
    {
         strcpy( last, la );
         strcpy( first, fi );
         strcpy( zip, z);
    }
    void Display( void )
    {
         cout << "First Name: " << first << endl;
         cout << "Last Name:  " << last << endl;
         cout << "Zip Code: " << zip << endl;
    }
};

class MagazineSubscription
{
private:
    Date SubDate;
    Person SubInfo;
public:
    MagazineSubscription(){}
    MagazineSubscription(Date da, Person pe )
    {
         SubDate= da;
         SubInfo = pe;
    }
         
    void Display( void )
    {
         SubInfo.Display();
         SubDate.Display();
    }
};

void main (void)
{
    Date da1(1,1,2000);
    Person pe1("X", "X", "X");
    MagazineSubscription p1(da1, pe1);
    p1.Display();
}

Exceter
0
 
SalteCommented:
A default constructor is a constructor that takes no arguments.

If a class has no constructors at all and a default constructor is required, the system will make one for you. However it will not do that if you have one constructor that takes arguments.

Default constructors are needed if you declare objects without supplying arguments to the constructor and also when you make an array of objects.

class X {
....
};


X table[14];

This will require X to have a default constructor since you make an array of 14 elements. Each of those 14 X's will be constructed using the default constructor.

I.e. X::X() { .... }

If you don't want a default constructor don't make arrays.

This will also demand a default constructor:

X var;

Here var is constructed using a default constructor. Don't do this either if you want no default constructor.

Allocation from here is the same thing:

X * p = new X; // default constructor.
X * q = new X[4]; // also default constructor.

However, these can work as long as you have appropriate constructor, even if the class has no default one:

X var(1, "hello");
X * r = new X(2, "there");

As long as the class has a constructor that takes an int and a string as argument that code above is just fine.

If the class has no constructors at all, a default constructor will be made for you if it is needed, in that case you don't have to worry about default constructor. The only exception is if you absolutely didn't want anyone to construct the object unless they have access to. In that case declare and define a private or protected default constructor. Also in this case you should not write code that require the constructors to be visible at places where they can't be called.

For example:

void func(X y)
{
   ...
}

Is a bad idea if the class X has private or protected constructors that is required in a place where you can't access them.

void func(X & y)
{
   ...
}

Is better in that it doesn't require any constructor, you don't make a new object, you only reference the existing object. This has the bad side effect that the function can modify the object and so you might want to consider:

void func(const X & y)
{
   ...
}

instead. Here the function cannot modify the object y but it gets a reference so it doesn't have to construct any object.

Alf
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now