永发信息网

定义C++一个类的基本形式

答案:4  悬赏:60  手机版
解决时间 2021-03-10 05:09
  • 提问者网友:温旧梦泪无声
  • 2021-03-09 19:18
定义C++一个类的基本形式
最佳答案
  • 五星知识达人网友:往事埋风中
  • 2021-03-09 20:01
class A
{
public://构造析构函数
A() ;
A(...);
A(const A&) ;
virtual~A();

initiA();初始化函数
destroyA();销毁函数
public:
操作接口若干:
虚函数若干:
static 共享接口若干;
private:
数据结构
static 类 类型信息;
static 类 支持结构
static 类 继承结构
friend 友元类
关联 类
耦合 类
};
全部回答
  • 1楼网友:一把行者刀
  • 2021-03-09 23:16
class{ private: public: };
  • 2楼网友:酒者煙囻
  • 2021-03-09 22:00
class 类名{ public: };
  • 3楼网友:低音帝王
  • 2021-03-09 20:46
亲手敲的代码,要加分啊,哪里看不懂问我 #include #include #include using namespace std; class date { public: date();//构造函数 date(const date&);//拷贝构造函数 date strtodate(string);//将string转化为date void setdate(string);//设置日期 void printdate();//打印日期 private: int year; int month; int day; bool pass; bool checkformat(string);//验证日期输入格式 void validate();//验证日期数值有效性 bool leapyear(int);//闰年判断 }; date::date() { year = 2006; month = day = 1; validate(); } date::date(const date& date) { year = date.year; month = date.month; day = date.day; pass = date.pass; } date date::strtodate(string ds) { // yyyy:mm:dd date d; d.setdate(ds); return d; } void date::setdate(string ds) { if( !checkformat(ds) ) { cout << "日期格式错误" << endl; pass = false; return; } validate(); if(pass) { year = atoi(ds.substr(0, 4).c_str()); month = atoi(ds.substr(5, 2).c_str()); day = atoi(ds.substr(8, 2).c_str()); } } void date::printdate() { if (pass) { cout << year << "年" << month << "月" << day << "日" << endl; } else { cout << "日期错误" << endl; } } void date::validate() { pass = false; if (year < 1900 || month < 1 || month > 12 || day < 1 || day > 31) { return; } if (month == 2) { if (leapyear(year)) { if (day <= 29) { pass = true; } return; } else { if (day <= 28) { pass = true; } return; } } else if (month == 4 || month == 6 || month == 9 || month == 11) { if (month <= 30) { pass = true; } return; } else pass = true; } bool date::leapyear(int year) { if (year % 100 == 0) { if (year % 400 == 0) { return true; } } else { if ((year % 4) == 0) { return true; } } return false; } bool date::checkformat(string ds) { //yyyy:mm:dd if (ds.length() != 10 || ds[4] != ':' || ds[7] != ':') { return false; } for (int i = 0; i < (int)ds.length(); i++ ) { if( i != 4 && i != 7) { if (ds[i] < '0' || ds[i] > '9') { return false; } } } return true; } int main() { // 测试是否能正确设置日期 string str; cout << "--test:请输入一个日期:" << endl; cin >> str; date d; d.setdate(str); d.printdate(); //测试拷贝构造函数 cout << "--test:调用拷贝构造函数" << endl; date dc = d; dc.printdate(); //测试字符串到日期转换的函数 cout << "--test:将字符串’2012:03:28‘转化为date类型并输入" << endl; date dt = d.strtodate("2012:03:28"); dt.printdate(); return 0; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯