C++对象初始化
#ifndef TDATE_H_INCLUDED
#define TDATE_H_INCLUDED#include<iostream>
class TDate{
public:TDate(int y,int m,int d);~TDate();void Print();
private:int year,month,day;
};
//構(gòu)造器
TDate::TDate(int y,int m,int d){year=y;month=m;day=d;std::cout<<"Constructer called.\n";
};
//析構(gòu)器
TDate::~TDate(){std::cout<<"Destructor called.\n";
};
void TDate::Print(){std::cout<<year<<"."<<month<<"."<<day<<std::endl;
}#endif // TDATE_H_INCLUDED
#include<iostream>
#include"tdate.h"
int main(){TDate today(1998,4,9),tomorrow(1998,4,10);today.Print();tomorrow.Print();return 0;
}
Result
總結(jié)
- 上一篇: 转向语句 goto语句
- 下一篇: C++类的静态成员