destruct0r

i have applied a cout statement with destructors,the programe is executing excellently but n0t showing the destructor cout statement but the constructor cout statement is showing then y not destructor !!! may be a Logical err0r............... :(


the c0de iz here :


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include<iostream>
#include<stdlib.h>
using namespace std;


class date
{
 public:
         void display();
         void setday(int i);
         void setmonth(int i);
         void setyear(int i);
         int getday();
         int getmonth();
         int getyear();
 //c0nstruct0r
 //we used constructor overloading in tht programe
         date();    //defauLt/simpLe c0nstruct0r:tAkes n0 argument
         date(int); //parametrized:with 1 argument
         date(int,int);//parametrized:with 2 arguments
         date(int,int,int);  //Parametrized c0nstructor: takes 3 argument       
//destruct0r
         ~date();//destructor ~ with cLasss name is kaLLed destruct0r
 private: 
         int Day,Month,Year;
};




//constructor definati0n
date::date()
{
            Day=01;
            Month=4;
            Year=425;
            cout<<"the simpLe/defauLt c0nstruct0r iz kaLLed\n";
}
date::date(int day1)
{
            Day=day1;
            Month=4;
            Year=425;
            cout<<"the pArametrized c0nstruct0r with 1 argument iz kaLLed\n";
}
date::date(int day1,int month1)
{
            Day=day1;
            Month=month1;
            Year=812;
            cout<<"the pArametrized c0nstruct0r with 2 argument iz kaLLed\n";
}
date::date(int day1,int month1,int year1)
{
            Day=day1;
            Month=month1;
            Year=year1;
            cout<<"the pArametrized c0nstruct0r with 3 argument iz kaLLed\n";
//u kan kaLL more arguments but the c0nditi0n iz tht,"the n0 of arguments or"
//the n0 of type of arguments must be different(function ovrLoading k0ncept)            
}



//destructor
date::~date()
{
             cout<<"0bject iz destroyed i.e..the destruct0r iz kaLLed\n";
}




//dispLying funct0nz
void date::display()    //dispLay date
{
     cout<<"daT iz :"<<getday()<<"-"<<getmonth()<<"-"<<getyear()<<endl;
}


//set day,month,year
void date::setday(int i)
{
     Day=i;
}

void date::setmonth(int i)
{
     Month=i;
}

void date::setyear(int i)
{
     Year=i;
}



//getting the vaLues of day,m0nth,Year
int date::getday()
{
    return Day;
}

int date::getmonth()
{
    return Month;
}

int date::getyear()
{
    return Year;
}



//mAin pr0gramme
int main()
{
    //taking objects of cLAss date
    date Date1,Date2(05),Date3(20,06),Date4(25,10,2011);
    //other signs than coma in datee patrren wiLL sh0w err0r
    //date5 wiLL sh0w the year by the c0nstructor we used 2020
    //dispLaY dAte
    Date1.display();
    Date2.display();
    Date3.display();
    Date4.display();
    
    system("pause");
}
Last edited on
Destructor calls would occur after yoursystem("pause") is executed.

Try:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    {   // limit the scope of the date objects.
        date Date1,Date2(05),Date3(20,06),Date4(25,10,2011);
        Date1.display();
        Date2.display();
        Date3.display();
        Date4.display();
    }

    system("pause");
}
gr8!!!it's w0rking bt y these brackets && y not compiler showing error for these xtra brackets...............;) thnxs again!!!!pLz resOLve mY confusion abt tht xtra brackets
For the love of god speak proper English.....
Agreed!
why would you take the time to spell so horribly??

ChEx YurSlF BeFoe Yu ReX YuSelfZ
closed account (zb0S216C)
Grammar Nazi overload! *Violent Explosion*

Wazzak
that i did in hurrry!!!!s0ry for inconvinience
great it's w0rking !!! but y these brackets are used in main programme && y not compiler showing error for these extra brackets && what are the rules to use these brackets............
These "extra" braces are not a syntactical error, they are perfectly legal. The braces denote a block scope. They are used in this instance to show you that your destructors are working correctly.

Also if you want to insure you are seeing your message you should flush your output stream with either flush or endl, just adding a '\n' character doesn't flush the stream.

Last edited on
but y these brackets are used in main programme && y not compiler showing error for these extra brackets && what are the rules to use these brackets............


You can use "extra" brackets pretty much anywhere within a function as long as they match up (well, provided you don't do something silly and try to put one in the middle of a statement or something.) What do they do? They define a scope. In the main I supplied above when the bottom bracket is reached, the date objects go out of scope (and the destructors are called.) If you were to try to access them after the brace that ends the scope they were defined in, you wouldn't be able to because they wouldn't exist any longer.
Topic archived. No new replies allowed.