实验三类与对象

#include<iostream> #include<string.h> #include<iomanip> using namespace std; class Stock{ public: Stock(){count++;} Stock(int a,int b,int c,int d,int e) { this->num=a; this->todaymax=b; this->todaymin=c; this->todaybegin=d; this->todayend=e; count++; } ~Stock() {count--;} void Assign_stack(Stock &c) {this->num=c.num; this->todaymax=c.todaymax; this->todaymin=c.todaymin; this->todaybegin=c.todaybegin; this->todayend=c.todayend; } void display() { cout<<"交易日 "<<this->num<<" "; cout<<"当天最高 "<<this->todaymax<<" "; cout<<"当天最低 "<<this->todaymin<<" "; cout<<"当天开盘 "<<this->todaybegin<<" "; cout<<"当天收盘 "<<this->todayend<<" "<<endl; } void show() { cout<<"Now the sum of Stack is "<<count<<endl;} friend bool cmp(Stock &c); double increase(Stock &c) { double x=c.todayend-todayend; return double(x/c.todayend); } private: static int count; int num,todaymax,todaymin,todaybegin,todayend; }; inline bool cmp(Stock &c) {return c.todaybegin>c.todayend;} int Stock::count=0; int main() { Stock a[5]; Stock *p; Stock b(1,2,3,4,5); //cout<<b.count<<endl; 注意类的私有成员在类外不能访问,,只能通过类的成员函数来访问 p->show(); b.display(); Stock c(2,3,4,5,6); p->show(); Stock d(3,4,5,6,7); Stock e(4,5,6,7,8); p->show(); Stock f(5,6,7,8,9); a[0].Assign_stack(b); p->show(); a[1].Assign_stack(c); a[2].Assign_stack(d); a[3].Assign_stack(e); a[4].Assign_stack(f); if(!cmp(a[0])) cout<<"profit \n"; else cout<<"not profit \n"; for(int i=0;i<5;++i) a[i].display(); Stock *point=a; for(;point!=&a[4];++point) cout<<"今天的涨幅为......"<<setprecision(2)<<point->increase(*(point+1))<<endl; system("pause"); return 0; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值