10.10-1
#include<iostream>
#include<string>
using namespace std;
class user
{
private://数据成员
string name;
string account;//账号
double money;//存款
public://成员函数
user(string x,string y,double z)
{
name=x;
account=y;
money=z;
}
void show();
double deposit(double);
double withdraw(double);
};
void user::show()
{
cout<<"name = "<<name<<",account = "<<account<<",money = "<<money<<endl;
}
double user::deposit(double i)
{
return money+=i;
}
double user::withdraw(double j)
{
return money-=j;
}
int main()
{
user oneInt("Many","123456",1000);
cout<<"Original:\n";
oneInt.show();
double a,b;
cout<<"Enter the deposit:";
cin>>a;
oneInt.deposit(a);
cout<<"Now,the deposit is ";
oneInt.show();
cout<<"Enter the withdraw:";
cin>>b;
oneInt.withdraw(b);
cout<<"Now,the deposit is ";
oneInt.show();
system("pause");
return 0;
}
10.10-2
头文件:person.h
#ifndef PERSON_H
#define PERSON_H
#include<string>
using namespace std;
class Person
{
private:
static const int LTMIT=25;
string lname;
char fname[LTMIT];
public:
Person ()
{
lname="";
fname[0]='\0';
}
Person(const string & ln,const char* fn="Heyyou");
void show() const;
void FormalShow() const;
};
#endif
源文件:
#include<iostream>
#include<string>
#include<cstring>
#include"person.h"
using namespace std;
Person::Person(const string & ln,const char * fn)
{
lname=ln;
strcpy(fname,fn);
}
void Person::show() const
{
cout<<"firstname lastname format : "<<fname<<" "<<lname<<endl;
}
void Person::FormalShow() const
{
cout<<"lastname firstname format : "<<lname<<" "<<fname;
}
int main()
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy","Sam");
cout<<"one:"<<endl;
one.show();
one.FormalShow();
cout<<endl;
cout<<"two:"<<endl;
two.show();
two.FormalShow();
cout<<endl;
cout<<"three:"<<endl;
three.show();
three.FormalShow();
cout<<endl;
system("pause");
return 0;
}
posted on
浙公网安备 33010602011771号