首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于自定义类数组.

2012-03-21 
关于自定义类数组..在线等..#includeiostream#includestring#includevector//#includearrayusingn

关于自定义类数组..在线等..
#include   <iostream>
#include   <string>
#include   <vector>
//#include   <array>
using   namespace   std;

class   Time
{
public:
Time(){};
Time(   int   tyear,int   tmonth,int   tdate){}
int   get_year()   {   return   year;}
int   get_month()   {   return   month;}
int   get_date()   {   return   date;}
void   set_year(int   ayear)   {   year   =   ayear;}
void   set_month(int   amonth)   {   month   =   amonth;}
void   set_date(int   adate)   {   date   =   adate;}  
private:
int   year;
int   month;
int   date;
};                                                              

class   Note
{
public:
Note   (Time   btime,string   bname,   double   ba,double   bb,string   bemoney);
void   set_name(string   aname)   {   name   =   aname;}
  void   set_time(Time   atime)   {   time   =   atime;}
void   set_money(double   amoney)   {   money   =   amoney;}
void   set_bank(double   abank)   {   bank   =   abank;}
Time   get_time()   {   return   time;}
string   get_name()   {   return   name;}
double   get_money()   {   return   money;}
double   get_bank()   {return   bank;}
string   get_emoney()   {return   emoney;}
private:
Time   time;
string   name;
double   money;
double   bank;
string   emoney;
};
Note::Note(Time   btime,string   bname,   double   ba,double   bb,string   bemoney)  
{
time   =   btime;
name   =   bname;
money   =   ba;
bank   =   bb;
emoney   =   bemoney;
}

int   welcome()
{
cout   < <   "welcome! "   < <   endl;
cout   < <   "start   a   new   note,   enter   1   "   < <   endl;
cout   < <   "select   a   note,   enter   2   "   < <   endl;
cout   < <   "sum   the   note,   enter   3   "   < <   endl;
cout   < <   "enter   0   to   exit!   "   < <   endl;
int   a;
cin   > >   a;
return   a;
}

Time   settime(int   year,   int   month,   int   date)
{
Time   time;
time.set_date(date);
time.set_month(month);
time.set_year(year);
return   time;
}
Note   setnote(Time   time,   string   name,   double   money,   double   bank   )
{
Time   kk;
Note   note(kk, "saga ",1,1, "english ");
note.set_bank(bank);
note.set_money(money);
note.set_name(name);
note.set_time(time);
return   note;
}




int   main()
{
int   year,month,date,i=0;
double   money,bank;
string   name;
Time   time[100];
Note   note[100];   //问题出在这里!!!!!!!!!!!!
welcome();
if   (welcome()   ==   1)
{
cout   < <   "year:   "   < <   endl;
cin   > >   year;
cout   < <   "month:   "   < <   endl;
cin   > >   month;
cout   < <   "date:   "   < <   endl;
cin   > >   date;
cout   < <   "name:   "   < <     endl;
                cin   > >   name;
cout   < <   "money:   "   < <     endl;
cin   > >   name;
cout   < <   "bank:   "   < <   endl;
cin   > >   bank;
time[i]   =   settime(year,   month,   date);
note[i]   =   setnote(time[i],   name,   money,   bank);
i++;
}
if   (welcome()   ==   2)
{
cout   < <   "year:   "   < <   endl;
cin   > >   year;
cout   < <   "month:   "   < <   endl;
cin   > >   month;
cout   < <   "date:   "   < <   endl;
cin   > >   date;
for   (int   a=0;   a <=i;   a++)
{
if   (time[a].get_year()   ==   year   &&
time[a].get_month()   ==   month   &&
time[a].get_date()   ==   date)

{
cout   < <   "name:   "   < <   note[a].get_name()   < <   endl;
cout   < <   "money:   "   < <   note[a].get_money()   < <   endl;
cout   < <   "bank:   "     < <   note[a].get_bank()   < <   endl;
}
}
}
return   0;

}

[解决办法]
Note (Time btime,string bname, double ba,double bb,string bemoney);
改成:
Note (Time btime = Time(),string bname = " ", double ba=0.0,double bb=0.0,string bemoney= " ");
[解决办法]
#include <iostream>
#include <string>
#include <vector>
//#include <array>
using namespace std;

class Time
{
public:
Time(){};
Time( int tyear,int tmonth,int tdate){}
int get_year() { return year;}
int get_month() { return month;}
int get_date() { return date;}
void set_year(int ayear) { year = ayear;}
void set_month(int amonth) { month = amonth;}
void set_date(int adate) { date = adate;}
private:
int year;
int month;
int date;
};

class Note
{
public:
Note(){name = " ";money = 0;bank = 0;emoney = " ";};
Note (Time btime,string bname, double ba,double bb,string bemoney);
void set_name(string aname) { name = aname;}


void set_time(Time atime) { time = atime;}
void set_money(double amoney) { money = amoney;}
void set_bank(double abank) { bank = abank;}
Time get_time() { return time;}
string get_name() { return name;}
double get_money() { return money;}
double get_bank() {return bank;}
string get_emoney() {return emoney;}
private:
Time time;
string name;
double money;
double bank;
string emoney;
};
Note::Note(Time btime,string bname, double ba,double bb,string bemoney)
{
time = btime;
name = bname;
money = ba;
bank = bb;
emoney = bemoney;
}

int welcome()
{
cout < < "welcome! " < < endl;
cout < < "start a new note, enter 1 " < < endl;
cout < < "select a note, enter 2 " < < endl;
cout < < "sum the note, enter 3 " < < endl;
cout < < "enter 0 to exit! " < < endl;
int a;
cin > > a;
return a;
}

Time settime(int year, int month, int date)
{
Time time;
time.set_date(date);
time.set_month(month);
time.set_year(year);
return time;
}
Note setnote(Time time, string name, double money, double bank )
{
Time kk;
Note note(kk, "saga ",1,1, "english ");
note.set_bank(bank);
note.set_money(money);
note.set_name(name);
note.set_time(time);
return note;
}


int main()
{
int year,month,date,i=0;
double money,bank;
string name;
Time time[100];
Note note[100]; //问题出在这里!!!!!!!!!!!!
welcome();
if (welcome() == 1)
{
cout < < "year: " < < endl;
cin > > year;
cout < < "month: " < < endl;
cin > > month;
cout < < "date: " < < endl;
cin > > date;
cout < < "name: " < < endl;
cin > > name;
cout < < "money: " < < endl;
cin > > name;
cout < < "bank: " < < endl;
cin > > bank;
time[i] = settime(year, month, date);
note[i] = setnote(time[i], name, money, bank);
i++;
}
if (welcome() == 2)
{
cout < < "year: " < < endl;
cin > > year;
cout < < "month: " < < endl;
cin > > month;
cout < < "date: " < < endl;
cin > > date;
for (int a=0; a <=i; a++)
{
if (time[a].get_year() == year &&
time[a].get_month() == month &&
time[a].get_date() == date)

{
cout < < "name: " < < note[a].get_name() < < endl;
cout < < "money: " < < note[a].get_money() < < endl;
cout < < "bank: " < < note[a].get_bank() < < endl;
}
}
}
return 0;

}
我吧默认构造函数写了,你按自己要求改改数据

热点排行