2:

#include <iostream>

using namespace std;

class Date

{public:

Date(int,int,int);

Date(int,int);

Date(int);

Date();

void display();

private:

int month;

int day;

int year;

};

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

{ }

Date::Date(int m,int d):month(m),day(d)

{year=2005;}

Date::Date(int m):month(m)

{day=1;

year=2005;

}

Date::Date()

{month=1;

day=1;

year=2005;

}

void Date::display()

{cout<<month<<"/"<<day<<"/"<<year<<endl;}

int main()

{

Date d1(10,13,2005);

Date d2(12,30);

Date d3(10);

Date d4;

d1.display();

d2.display();

d3.display();

d4.display();

return 0;

}

3:

#include <iostream>

using namespace std;

class Date

{public:

Date(int=1,int=1,int=2005);

void display();

private:

int month;

int day;

int year;

};

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

{ }

void Date::display()

{cout<<month<<"/"<<day<<"/"<<year<<endl;}

int main()

{

Date d1(10,13,2005);

Date d2(12,30);

Date d3(10);

Date d4;

d1.display();

d2.display();

d3.display();

d4.display();

return 0;

}

4:

#include <iostream>

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void display();

private:

int num;

float score;

};

void Student::display()

{cout<<num<<" "<<score<<endl;}

int main()

{Student stud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5),

Student(104,100.0),Student(105,95.5)};

Student *p=stud;

for(int i=0;i<=2;p=p+2,i++)

p->display();

return 0;

}

5:

#include <iostream>

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

int num;

float score;

};

void main()

{Student stud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5),

Student(104,100.0),Student(105,95.5)};

void max(Student* );

Student *p=&stud[0];

max(p);

}

void max(Student *arr)

{float max_score=arr[0].score;

int k=0;

for(int i=1;i<5;i++)

if(arr[i].score>max_score) {max_score=arr[i].score;k=i;}

cout<<arr[k].num<<" "<<max_score<<endl;

}

c++面向对象程序设计

6:

#include <iostream>

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void change(int n,float s) {num=n;score=s;}

void display(){cout<<num<<" "<<score<<endl;}

private:

int num;

float score;

};

int main()

{Student stud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return 0;

}

c++面向对象程序设计 谭浩强 答案

7: 解法一

#include <iostream>

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void change(int n,float s) {num=n;score=s;}

void display() {cout<<num<<" "<<score<<endl;}

//可改为:void display() const {cout<<num<<" "<<score<<endl;}

private:

int num;

float score;

};

int main()

{const Student

stud(101,78.5);

stud.display();

//stud.change(101,80.5);

stud.display();

return 0;

}

c++面向对象程序设计 谭浩强 答案

解法二:

#include <iostream>

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void change(int n,float s) const  {num=n;score=s;}

void display() const {cout<<num<<" "<<score<<endl;}

private:

mutable int num;

mutable float score;

};

int main()

{const Student stud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return 0;

}

解法三:

#include <iostream>

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void change(int n,float s) {num=n;score=s;}

void display() {cout<<num<<" "<<score<<endl;}

private:

int num;

float score;

};

c++面向对象程序设计 谭浩强 答案

int main()

{Student stud(101,78.5);

Student *p=&stud;

p->display();

p->change(101,80.5);

p->display();

return 0;

}

8:

#include <iostream>

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void change(int n,float s) {num=n;score=s;}

void display() {cout<<num<<" "<<score<<endl;}

private:

int num;

float score;

};

int main()

{Student stud(101,78.5);

void fun(Student&);

fun(stud);

return 0;

}

void fun(Student &stu)

{stu.display();

stu.change(101,80.5);

stu.display();

}

谭浩强

9:

#include <iostream>

using namespace std;

class Product

{public:

Product(int n,int q,float p):num(n),quantity(q),price(p){};

void total();

static float average();

static void display();

private:

int num;

int quantity;

float price;

static float discount;

static float sum;

static int n;

};

void Product::total()

{float rate=1.0;

if(quantity>10) rate=0.98*rate;

sum=sum+quantity*price*rate*(1-discount);

n=n+quantity;

}

void Product::display()

{cout<<sum<<endl;

cout<<average()<<endl;

}

float Product::average()

{return(sum/n);}

float Product::discount=0.05;

float Product::sum=0;

int Product::n=0;

int main()

{

Product Prod[3]={

Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)

};

for(int i=0;i<3;i++)

Prod[i].total();

Product::display();

return 0;

}

c++面向对象程序设计 谭浩强

10:

#include <iostream>

using namespace std;

class Date;

class Time

{public:

Time(int,int,int);

friend void display(const Date &,const Time &);

private:

int hour;

int minute;

int sec;

};

c++面向对象程序设计

Time::Time(int h,int m,int s)

{hour=h;

minute=m;

sec=s;

}

class Date

{public:

Date(int,int,int);

friend void display(const Date &,const Time &);

private:

int month;

int day;

int year;

};

Date::Date(int m,int d,int y)

{month=m;

day=d;

year=y;

}

void display(const Date &d,const Time &t)

{

cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;

cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;

}

int main()

{

Time t1(10,13,56);

Date d1(12,25,2004);

display(d1,t1);

return 0;

}

11:

#include <iostream>

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<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;

cout<<hour<<":"<<minute<<":"<<sec<<endl;

}

int main()

{

Time t1(10,13,56);

Date d1(12,25,2004);

t1.display(d1);

return 0;

}

12:

#include <iostream>

using namespace std;

template<class numtype>

class Compare

{public:

Compare(numtype a,numtype b);

numtype max();

numtype min();

private:

numtype x,y;

};

template <class numtype>

Compare<numtype>::Compare(numtype a,numtype b)

{x=a;y=b;}

template <class numtype>

numtype Compare<numtype>::max()

{return (x>y)?x:y;}

template <class numtype>

numtype Compare<numtype>::min()

{return (x<y)?x:y;}

int main()

{Compare<int> cmp1(3,7);

cout<<cmp1.max()<<" is the Maximum of two integer numbers."<<endl;

cout<<cmp1.min()<<" is the Minimum of two integer numbers."<<endl<<endl;

Compare<float> cmp2(45.78,93.6);

cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;

cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl;

Compare<char> cmp3('a','A');

cout<<cmp3.max()<<" is the Maximum of two characters."<<endl;

cout<<cmp3.min()<<" is the Minimum of two characters."<<endl;

return 0;

}

c++面向对象程序设计 谭浩强 第三章答案的更多相关文章

  1. c++面向对象程序设计 谭浩强 第五章答案

    1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>&g ...

  2. c++面向对象程序设计 谭浩强 第一章答案

    c++面向对象程序设计 谭浩强 答案 第一章 目录: c++面向对象程序设计 谭浩强 答案 第一章 c++面向对象程序设计 谭浩强 答案 第二章 c++面向对象程序设计 谭浩强 答案 第三章 c++面 ...

  3. c++面向对象程序设计 谭浩强 第二章答案

    类体内定义成员函数 #include <iostream> using namespace std; class Time { public: void set_time(); void ...

  4. c++面向对象程序设计 课后题 答案 谭浩强 第四章

    c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...

  5. C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂

    C语言程序小练习 1.用C语言设计程序算出1-1/2+1/3-14+1/5...+1/99-1/100的值 #include<stdio.h> int main() { ; double ...

  6. 关于指针的笔记【1】【C语言程序设计-谭浩强】

    指针是什么? 一个 变量的地址称为该变量的"指针"[将地址形象化的称为“指针”].(指针是什么百度百科) 注意区分储存单元的地址和内容这两个概念的区别. 直接访问:直接按变量名进行 ...

  7. C程序设计(谭浩强)第五版课后题答案 第一章

    大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...

  8. 挂羊头卖狗肉蓄意欺骗读者——谭浩强《C程序设计(第四版)》中所谓的“按照C99”(二)

    挂羊头卖狗肉蓄意欺骗读者——谭浩强<C程序设计(第四版)>中所谓的“按照C99”(二) 在<谭C>p4:“本书的叙述以C99标准为依据”,下面从C89到C99的主要变化方面来看 ...

  9. 再论谭浩强《C语言程序设计》

    一些同学学不好C语言,把罪责归于“因为教材是谭浩强写的”实在是很滑稽. 谭浩强老先生 1934 年生,现在已经 80 岁了.他 1958 年从清华大学自动控制系毕业,那年 24 岁.要知道 C 语言那 ...

随机推荐

  1. 适配器模式(adapter)C++实现

    意图:将一个类的接口转换成客户希望的另一个接口. 适用性:1.你想使用一个已存在的类,而它的接口不符合你的需求. 2.你想创建一个可以复用的类,该类可以与其它不相关的类或不可预见的类协同工作. 类适配 ...

  2. Css问题 margin float 文档流 背景图底部充满

    今天来整理一下做网页遇到的问题吧 1.插入背景图片并使图片居于div底部充满整个行. <style> background:url(xxx.jpg) no-repeat; backgrou ...

  3. 时间&物质&效率

    由于我的家庭是地道的农民家庭,在上学的时候,父母很辛苦的供我读初中,高中,大学. 现在我想说的是,用时间来换取效率是我求学时最大的遗憾. 举一个例子吧:每次回家坐火车,火车很费时间,假如我不缺钱,完全 ...

  4. 常用MySql命令列选

    常用MySql命令列选 命令 参数 含义 alter 数据库,表 修改数据库或表 backup 表 备份表 \c   取消输入 create 数据库,表 创建数据库或表 delete 表和行的表达式 ...

  5. HashMap以及ConcurrentHashMap

    HashMap源码相关 HashMap实现原理及源码分析 总之就是这个博客,简直就是源码带逛,开心,最关键的是下面的图像 另外,自己的理解加上源码,总结如下 hash,原义散列,就是一对一: hash ...

  6. 03--SQLtie三言两语SQLtie链接(join)

    本文将从连接的理论和语法讲起,结合具体的例子,详细分析 SQL 连接. 之前对数据库的连接操作似懂非懂,大概知道是什么东西,但是面试笔试的时候被虐成渣,讲不清连接到底是什么.吃一堑,长一智.这就是所谓 ...

  7. MongoDB 学习笔记(五):固定集合、GridFS文件系统与服务器端脚本

    一.count.distinct与group 1.count函数:查询文档数,如下图: 2.distinct:去重,用法:db.runCommand({distinct:"集合名" ...

  8. Validate US Telephone Numbers FreeCodeCamp

    function telephoneCheck(str) { // 祝你好运 //var re = /^1? ?(\(\d{3}\)|\d{3})[ |-]?\d{3}[ |-]?\d{4}$/; / ...

  9. matlab学习-使用自带的函数

    >> %定义矩阵求最大值>> a=[1 7 3;6 2 9];>> A=max(a);>> a a = 1 7 3 6 2 9 >> A A ...

  10. spring注解@Autowired和@Resource比较

    用途:都是做bean的注入时使用 历史:@Autowired        属于Spring的注解    org.springframework.beans.factory.annotation.Au ...