首页 > 代码库 > C++课程设计类作业4

C++课程设计类作业4

 1 #include <bits/stdc++.h> 2 using namespace std; 3 class xiexin 4 { 5 public: 6     xiexin() 7     { 8         weight=0; 9         grade=0;10         cout<<"weight="<<weight<<",grade="<<grade<<endl;11     }12     xiexin(int w,int g)13     {14         weight=w;15         grade=g;16         cout<<"weight="<<weight<<",grade="<<grade<<endl;17     }18     ~xiexin()19     {20         cout<<"Destructor......\n";21     }22     void setvalue(int w=100,int g=40)23     {24         weight=w;25         grade=g;26         cout<<"weight="<<weight<<",grade="<<grade<<endl;27     }28     static int calculate(xiexin obj)29     {30         return obj.weight*obj.grade;31     }32     friend void display(xiexin obj);33 private:34     int weight;35     int grade;36     static int cnt;37 };38 int xiexin::cnt=0;39 void display(xiexin obj)40 {41     cout<<"weight="<<obj.weight<<",grade="<<obj.grade<<",total="<<xiexin::calculate(obj)<<endl;42 }43 int main()44 {45     xiexin boxl;46     display(boxl);47     xiexin box2(5,10);48     display(box2);49     box2.setvalue(20,18);50     display(box2);51 }

如果函数中有多条语句的话,会先执行完这些语句,直到函数结束再析构

技术分享

C++课程设计类作业4