c++面向对象程序设计 谭浩强 第三章答案
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++面向对象程序设计 谭浩强 第三章答案的更多相关文章
- c++面向对象程序设计 谭浩强 第五章答案
1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>&g ...
- c++面向对象程序设计 谭浩强 第一章答案
c++面向对象程序设计 谭浩强 答案 第一章 目录: c++面向对象程序设计 谭浩强 答案 第一章 c++面向对象程序设计 谭浩强 答案 第二章 c++面向对象程序设计 谭浩强 答案 第三章 c++面 ...
- c++面向对象程序设计 谭浩强 第二章答案
类体内定义成员函数 #include <iostream> using namespace std; class Time { public: void set_time(); void ...
- c++面向对象程序设计 课后题 答案 谭浩强 第四章
c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...
- C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂
C语言程序小练习 1.用C语言设计程序算出1-1/2+1/3-14+1/5...+1/99-1/100的值 #include<stdio.h> int main() { ; double ...
- 关于指针的笔记【1】【C语言程序设计-谭浩强】
指针是什么? 一个 变量的地址称为该变量的"指针"[将地址形象化的称为“指针”].(指针是什么百度百科) 注意区分储存单元的地址和内容这两个概念的区别. 直接访问:直接按变量名进行 ...
- C程序设计(谭浩强)第五版课后题答案 第一章
大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...
- 挂羊头卖狗肉蓄意欺骗读者——谭浩强《C程序设计(第四版)》中所谓的“按照C99”(二)
挂羊头卖狗肉蓄意欺骗读者——谭浩强<C程序设计(第四版)>中所谓的“按照C99”(二) 在<谭C>p4:“本书的叙述以C99标准为依据”,下面从C89到C99的主要变化方面来看 ...
- 再论谭浩强《C语言程序设计》
一些同学学不好C语言,把罪责归于“因为教材是谭浩强写的”实在是很滑稽. 谭浩强老先生 1934 年生,现在已经 80 岁了.他 1958 年从清华大学自动控制系毕业,那年 24 岁.要知道 C 语言那 ...
随机推荐
- Function 构造器及其对象、方法
一.基础 Function 是一个构造器,能创建Function对象,即JavaScript中每个函数实际上都是Function 对象. 构造方法: new Function ([arg1[, ar ...
- Linux od与hexdump命令
od命令:以指定格式输出文件内容常用格式:od -Ax -tx1 filename直接格式:od filename 等价 od -o filename语法:od [-abcdfsiloxv] [-An ...
- Python框架、库和软件资源大全(整理篇)
有少量修改,请访问原始链接.PythonWIn的exe安装包;http://www.lfd.uci.edu/~gohlke/pythonlibs/ 原文链接:codecloud.net/python- ...
- 生物信息之ME, HMM, MEMM, CRF
原文链接:http://bbs.sciencenet.cn/home.php?mod=space&uid=260809&do=blog&id=573755 注:有少量修改!如有 ...
- 实验6 Bezier曲线生成
1.实验目的: 了解曲线的生成原理,掌握几种常见的曲线生成算法,利用VC+OpenGL实现Bezier曲线生成算法. 2.实验内容: (1) 结合示范代码了解曲线生成原理与算法实现,尤其是Bezier ...
- 06--C语言数学函数
在使用C语言数学函数时候,应该在该源文件中使用以下命令行: #include <math.h> 或 #include "math.h",这里的<>跟&quo ...
- http请求后台报406错误
1.springMVC的项目,通过浏览器访问后台方法遇到了报406的错误,找了很多原因,最终发现是因为缺少spring-mvc的json配置. 2.添加依赖:jackson-databind.jack ...
- Java 分布式事务
0 引言 本文主要介绍java中分布式事务以及对应的解决方案. 1 分布式事务产生的原因 1.1 数据库分库分表 当数据库单表一年产生的数据超过1000W,那么就要考虑分库分表,具体分库分表的原理在此 ...
- C++继承与组合
转自https://blog.csdn.net/caoyan_12727/article/details/52337297 类的组合和继承一样,是软件重用的重要方式.组合和继承都是有效地利用已有类的资 ...
- mysql进阶练习
一 . MySQL进阶练习 /*==========================创建班级表=============================*/ CREATE TABLE class ( ...