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. WebForm--j简单控件、简单的登录(怎么链接数据库)

    一.简单控件 1.label:边框(边框的颜色.样式.粗细)  是专门显示文字的,   被编译后是    <span id="Label1">Label</spa ...

  2. Unity3d Time

    using UnityEngine; using System.Collections; public class test1 : MonoBehaviour { public float angle ...

  3. wordcloud + jieba 生成词云

    利用jieba库和wordcloud生成中文词云. jieba库:中文分词第三方库 分词原理: 利用中文词库,确定汉字之间的关联概率,关联概率大的生成词组 三种分词模式: 1.精确模式:把文本精确的切 ...

  4. 重温前端基础之-css浮动与清除浮动

    文档流的概念指什么?有哪种方式可以让元素脱离文档流? 文档流,指的是元素排版布局过程中,元素会自动从左往右,从上往下的流式排列.并最终窗体自上而下分成一行行,并在每行中按从左到右的顺序排放元素.脱离文 ...

  5. 函数的arguments

    1.arguments a.只能在函数内部被访问. b.arguments是一个伪数组(有下标索引,可以存放多个值,但是他里面没有数组的方法.) c.arguments里面存的是什么?函数的实际参数传 ...

  6. JDK和Cglib实现动态代理实例及优缺点分析

    Spring AOP使用的核心技术是动态代理,说到动态代理就不得不和设计模式中的代理模式联系起来,通过代理模式我们可以对目标类进行功能增强,在某个方法的执行前后增加一些操作,例如计算方法执行效率.打印 ...

  7. 6) 十分钟学会android--Activity的生命周期之启动与销毁

    当用户导航.退出和返回您的应用时,应用中的 Activity 实例将在其生命周期中转换不同状态. 例如,当您的Activity初次开始时,它将出现在系统前台并接收用户焦点. 在这个过程中,Androi ...

  8. linq.sort

    reflections.Sort(delegate(ReflectionEntity a, ReflectionEntity b) { if (a.CreatedTime < b.Created ...

  9. 搭建 Lepus 天兔 监控MySQL

    Part1: Lepus安装需要Lamp环境,lepus官网手册也建议采用XAMPP的方式安装,lepus也是在XAMPP上进行研发的 注意xampp会把apache,mysql,php都安装,所以要 ...

  10. 暴雪的hash算法[翻译]

    原文来自:http://sfsrealm.hopto.org/inside_mopaq/chapter2.htm#hashes 促进历史进步的大多数契机都是在解决特定问题的过程中产生的,本文讨论一下M ...