主题:怎么让构建的对象不自动析构?
作业题,大致内容是实现普通,专线,双层,郊区4种不同的公交车的信息管理。
我定义了putong,zhuanxian,shuangceng,jiaoqu四个类。
类的声明如下:
#include<iostream>
#include<string>
using namespace std;
static int count=0;
class putong
{
public:
putong()
{
count++;
num=count;
grade=1;
};
~putong(){count--;};
void sethowmany(int sk,int tb)
{
sknum=sk;
tbnum=tb;
};
float getsum()
{
sum=sknum*0.8+tbnum;
return sum;
}
void upgrade(int c){grade+=c;};
void setgrade(int g){grade=g;};
void setname(string n){name=n;};
string getname(){return name;}
int getnum(){return num;};
int getgrade(){return grade;};
protected:
string name;
int grade;
int num;
private:
float sum;
int sknum,tbnum;
};
class zhuanxian
{
public:
zhuanxian()
{
count++;
num=count;
grade=1;
};
~zhuanxian(){count--;};
void sethowmany(int sk,int tb)
{
sknum=sk;
tbnum=tb;
};
float getsum()
{
sum=sknum*1.6+tbnum*2.0;
return sum;
}
void upgrade(int c){grade+=c;};
void setgrade(int g){grade=g;};
void setname(string n){name=n;};
string getname(){return name;}
int getnum(){return num;};
int getgrade(){return grade;};
protected:
string name;
int grade;
int num;
private:
float sum;
int sknum,tbnum;
};
class shuangceng
{
public:
shuangceng()
{
count++;
num=count;
grade=1;
};
~shuangceng(){count--;};
void sethowmany(int sk,int tb)
{
sknum=sk;
tbnum=tb;
};
float getsum()
{
sum=sknum*1.4+tbnum*1.5;
return sum;
}
void upgrade(int c){grade+=c;};
void setgrade(int g){grade=g;};
void setname(string n){name=n;};
string getname(){return name;}
int getnum(){return num;};
int getgrade(){return grade;};
protected:
string name;
int grade;
int num;
private:
float sum;
int sknum,tbnum;
};
class jiaoqu
{
public:
jiaoqu()
{
count++;
num=count;
grade=1;
};
~jiaoqu(){count--;};
void sethowmany(int d){dis=d;};
float getsum()
{
if(dis>=5)
sum=2;
else sum=1;
return sum;
}
void upgrade(int c){grade+=c;};
void setgrade(int g){grade=g;};
void setname(string n){name=n;};
string getname(){return name;}
int getnum(){return num;};
int getgrade(){return grade;};
protected:
string name;
int grade;
int num;
private:
float sum;
int dis;
};
主函数如下:
void main()
{
putong pt[10];
zhuanxian zx[10];
shuangceng sc[10];
jiaoqu jq[10];
string n;
int over;
do
{
static int pts=0,zxs=0,scs=0,jqs=0;
cout<<"1.查找 2.增加 3.删除"<<endl;
int o1;
cin>>o1;
switch(o1)
{
case 1:
{
cout<<"1.普通车"<<endl;
int o2;
cin>>o2;
switch(o2)
{
case 1:
cout<<"1.编号 2.名称"<<endl;
int o3;
cin>>o3;
switch(o3)
{
case 1:
cout<<"请输入编号:"<<endl;
int nn;
cin>>nn;
int i;
for(i=0;i<=pts;i++)
{ if(pt[i].getnum()==nn)
{
cout<<"编号:"<<pt[i].getnum()<<" 名称:"<<pt[i].getname()<<" 级别:"<<pt[i].getgrade()<<" 总额:"<<pt[i].getsum()<<endl;
break;
}
else cout<<"查无此车"<<endl;
break;
}break;
case 2:
cout<<"请输入名称:"<<endl;
string nam;
cin>>nam;
for(i=0;i<=pts;i++)
{ if(pt[i].getname()==nam)
{
cout<<"编号:"<<pt[i].getnum()<<" 名称:"<<pt[i].getname()<<" 级别:"<<pt[i].getgrade()<<" 总额:"<<pt[i].getsum()<<endl;
break;
}
else cout<<"查无此车"<<endl;
break;
}break;
}
}break;
}
case 2:
{
cout<<"1.普通车 "<<endl;
int o2;
cin>>o2;
switch(o2)
{
case 1:
cout<<"输入车名:"<<endl;
cin>>n;
pt[pts].setname(n);
cout<<"输入刷卡总人次:"<<endl;
int sk;
cin>>sk;
cout<<"输入投币总人次:"<<endl;
int tb;
cin>>tb;
pt[pts].sethowmany(sk,tb);
pts++;
break;
}
}
}
cout<<"1.继续 2.结束"<<endl;
cin>>over;
}while(over==1);
}
我这里只做了和普通车有关的。
现在的问题是,比方说我增加了一条普通车的记录,在最后选择继续,再增加一条。然后再查找的时候,找第二条车的记录的结果是查无此车。我感觉是完成第一条记录返回之后,那个对象就自动被析构掉了?是不是这样? 应该怎么解决呢?
我定义了putong,zhuanxian,shuangceng,jiaoqu四个类。
类的声明如下:
#include<iostream>
#include<string>
using namespace std;
static int count=0;
class putong
{
public:
putong()
{
count++;
num=count;
grade=1;
};
~putong(){count--;};
void sethowmany(int sk,int tb)
{
sknum=sk;
tbnum=tb;
};
float getsum()
{
sum=sknum*0.8+tbnum;
return sum;
}
void upgrade(int c){grade+=c;};
void setgrade(int g){grade=g;};
void setname(string n){name=n;};
string getname(){return name;}
int getnum(){return num;};
int getgrade(){return grade;};
protected:
string name;
int grade;
int num;
private:
float sum;
int sknum,tbnum;
};
class zhuanxian
{
public:
zhuanxian()
{
count++;
num=count;
grade=1;
};
~zhuanxian(){count--;};
void sethowmany(int sk,int tb)
{
sknum=sk;
tbnum=tb;
};
float getsum()
{
sum=sknum*1.6+tbnum*2.0;
return sum;
}
void upgrade(int c){grade+=c;};
void setgrade(int g){grade=g;};
void setname(string n){name=n;};
string getname(){return name;}
int getnum(){return num;};
int getgrade(){return grade;};
protected:
string name;
int grade;
int num;
private:
float sum;
int sknum,tbnum;
};
class shuangceng
{
public:
shuangceng()
{
count++;
num=count;
grade=1;
};
~shuangceng(){count--;};
void sethowmany(int sk,int tb)
{
sknum=sk;
tbnum=tb;
};
float getsum()
{
sum=sknum*1.4+tbnum*1.5;
return sum;
}
void upgrade(int c){grade+=c;};
void setgrade(int g){grade=g;};
void setname(string n){name=n;};
string getname(){return name;}
int getnum(){return num;};
int getgrade(){return grade;};
protected:
string name;
int grade;
int num;
private:
float sum;
int sknum,tbnum;
};
class jiaoqu
{
public:
jiaoqu()
{
count++;
num=count;
grade=1;
};
~jiaoqu(){count--;};
void sethowmany(int d){dis=d;};
float getsum()
{
if(dis>=5)
sum=2;
else sum=1;
return sum;
}
void upgrade(int c){grade+=c;};
void setgrade(int g){grade=g;};
void setname(string n){name=n;};
string getname(){return name;}
int getnum(){return num;};
int getgrade(){return grade;};
protected:
string name;
int grade;
int num;
private:
float sum;
int dis;
};
主函数如下:
void main()
{
putong pt[10];
zhuanxian zx[10];
shuangceng sc[10];
jiaoqu jq[10];
string n;
int over;
do
{
static int pts=0,zxs=0,scs=0,jqs=0;
cout<<"1.查找 2.增加 3.删除"<<endl;
int o1;
cin>>o1;
switch(o1)
{
case 1:
{
cout<<"1.普通车"<<endl;
int o2;
cin>>o2;
switch(o2)
{
case 1:
cout<<"1.编号 2.名称"<<endl;
int o3;
cin>>o3;
switch(o3)
{
case 1:
cout<<"请输入编号:"<<endl;
int nn;
cin>>nn;
int i;
for(i=0;i<=pts;i++)
{ if(pt[i].getnum()==nn)
{
cout<<"编号:"<<pt[i].getnum()<<" 名称:"<<pt[i].getname()<<" 级别:"<<pt[i].getgrade()<<" 总额:"<<pt[i].getsum()<<endl;
break;
}
else cout<<"查无此车"<<endl;
break;
}break;
case 2:
cout<<"请输入名称:"<<endl;
string nam;
cin>>nam;
for(i=0;i<=pts;i++)
{ if(pt[i].getname()==nam)
{
cout<<"编号:"<<pt[i].getnum()<<" 名称:"<<pt[i].getname()<<" 级别:"<<pt[i].getgrade()<<" 总额:"<<pt[i].getsum()<<endl;
break;
}
else cout<<"查无此车"<<endl;
break;
}break;
}
}break;
}
case 2:
{
cout<<"1.普通车 "<<endl;
int o2;
cin>>o2;
switch(o2)
{
case 1:
cout<<"输入车名:"<<endl;
cin>>n;
pt[pts].setname(n);
cout<<"输入刷卡总人次:"<<endl;
int sk;
cin>>sk;
cout<<"输入投币总人次:"<<endl;
int tb;
cin>>tb;
pt[pts].sethowmany(sk,tb);
pts++;
break;
}
}
}
cout<<"1.继续 2.结束"<<endl;
cin>>over;
}while(over==1);
}
我这里只做了和普通车有关的。
现在的问题是,比方说我增加了一条普通车的记录,在最后选择继续,再增加一条。然后再查找的时候,找第二条车的记录的结果是查无此车。我感觉是完成第一条记录返回之后,那个对象就自动被析构掉了?是不是这样? 应该怎么解决呢?