C++

//Source:<<C++程序设计教程基于案例与实验驱动>>P17
//Person
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
private:
	char *name;
	char gender;
	int height;
	int weight;
public:
	Person();
	void SetName(char *tname);
	char* AnswerName();
	void SetGender(char tgender);
	char AnswerGender();
	void SetHeight(int theight);
	int AnswerHeight();
	void SetWeight(int weight);
	int AnswerWeight();
	void Display();
	void Walk(int distance);
	void Thinking();
};

void Person::SetName(char *tname)
{
	name=new char[strlen(tname)+1];
	strcpy(name,tname);
}

char* Person::AnswerName()
{
	return name;
}

void Person::SetGender(char tgender)
{
	gender=tgender;
}

char Person::AnswerGender()
{
	return gender;
}

void Person::SetHeight(int theight)
{
	height=theight;
}

int Person::AnswerHeight()
{
	return height;
}

void Person::SetWeight(int tweight)
{
	weight=tweight;
}

int Person::AnswerWeight()
{
	return weight;
}

void Person::Display()
{
	cout<<"Name:"<<AnswerName()<<", Gender:"<<AnswerGender()
		<<", Weight:"<<AnswerWeight()<<"kg, Height:"
		<<AnswerHeight()<<"cm."<<endl;
}

void Person::Walk(int distance)
{
	cout<<name<<" walked "<<distance;
	if(distance>1) cout<<" kms."<<endl;
	else cout<<" km."<<endl;
}

void Person::Thinking()
{
	cout<<name<<" is thinking."<<endl;
}


Person::Person()
{
	name=new char[4];//指针必须先给开辟空间
	strcpy(name,"lai");
	gender='m';
	height=170;
	weight=55;
}


int main()
{
	Person z,l,L;
	z.Person::SetName("zhangsan");
	z.Person::SetGender('m');
	z.SetHeight(175);
	z.SetWeight(60);

	l.SetName("lihong");
	l.SetGender('f');
	l.SetHeight(165);
	l.SetWeight(50);


	z.Display();
	z.Walk(1);
	z.Thinking();
	cout<<endl;

	l.Display();
	l.Walk(2);
	l.Thinking();
	cout<<endl;
	
	L.Display();
	L.Walk(3);
	L.Thinking();
	
	return 0;
}


//Source:<<C++程序设计教程基于案例与实验驱动>>P24
//BankAccount
#include<iostream>
using namespace std;
class BankAccount
{
public:
	BankAccount(int dollars,int cents,double rate);
	BankAccount(int dollars,double rate);
	BankAccount();//三个构造函数(无任何返回,同类名,重载)
	void updata();
	double get_balance();
	double get_rate();
	void output();
private:
	double balance;
	double interest_rate;
};

BankAccount::BankAccount(int dollars,int cents,double rate)
{
	if((dollars<0)||(cents<0)||(rate<0))
	{
		cout<<"Illegal values for money or interest rate."<<endl;
		exit(1);//该语句的结果是结束整个程序
	}
	balance=(double)dollars+0.01*(double)cents;
	interest_rate=rate;
}

BankAccount::BankAccount(int dollars,double rate)
{
	if((dollars<0)||(rate<0))
	{
		cout<<"Illegal values for money or interest rate."<<endl;
		exit(1);
	}
	balance=(double)dollars;
	interest_rate=rate;
}

BankAccount::BankAccount()
{
	balance=0.0;
	interest_rate=0.0;
}

void BankAccount::updata()
{
	balance=balance+interest_rate*0.01*balance;
}

double BankAccount::get_balance()
{
	return balance;
}

double BankAccount::get_rate()
{
	return interest_rate;
}

void BankAccount::output()
{
	cout<<"Account balance {1}quot;<<balance<<endl;
	cout<<"Interest rate "<<interest_rate<<"%"<<endl;
}

int main()
{
	BankAccount account1(-100,2.25);
	BankAccount account2;

	cout<<"account1 initialized as follows:"<<endl;
	cout<<"Account balance {1}quot;<<account1.get_balance()<<endl;
	cout<<"Interest rate "<<account1.get_rate()<<"%"<<endl<<endl;

	cout<<"account2 initialized as follows:"<<endl;
	account2.output();
	cout<<endl;

	account1=BankAccount(999,99,2.52);
	cout<<"account1 initialized as follows:"<<endl;
	account1.output();
	account1.updata();
	cout<<endl;

	cout<<"After caculating interest,"<<endl
		<<"acount1 reset to the following:"<<endl;
	account1.output();
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值