C++类的相关基础2

 

//C++类的相关基础2 //baseClassRelInfo2.h #include <iostream> #ifndef Currency_ //可确保Currency的代码仅被程序包含(include)和编译一次 #define Currency_ //C++类的相关基础2 signed plus=0; signed minus=1; //定义 初始化失败异常类 class BadInitializers { public : BadInitializers() {} } ; //定义 抽象类 货币类 class AbsCurrency { public : void Output() const; virtual void Test(){ }; //虚函数 virtual void Test_hsg()=0; //纯虚函数 抽象类 }; AbsCurrency* absCurr; //必须是指针 //定义货币类($) (基类) class BaseCurrency :public AbsCurrency { public : //类的公共成员 BaseCurrency(signed s=plus,unsigned long d=0,unsigned int c=0); ~BaseCurrency(){ } bool Set(signed s,unsigned long d,unsigned int c); bool Set(float a); signed Sign()const //常元函数(不会修改类属性值) { return sgn; } unsigned long Dollars() const { return dollars; } unsigned int Cents() const { return cents; } BaseCurrency Add(const BaseCurrency& x) const; //返回货币值(有复制) BaseCurrency & Increment(const BaseCurrency & x); //返回货币引用 void Output() const; void Output(std::ostream& out) const; protected : //类的保护成员 virtual void Test(); //虚函数 virtual void Test_hsg(); //父类中是纯虚函数 private: //类的私有成员 signed sgn; unsigned long dollars; unsigned int cents; }; //创建类方法: //BaseCurrency f,g(plus,3,45),h(minus,10); //BaseCurrency *m=new BaseCurrency(plus,8,12); //定义类方法:(可以放到Currency.cpp中) BaseCurrency::BaseCurrency(signed s,unsigned long d,unsigned int c) { if(c>99) { std::cout<<"Cents should be<100"<<std::endl; exit(1); } sgn=s;dollars=d;cents=c; } bool BaseCurrency::Set(signed s,unsigned long d,unsigned int c) { if(c>99) return false; sgn=s;dollars=d,cents=c; return true; } bool BaseCurrency::Set(float a) { if(a<0){sgn=minus;a=-a;} else sgn=plus; dollars=a; //抽取整数部分 (美元) cents=(a+0.005-dollars)*100; //获取两位小数位 (美分) return true; } BaseCurrency BaseCurrency::Add (const BaseCurrency& x)const { long a1,a2,a3; BaseCurrency ans; a1=dollars*100+cents; if(sgn==minus) a1=-a1; a2=x.dollars*100+x.cents; if(x.sgn==minus) a2=-a2; a3=a1+a2; if(a3<0){ans.sgn=minus;a3=-a3;} else ans.sgn =plus; ans.dollars=a3/100; ans.cents=a3-ans.dollars*100; return ans; } BaseCurrency& BaseCurrency::Increment(const BaseCurrency& x) { *this=Add(x); return *this; } void BaseCurrency::Output() const { if(sgn==minus) std::cout<<"-"; std::cout<<'</p> <div></div> <p></p><<dollars<<"."; if(cents<10)std::cout<<"0"; std::cout<<cents; } void BaseCurrency::Test() //重载虚函数 { } void BaseCurrency::Test_hsg() //重载纯虚函数 { } //使用不同的描述方法 (派生类) class Currency :public BaseCurrency { public : // 构造函数 Currency(signed s = plus, unsigned long d = 0, unsigned int c = 0); // 析构函数 ~Currency() {} bool Set(signed s, unsigned long d, unsigned int c); bool Set(float a); signed Sign() const { if (amount < 0) return minus; else return plus; } unsigned long Dollars() const { if (amount < 0) return (-amount) / 100; else return amount / 100; } unsigned int Cents() const { if (amount < 0) return -amount - Dollars() * 100; else return amount - Dollars() * 100; } //定义的类方法 Currency Add(const Currency& x) const; Currency& Increment(const Currency& x) { amount += x.amount; return *this;} //操作符重载 Currency operator+(const Currency& x) const; Currency& operator+=(const Currency& x) { amount += x.amount; return *this; } //输出信息方法 (普通方法) void Output() const; void Output(std::ostream& out) const; //输出信息方法2 (友元方法) friend std::ostream& operator<<(std::ostream&,const Currency&); //定义友元函数 protected: virtual void Test(); //虚函数 virtual void Test_hsg(); //父类中是纯虚函数 private : long amount; } ; Currency::Currency(signed s, unsigned long d, unsigned int c) {// 创建Currency 对象 if (c > 99) {// 美分数目过多 std::cout << "Cents should be < 100" << std::endl; throw BadInitializers(); exit(1) ; } amount = d * 100 + c; if (s == minus) amount = -amount; } bool Currency::Set(signed s, unsigned long d,unsigned int c) {// 取值 if (c > 99) return false; amount = d * 100 + c; if (s == minus) amount = -amount; return true; } bool Currency::Set(float a) {// 取值 signed sgn; if (a < 0) {sgn = minus; a = -a;} else sgn = plus; amount = (a + 0.001) * 100; if (sgn == minus) amount = -amount; return true; } Currency Currency::Add(const Currency& x) const {// 把x累加至*this. Currency y; y.amount = amount + x.amount; return y; } void Currency::Output() const { //输出currency 的值 long a = amount; if (a < 0) {std::cout << '-' ; a = -a;} long d = a / 100; // 美元 std::cout << '</p> <div></div> <p></p> << d << '.'; int c = a-d * 100; // 美分 if (c < 10) std::cout << "0"; std::cout << c; } //操作符重载 Currency Currency::operator+(const Currency& x) const {// 把x 累加至*this. Currency y; y.amount = amount + x.amount; return y; } void Currency::Output(std::ostream& out) const {// 将currency 的值插入到输出流 long a = amount; if (a < 0) {out << '-' ; a = -a ; } long d = a / 100; // 美元 out << '</p> <div></div> <p></p> << d << '.' ; int c = a - d * 100; // 美分 if (c < 10) out << "0"; out << c; } ////重载<< (其定义原型: std::ostream& operator<<(std::ostream&,const Currency&);) //std::ostream& operator<<(std::ostream& out, const Currency& x) //{ // x.Output(out); return out; //} // 重载友元函数<< (其定义原型:friend std::ostream& operator<<(std::ostream&,const Currency&);) std::ostream& operator<<(std::ostream& out, const Currency& x) { // 把currency 的值插入到输出流 long a = x.amount; //可访问私有成员 if (a < 0) {out << '-' ; a = -a;} long d = a / 100; // 美元 out<<'</p> <div></div> <p></p> << d << '.' ; int c = a - d * 100; // 美分 if (c < 10) out<<"0"; out<<c; return out; } void Currency::Test() //重载虚函数 { } void Currency::Test_hsg() //重载纯虚函数 { } //Currency f,g(plus,3,45),h(minus,10); //Currency *m=new Currency(plus,8,12); #endif // Win32Console.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "baseCplusplus1.h"; #include "baseClassRelInfo2.h"; void baseCplusplusOp(); //C++函数模板递归一二维数组动态分配存储空间实例1 void baseClassRelInfo2();//C++类的相关基础2 void baseClassRelInfo2_1(); int _tmain(int argc, _TCHAR* argv[]) { baseCplusplusOp(); baseClassRelInfo2(); std::cout<<"/操作符重载"<<std::endl; baseClassRelInfo2_1(); //暂停操作 char str; std::cin>>str; //程序结束 return 0; } //C++类的相关基础2 void baseClassRelInfo2() { Currency g,h(plus,3,50),i,j; g.Set(minus,2,25); i.Set(-6.45); j=h.Add(g); j.Output();std::cout<<std::endl; i.Increment(h); i.Output();std::cout<<std::endl; j=i.Add(g).Add(h); j.Output();std::cout<<std::endl; j=i.Increment(g).Add(h); j.Output();std::cout<<std::endl; i.Output();std::cout<<std::endl; } //C++类的相关基础2 //操作符重载 void baseClassRelInfo2_1() { Currency g, h(plus, 3, 50), i, j; g.Set(minus, 2, 25); i.Set(-6.45); j = h + g; std::cout <<j<<std::endl; i += h; std::cout <<i<<std::endl; j = i + g + h; std::cout <<j<<std::endl; j = (i+=g) + h; std::cout <<j<< std::endl; std::cout <<i<< std::endl; } 

 

转载于:https://www.cnblogs.com/sqlite3/archive/2010/07/06/2566934.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值