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. 数据结构——栈的实现(数组、Java)

    巩固数据结构 栈是一种有限制的线性表 只能对表尾进行操作 package com.shine.test.datastruct; import java.util.Arrays; public clas ...

  2. [oracle] 递归追溯完整部门名称 函数

    create or replace function fn_DeptWholeName2(objectid in number) return nvarchar2 is wholename nvarc ...

  3. postgreSQL格式化时间的函数详解

    数据类型格式化函数:    PostgreSQL格式化函数提供一套有效的工具用于把各种数据类型(日期/时间.integer.floating point和numeric)转换成格式化的字符串以及反过来 ...

  4. aspnet_regiis加密文件提示路径中具有非法字符

    加密结果受当前目录影响,不知道是不是哪里操作错误,mark.   Web.config文件位置:E:\Web\Surgery 加密成功,上图命令最后有个".",表示web.conf ...

  5. PythonOpenCV--Rtrees随机森林

    360确实很个性,哈哈,你个貔貅,只吃不吐! Rtrees介绍!参考链接:http://docs.opencv.org/modules/ml/doc/random_trees.html 原文链接:Py ...

  6. Kinect安装与配置(openNI2)

    原文链接:http://blog.csdn.net/chenxin_130/article/details/8580636 简介 最近OpenNI2的推出,小斤也要多给博客除除草了,并在闲暇之余做一些 ...

  7. RabbitMQ学习之基于spring-rabbitmq的消息异步发送

    spring-rabbitmq的源码到http://github.com/momania/spring-rabbitmq下载,并可以下载实例代码.由于我使用的rabbitmq版本是3.0.4,部分代码 ...

  8. ES6的let命令

    1.let命令所在的代码块内有效: 2.循环的计数器,就很合适使用let命令:(for循环还有一个特别之处,就是循环语句部分是一个父作用域,而循环体内部是一个单独的子作用域.) 例如:如下会输出十次s ...

  9. day06-08面向对象的三大特性

    一.继承 1.1什么是继承?继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类python中类的继承分为:单继承和多继承 cl ...

  10. 写shell工具类,一个常用实例

    简述: 当我们常用到某些指令时,我们就需要将这个命令进行封装.封装的设计和扩展,因人而异.但为了每个人都能够了解到这个命令,常需要写出这个类的help. 关键字: 函数.getopts 函数 通过自定 ...