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;
};
//构造器
TDate::TDate(int y,int m,int d){
year=y;
month=m;
day=d;
std::cout<<"Constructer called.\n";
};
//析构器
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