| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1502 人关注过本帖
标题:c++类的申请
只看楼主 加入收藏
育帝
Rank: 2
来 自:黑龙江哈尔滨
等 级:论坛游民
帖 子:75
专家分:43
注 册:2016-10-16
结帖率:87.5%
收藏
已结贴  问题点数:20 回复次数:1 
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怎么调用显示构造,还是不需要调用显示构造。
2017-04-17 14:36
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:20 
    ka k(num, password, money);
    p=&k;
    return p;
返回局部临时变量的地址?

程序代码:
#include <iostream>
#include <string>

class card
{
public:
    card( const std::string& num, const std::string& password, int money ) : num_(num), password_(password), money_(money)
    {
        ++g_count_;
    }
    card( const card& c ) : num_(c.num_), password_(c.password_), money_(c.money_)
    {
        ++g_count_;
    }
    ~card()
    {
        --g_count_;
    }
    void save_cash( double cash )
    {
        money_ += cash;
    }
    double cash() const
    {
        return money_;
    }
private:
    std::string num_;
    std::string password_;
    double money_;

public:
    static size_t card_num()
    {
        return g_count_;
    }
private:
    static size_t g_count_;
};
size_t card::g_count_ = 0;

using namespace std;

card card_input( void )
{
    string num;
    string password;
    int money;
    cout << "input card num:";
    cin >> num;
    cout << "input card password:";
    cin >>password;
    cout << "input card cash:";
    cin >> money;

    return card( num, password, money );
}

int main( void )
{
    card c = card_input();
    cout << "remaining cash: " << c.cash() << '\n';

    double cash;
    cin >> cash;
    c.save_cash( cash );
    cout << "remaining cash: " << c.cash() << '\n';

    cin >> cash;
    c.save_cash( cash );
    cout << "remaining cash: " << c.cash() << '\n';

    cout << "Card number: " << card::card_num() << endl;

    return 0;
}

2017-04-17 15:23
快速回复:c++类的申请
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043231 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved