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;

}

原文地址:https://www.cnblogs.com/suibian1/p/10987290.html

时间: 2024-08-29 06:26:23

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

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

1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>>num>>name>>sex;} void display( ) {cout<<"num: "<<num<<endl; cout<<"name: "<<name<<end

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

c++面向对象程序设计 谭浩强 答案 第一章 第1章  C++?的初步知识 1.请根据你的了解,叙述C++?的特点.C++?对C有哪些发展? [解] 略. 2.一个C++的程序是由哪几部分构成的?其中的每一部分起什么作用? [解] 略. 3.从拿到一个任务到得到最终结果,一般要经过几个步骤? [解] 略. 4.请说明编辑.编译.连接的作用.在编译后得到的目标文件为什么不能直接运行? [解] 编译是以源程序文件为单位进行的,而一个完整的程序可能包含若干个程序文件,在分别对它们编译之后,得到若干个目

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

第二章 类体内定义成员函数 #include <iostream> using namespace std; class Time { public: void set_time(); void show_time(); private:                    //成员改为公用的 int hour; int minute; int sec; }; void Time::set_time()          //在main函数之前定义 { cin>>hour; ci

【c语言】c程序设计--谭浩强--(第一章)

闲来无事把谭浩强的书里边的题敲了一遍,,纪念一哈,,纯手工 // 在屏幕上输出 This is a c program. #include <stdio.h> int main() { printf("This is a c program.\n"); return 0; } // 求两个整数之和 #include <stdio.h> int main() { int a,b,c; printf("请输入两个整数:\n"); scanf(&q

【c语言】c程序设计--谭浩强--(第二章)--2

// 有两个瓶子a和b,分别盛放醋和酱油,要求将它们互换 #include <stdio.h> int main() { int a,b,c; printf("请输入两个整数:"); scanf("%d%d",&a,&b); c = a; a = b; b = c; printf("交换后的结果是:%d %d\n",a,b); return 0; } // 依次将10个数输入,要求输出其中最大的数 #include &

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

c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> 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;

【c语言】c程序设计--谭浩强--(第三章)--2

// 假如我国国民生产总值的年增长率为9%,计算10年后我国国民生产总值与现在相比增长多少百分比 // p = ( 1 + r )^n r--年增长率 n--年数 p--与现在相比的倍数 #include <stdio.h> #include <math.h> int main() { double r = 0.09; double p; int n = 10; p = pow( ( 1 + r ) , n ); printf("10年后增长了 %f 倍\n",

【c语言】c程序设计--谭浩强--(第四章)--1

// 输入两个实数,按代数值由小到大的顺序输出这两个数 #include <stdio.h> int main() { int a,b; printf("请输入两个数:"); scanf("%d%d",&a,&b); if(a > b) printf("由小到大:%d %d\n",b,a); else printf("由小到大:%d %d\n",a,b); return 0; } // 输入3

【c语言】c程序设计--谭浩强--(第四章)--2

// 从键盘输入一个小于1000的正数,输出它的平方根(若平方根不是整数,则输出它的整数部分) // 要求在输入数据后检查是否为小于1000的正数,若不是则要求重新输入 #include <stdio.h> #include <math.h> int main() { int a; double b; printf("请输入一个小于1000的正数:"); scanf("%d",&a); if( a > 0 &&