c++类的申请
#include<iostream>#include<string>
using namespace std;
class ka
{
string num;
string password;
double money;
static int count;
public:
ka(string num, string password, int money){this->num=num; this->password=password; this->money=money; count++;}
~ka(){count--;}
void xg(double cash){money+=cash;}
void showmoney(){cout<<"余额为:"<<money<<endl;}
static void show(){cout<<"已发卡:"<<count<<endl;}
};
int ka::count=0;
ka *sq()
{
ka *p;
string num, password;
int money;
cout<<"请输入卡号:";
cin>>num;
cout<<"请输入密码:";
cin>>password;
cout<<"请输入金额:";
cin>>money;
ka k(num, password, money);
p=&k;
return p;
}
int main()
{
ka *p;
p=sq();
double cash;
p->showmoney();
cout<<"改变余额:"<<endl;
cin>>cash;
p->xg(cash);
p->showmoney();
cout<<"再次改变余额:"<<endl;
cin>>cash;
p->xg(cash);
p->showmoney();
//p::show();
return 0;
}
执行代码后,改变余额余额没有正确的变化。
还有就是怎么用指针访问静态函数。
如果我用new怎么调用显示构造,还是不需要调用显示构造。