Hi, I am getting a very weird error about constructors and I do not know why? When The error occurs in class Biorythm, where it is asking me to create a constructor, however I do not need one. But when I do crate one it gives the same error but in my parent class date which has a constructor.
//Option #1
//Question #1
#include <iostream>
#include <cmath>
#include <string>
usingnamespace std;
#define PI 3.141593
class date {
public:
//Date of birth
int m; //month
int y; //year
int d; //d ay
int timeSinceBirth;
int currentM;
int currentY;
int currentD;
date(int x, int y,int z){
d = x;
m = y;
y = z;
currentM = 1;
currentY = 2014;
currentD = 14;
timeSinceBirth= 0;
}
int daysInMonth(int month, int year){
switch(month){
case 1:
return 31;
break;
case 2:
if(isLeap(year)== true){
return 29;
break;
}
else {
return 28;
break;
}
case 3:
return 31;
break;
case 4:
return 30;
break;
case 5:
return 31;
break;
case 6:
return 30;
break;
case 7:
return 31;
break;
case 8:
return 31;
break;
case 9:
return 30;
break;
case 10:
return 31;
break;
case 11:
return 30;
break;
case 12:
return 31;
break;
default:
cout<<"Error in daysInMonth()"<<endl;
return 0;
break;
}
}
bool isLeap(int year){
if( year % 4 == 0 && year % 100 !=0 && year % 400 ==0 )
returntrue;
elsereturnfalse;
}
void daysInRemainingMonth(){
//How many days have passed from the current time in the current month
if( daysInMonth(currentM,currentY) - currentD >=0)
timeSinceBirth += daysInMonth(currentM,currentY) - currentD;
}
void DaysLeft(){
int once = 0;
//calculating number of days since birth
while( y >= 0){
for(int i =1; i <= 12; i++){
//Condition to stop at the date of birth of the person
//calculate time offset in the first month of birth
if(once == 0){
if( daysInMonth(m,y) - d >=0)
timeSinceBirth += daysInMonth(currentM,currentY) - currentD;
once ++;
}
//Calculate "normal days left"
timeSinceBirth += daysInMonth(i,y);
}
y--;
}
}
void calculate(){
daysInRemainingMonth();
DaysLeft();
}
};
class Biorythm : public date {
public:
//Necessary Empty Constructor
Biorythm(){}
int getPhysical(date x){
x.calculate();
int counter = timeSinceBirth;
double answer = 0;
while(counter >=23){
answer = answer + sin(2*PI*timeSinceBirth/23);
counter = counter - 23;
}
cout<<"The Physical Rythm of that person since birth is: "<< answer <<endl;
}
int getEmotional(date x){
x.calculate();
int counter = timeSinceBirth;
double answer = 0;
while(counter >=28){
answer = answer + sin(2*PI*timeSinceBirth/28);
counter = counter - 28;
}
cout<<"The Emotional Rythm of that person since birth is: "<< answer <<endl;
}
int getIntellectual(date x){
x.calculate();
int counter = timeSinceBirth;
double answer = 0;
while(counter >=33){
answer = answer + sin(2*PI*timeSinceBirth/33);
counter = counter - 33;
}
cout<<"The Intellectual Rythm of that person since birth is: "<< answer <<endl;
}
};
int main(){
system("COLOR 2F");
cout<<"Question #1"<<endl;
int month = 0;
int day = 0;
int year = 0;
cout<<"Please Enter Your Date Of Birth (M/D/Y): "<<endl;
cin>>month;
cin>>day;
cin>>year;
date BR(day,month,year);
Biorythm Rythm;
cout<<Rythm.getPhysical(BR);
cout<<Rythm.getEmotional(BR);
cout<<Rythm.getIntellectual(BR);
return 0;
}
When you supply a constructor such as date(int x, int y,int z) { // etc the compiler will not supply any default constructor.
If you intend to create a date object without any parameters, you need to specify a default constructor too such as: date() { } // default constructor though you may wish to add appropriate code to initialise the object with sensible default values.
You always need a constructor to be able to create a class object. If you do not define any constructor the compiler will automatically create a default constructor (if possible). A default constructor is a constructor that takes no arguments.
The automatically generated default constructor will call the default constructor of the base classes. In your case date is the base class but it doesn't have a default constructor so the generation of the default constructor for Biorythm will fail.
In your code you have tried to define the default Biorythm constructor yourself: Biorythm(){}
You have not specified what date constructor to use so it will assume you want to use the default date constructor, which doesn't exist.
What you can do is that you create a default constructor for the date class, or that you specify what date constructor to use like this: Biorythm() : date(1, 2, 3) {}