C++

来源:互联网 发布:我的吸血鬼男友网络剧 编辑:程序博客网 时间:2024/06/08 04:21

//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;}






原创粉丝点击