C + + Object-oriented jobs 1

Source: Internet
Author: User

1#include <iostream>2#include <stdlib.h>3 using namespacestd;4 classtdate5 {6  Public:7Tdate (intmm=0,intDd=0,intyy=0);8~tdate ()9     {Tencout<<"Call destructor.\n"; One     } A     voidTdate::setdate (intMintDinty);//the constructor parameter sets the default value -     voidprint () -     { thecout<<"month="<<month<<", day="<<day<<", year="<<year<<"\ n"; -     } - protected: -     intmonth,day,year; + }; -Tdate::tdate (intMmintDdintyy) + { AMonth=mm; atday=DD; -Year=yy; -cout<<"Call constructor.\n"; - } - voidTdate::setdate (intMintDinty) - { in     if(m>0&&m< -) -Month=m; to     if(d>0&&d< +) +day=D; -     if(y>0&&y< the) theYear=y; * } $ intMain ()Panax Notoginseng { -Tdate *pd1,*pd2; thepd1=NewTdate (1,1, -);//to create a dynamic object, the constructor is called +Pd1->print ();//You can only use the pointer to access the members of the object APd2=NewTdate ( A, to, -);//to create a dynamic object, the constructor is called the     if(!PD2)//If allocating heap memory is unsuccessful +     { -cout<<"Heap error!\n";//Output error message $Exit1);//Terminate program run $     } -     Else//If allocating heap memory succeeds -Pd2->setdate (8,8, -);//Call function Reset Date thePd2->print (); -     DeletePD1;//Deleting a Dynamic object invokes a destructorWuyi     DeletePD2;//Deleting a Dynamic object invokes a destructor the}

C + + Object-oriented jobs 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.