高分悬赏C++编程问题
发布网友
发布时间:2022-06-05 16:23
我来回答
共1个回答
热心网友
时间:2023-11-25 03:15
看你这道题在这放几天了,给你做了吧,花了我很长时间啊
//sy3_4.cpp
#include<iostream>
using namespace std;
class Date{
private:
int year;
int month;
int day;
public:
Date(int y=1900,int m=1,int d=1);
void setYear(int);
void setMonth(int);
void setDay(int);
void setDate(int yr,int mo,int dy);
int getDay();
int getMonth();
int getYear();
void print();
bool isLeapYear();
int monthDays();
};
Date::Date(int y,int m,int d){
setDate(y,m,d);
}
void Date::setYear(int y){
year=y;
}
void Date::setMonth(int m){
month=m;
}
void Date::setDay(int d){
day=d;
}
void Date::setDate(int yr,int mo,int dy){
int a[12];
a[1-1]=a[3-1]=a[5-1]=a[7-1]=a[8-1]=a[10-1]=a[12-1]=31;
a[4-1]=a[6-1]=a[9-1]=a[10-1]=30;
if((yr%4==0&&yr%100!=0)||(yr%400==0))a[2-1]=29;
else a[2-1]=28;
if(!(mo<=12&&mo>=1)||!(dy>=1&&dy<=a[dy-1])){
cout<<"给定的日期不正确,将以默认值初始化!"<<endl;
setDate(1900,1,1);
return ;
}
year=yr;
month=mo;
day=dy;
}
int Date::getDay(){
return day;
}
int Date::getMonth(){
return month;
}
int Date::getYear(){
return year;
}
void Date::print(){
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
bool Date::isLeapYear(){
if((year%4==0&&year%100!=0)||(year%400==0))return true;
else return false;
}
int Date::monthDays(){
int a[12];
a[1-1]=a[3-1]=a[5-1]=a[7-1]=a[8-1]=a[10-1]=a[12-1]=31;
a[4-1]=a[6-1]=a[9-1]=a[10-1]=30;
if(isLeapYear())a[2-1]=29;
else a[2-1]=28;
return a[month-1];
}
int main(){
Date date;
date.print();
//system("pause");
return 0;
}
******************************************************************
//sy3_5.cpp
#include<iostream>
using namespace std;
class Date{
private:
int year;
int month;
int day;
public:
Date(int y=1900,int m=1,int d=1);
void setYear(int);
void setMonth(int);
void setDay(int);
void setDate(int yr,int mo,int dy);
int getDay();
int getMonth();
int getYear();
void print();
bool isLeapYear();
int monthDays();
void nextday();
};
Date::Date(int y,int m,int d){
setDate(y,m,d);
}
void Date::setYear(int y){
year=y;
}
void Date::setMonth(int m){
month=m;
}
void Date::setDay(int d){
day=d;
}
void Date::setDate(int yr,int mo,int dy){
int a[12];
a[1-1]=a[3-1]=a[5-1]=a[7-1]=a[8-1]=a[10-1]=a[12-1]=31;
a[4-1]=a[6-1]=a[9-1]=a[10-1]=30;
if((yr%4==0&&yr%100!=0)||(yr%400==0))a[2-1]=29;
else a[2-1]=28;
if(!(mo<=12&&mo>=1)||!(dy>=1&&dy<=a[dy-1])){
cout<<"给定的日期不正确,将以默认值初始化!"<<endl;
setDate(1900,1,1);
return ;
}
year=yr;
month=mo;
day=dy;
}
int Date::getDay(){
return day;
}
int Date::getMonth(){
return month;
}
int Date::getYear(){
return year;
}
void Date::print(){
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
bool Date::isLeapYear(){
if((year%4==0&&year%100!=0)||(year%400==0))return true;
else return false;
}
int Date::monthDays(){
int a[12];
a[1-1]=a[3-1]=a[5-1]=a[7-1]=a[8-1]=a[10-1]=a[12-1]=31;
a[4-1]=a[6-1]=a[9-1]=a[10-1]=30;
if(isLeapYear())a[2-1]=29;
else a[2-1]=28;
return a[month-1];
}
void Date::nextday(){
day++;
if(day>monthDays()){
day-=monthDays();
month++;
}
if(month>12){
month-=12;
year++;
}
}
int main(){
int i;
Date date(2010,3,28);
date.print();
for(i=0;i<15;i++){
date.nextday();
date.print();
}
//system("pause");
return 0;
}
*******************************************************
//sy4_2.cpp
#include<iostream>
using namespace std;
class Increment{
public:
Increment(int c=0,int i=1);
void addIncrement(){count+=increment;}
void print()const;
private:
int count;
const int increment;
};
Increment::Increment(int c,int i):increment(i){
count=c;
}
void Increment::print()const{
cout<<"count="<<count<<",increment="<<increment<<endl;
}
int main(){
Increment value(10,5);
cout<<"Before incrementing:";
value.print();
for(int j=1;j<=3;j++){
value.addIncrement();
cout<<"After increment "<<j<<": ";
value.print();
}
//system("pause");
return 0;
}
*********************************************************
我用的Dev C++编的,将上述代码修改后提示信息如下:
F:\编程\04071945.cpp In constructor `Increment::Increment(int, int)':
12 F:\编程\04071945.cpp uninitialized member `Increment::increment' with `const' type `const int'
14 F:\编程\04071945.cpp assignment of read-only data-member `Increment::increment'
因为const成员常量不能被修改,无法使用赋值语句。