云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 答案-c++面向对象程序设计课后习题答案(谭浩强版)

答案-c++面向对象程序设计课后习题答案(谭浩强版)

  • 62 次阅读
  • 3 次下载
  • 2025/5/1 9:27:17

cout<

int main() {

Time t1(10,13,56); Date d1(12,25,2004); display(d1,t1); return 0; }

11:

#include using namespace std; class Time; class Date {public:

Date(int,int,int); friend Time; private:

int month; int day; int year; };

Date::Date(int m,int d,int y):month(m),day(d),year(y){ }

class Time {public:

Time(int,int,int);

void display(const Date &); private: int hour; int minute; int sec; };

Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){ }

void Time::display(const Date &d) {

cout<

cout<

int main() {

Time t1(10,13,56); Date d1(12,25,2004); t1.display(d1); return 0; }

12:

#include using namespace std; template class Compare {public:

Compare(numtype a,numtype b); numtype max(); numtype min(); private:

numtype x,y; };

template

Compare::Compare(numtype a,numtype b) {x=a;y=b;}

template

numtype Compare::max() {return (x>y)?x:y;}

template

numtype Compare::min() {return (x

int main()

{Compare cmp1(3,7);

cout<

cout< cmp2(45.78,93.6);

cout< cmp3('a','A');

cout<

return 0; }

第四章

1:

#include using namespace std; class Complex {public:

Complex(){real=0;imag=0;}

Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display(); private:

double real; double imag; };

double Complex::get_real() {return real;}

double Complex::get_imag() {return imag;}

void Complex::display()

{cout<<\

Complex operator + (Complex &c1,Complex &c2) {

return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag()); }

int main()

{Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<\ c3.display(); return 0; } 2:

#include

using namespace std; class Complex {public:

Complex(){real=0;imag=0;}

Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2); Complex operator-(Complex &c2); Complex operator*(Complex &c2); Complex operator/(Complex &c2); void display(); private:

double real; double imag; };

Complex Complex::operator+(Complex &c2) {Complex c;

c.real=real+c2.real; c.imag=imag+c2.imag; return c;}

Complex Complex::operator-(Complex &c2) {Complex c;

c.real=real-c2.real; c.imag=imag-c2.imag; return c;}

Complex Complex::operator*(Complex &c2) {Complex c;

c.real=real*c2.real-imag*c2.imag; c.imag=imag*c2.real+real*c2.imag; return c;}

Complex Complex::operator/(Complex &c2) {Complex c;

c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c;}

void Complex::display()

{cout<<\

int main()

{Complex c1(3,4),c2(5,-10),c3;

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

cout<

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com