#include <iostream.h>
class Date
{
public:
Date(int y);
Date(int y,int m);
Date(int y,int m,int d);
void print();
private:
int year,month,day;
};
//类的实现部分
Date::Date(int y)
{
year=y;month=day=1;
cout<<"1个参数的构造函数已被调用。"<<endl;
}
Date::Date(int y,int m)
{
year=y;
month=m;
day=1;
cout<<"2个参数的构造函数已被调用。"<<endl;
}
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
cout<<"3个参数的构造函数已被调用。"<<endl;
}
void Date::print()
{
cout<<year<<"."<<month<<"."<<day<<endl;
}
void main()
{
Date t1(2005),t2(2005,2),t3(2005,3,2);
t1.print();
t2.print();
t3.print();
}
#include <iostream.h>
class rectangle
{
private:
int length;
int width;
static int extra_data;
public:
rectangle();
void set(int new_length,int new_width);
int get_area();
int get_extra();
};
int rectangle::extra_data=1;
rectangle::rectangle()
{
length=8;
width=8;
}
void rectangle::set(int new_length,int new_width)
{
length=new_length;
width=new_width;
}
int rectangle::get_area()
{
return (length*width);
}
int rectangle::get_extra()
{
return extra_data++;
}
void main()
{
rectangle small,medium,large;
small.set(5,7);
large.set(15,20);
cout<<"Small rectangle area is "<<small.get_area()<<endl;
cout<<"Medium rectangle area is "<<medium.get_area()<<endl;
cout<<"Large rectangle area is "<<large.get_area()<<endl;
cout<<"The static data value is "<<small.get_extra()<<endl;
cout<<"The static data value is "<<medium.get_extra()<<endl;
cout<<"The static data value is "<<large.get_extra()<<endl;
}