1.
#include <iostream>
using namespace std;
class usr
{
public:
char usrname[30] = {0};
char password[30] = {0};
long money;
usr()
{
money = 0;
cout << "请输入用户名" << endl;
cin >> usrname;
cout << "请输入密码" << endl;
cin >> password;
}
void add(long mon)
{
money += mon;
}
void rduce(long mon)
{
if (money < mon)
{
cout << "余额不足" << endl;
}
else
{
money -= mon;
}
}
void select()
{
cout << "余额:" << money << endl;
cout << "用户名" << usrname << endl;
cout << "密码" << password << endl;
}
};
int main()
{
usr xiaohong;
xiaohong.add(500);
xiaohong.select();
xiaohong.rduce(200);
xiaohong.select();
return 0;
}
2.
#include <iostream>
using namespace std;
class Test
{
public:
double line=0;
void setValue(double value)
{
line = value;
}
double getArea()
{
double res = line * line;
return res;
}
double getVolume()
{
double res = line * line * line;
return res;
}
void display()
{
cout << "面积:" << getArea() << endl;
cout << "体积" << getVolume() << endl;
}
};
int main()
{
//std::cout << "Hello World!\n";
Test lifang;
lifang.setValue(2);
lifang.display();
return 0;
}