I can use some enlightment!

Hello everyone,i am reading Strustrups book and i found a spot in a example of his code which i don't fully understant.In this example we have a class Date which saves three variables,date,month,year.We have also a constructor which gives values to these variables by default.I will so you what i mean.

This is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
using namespace std;

class Date
{
        int d,m,y;
    public:
        Date(int dd=0,int mm=0,int yy=0);
        void show();

}today;

Date::Date(int dd,int mm,int yy)
{
    d=dd?dd:today.d;
    m=mm?mm:today.m;
    y=yy?yy:today.y;
}

void Date::show()
{
    cout<<"date:"<<d<<",month :"<<m<<",year:"<<y<<endl;
}


int main()
{
    Date today1;
    Date today2(10,10,1980);

    today1.show();
    today2.show();

    return 0;
}



Well in this point the class cheks if the specific date which i want to pass i acceptable.
1
2
3
4
5
6
Date::Date(int dd,int mm,int yy)
{
   [code] d=dd?dd:today.d;
    m=mm?mm:today.m;
    y=yy?yy:today.y;
}


Actually i don't understant this
1
2
3
 d=dd?dd:today.d;
    m=mm?mm:today.m;
    y=yy?yy:today.y;


It means for example compare the zero value with the given value and if they are not equal return the new value?I am a litle bit confused.Can anyone help me? Thanks anyway.
It says if dd is non-zero, assign the value to d,
otherwise, assign the value of today.d to d.

So the net-effect of that constructor is this:
1. If you enter non-zero values for dd, mm, yy, assign these values to my guts.
2. Otherwise (dd, mm, yy are zero), default my guts to today's ymd.
Last edited on
ok so simple ,thanks !
Topic archived. No new replies allowed.