?
Solved

Dealing With Multiple Classes and Constructors

Posted on 2003-02-19
2
Medium Priority
?
214 Views
Last Modified: 2010-04-01
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
Comment
Question by:eccentricbeats
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 7985376
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
 
LVL 12

Accepted Solution

by:
Salte earned 200 total points
ID: 7986241
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question