面向对象程序设计课后习题答案谭浩强版
C面向对象程序设计课后习题答案第版谭浩强
C面向对象程序设计课后习题答案第版谭浩强文件排版存档编号:[UYTR-OUPT28-KBNTL98-UYNN208]第六章课后习题答案(第二版谭浩强)1://xt6-1/cpp#include <iostream> //如用VC++应改为∶#include <iosttram.h>using namespace std; //如用VC++应取消此行#include "cylinder.h"#include "point.cpp"#include "circle.cpp"#include "cylinder.cpp"int main(){Cylinder cy1(3.5,6.4,5.2,10);cout<<"\noriginal cylinder:\nx="<<cy1.getX()<<",y="<<cy1.getY()<<", r="<<cy1.getRadius()<<",h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<", volume="<<cy1.volume()<<endl;cy1.setHeight(15);cy1.setRadius(7.5);cy1.setPoint(5,5);cout<<"\nnew cylinder:\n"<<cy1;Point &pRef=cy1;cout<<"\npRef as a point:"<<pRef;Circle &cRef=cy1;cout<<"\ncRef as a Circle:"<<cRef;return 0;}3:解法一#include <iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}~Point(){cout<<"executing Point destructor"<<endl;} private:float x;float y;};class Circle:public Point{public:Circle(float a,float b,float r):Point(a,b),radius(r){} ~Circle(){cout<<"executing Circle destructor"<<endl;} private:float radius;};int main(){Point *p=new Circle(2.5,1.8,4.5);delete p;return 0;}3:解法二#include <iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}~Point(){cout<<"executing Point destructor"<<endl;} private:float x;float y;};class Circle:public Point{public:Circle(int a,int b,int r):Point(a,b),radius(r){}~Circle(){cout<<"executing Circle destructor"<<endl;}private:float radius;};int main(){Point *p=new Circle(2.5,1.8,4.5);Circle *pt=new Circle(2.5,1.8,4.5);delete pt;return 0;}3:解法三#include <iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}virtual ~Point(){cout<<"executing Point destructor"<<endl;} private:float x;float y;};class Circle:public Point{public:Circle(float a,float b,float r):Point(a,b),radius(r){}virtual ~Circle(){cout<<"executing Circle destructor"<<endl;} private:float radius;};void main(){Point *p=new Circle(2.5,1.8,4.5);delete p;}4:#include <iostream>using namespace std;//定义抽象基类Shapeclass Shape{public:virtual double area() const =0; //纯虚函数};//定义Circle类class Circle:public Shape{public:Circle(double r):radius(r){} //结构函数virtual double area() const {return 3.14159*radius*radius;}; //定义虚函数protected:double radius; //半径};//定义Rectangle类class Rectangle:public Shape{public:Rectangle(double w,double h):width(w),height(h){} //结构函数virtual double area() const {return width*height;} //定义虚函数protected:double width,height; //宽与高};class Triangle:public Shape{public:Triangle(double w,double h):width(w),height(h){} //结构函数virtual double area() const {return 0.5*width*height;} //定义虚函数protected:double width,height; //宽与高};//输出面积的函数void printArea(const Shape &s){cout<<s.area()<<endl;} //输出s的面积int main(){Circle circle(12.6); //建立Circle类对象circlecout<<"area of circle =";printArea(circle); //输出circle的面积Rectangle rectangle(4.5,8.4); //建立Rectangle类对象rectanglecout<<"area of rectangle ="; printArea(rectangle); //输出rectangle的面积Triangle triangle(4.5,8.4); //建立Triangle类对象cout<<"area of triangle =";printArea(triangle); //输出triangle的面积return 0;}5:#include <iostream>using namespace std;//定义抽象基类Shapeclass Shape{public:virtual double area() const =0; //纯虚函数};//定义Circle(圆形)类class Circle:public Shape{public:Circle(double r):radius(r){} //结构函数virtual double area() const {return 3.14159*radius*radius;}; //定义虚函数protected:double radius; //半径};//定义Square(正方形)类class Square:public Shape{public:Square(double s):side(s){} //结构函数virtual double area() const {return side*side;} //定义虚函数protected:double side;};//定义Rectangle(矩形)类class Rectangle:public Shape{public:Rectangle(double w,double h):width(w),height(h){}//结构函数virtual double area() const {return width*height;}//定义虚函数protected:double width,height;//宽与高};//定义Trapezoid(梯形)类class Trapezoid:public Shape{public:Trapezoid(double t,double b,doubleh):top(t),bottom(t),height(h){} //结构函数virtual double area() const {return 0.5*(top+bottom)*height;} //定义虚函数protected:double top,bottom,height; //上底、下底与高};//定义Triangle(三角形)类class Triangle:public Shape{public:Triangle(double w,double h):width(w),height(h){} //结构函数virtual double area()const {return 0.5*width*height;} //定义虚函数protected:double width,height; //宽与高};int main(){Circle circle(12.6); //建立Circle类对象circleSquare square(3.5); //建立Square类对象squareRectangle rectangle(4.5,8.4); //建立Rectangle类对象rectangleTrapezoid trapezoid(2.0,4.5,3.2); //建立Trapezoid类对象trapezoidTriangle triangle(4.5,8.4); //建立Triangle类对象Shape *pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};//定义基类指针数组pt,使它每一个元素指向一个派生类对象double areas=0.0; //areas为总面积for(int i=0;i<5;i++){areas=areas+pt[i]->area();}cout<<"totol of all areas="<<areas<<endl; //输出总面积return 0;}。
C++面向对象程序设计答案1~8章_谭浩强
第一章11:求2个或3个正整数中的最大数,用带有默认参数的函数实现。
#include <iostream>using namespace std;int main(){int max(int a,int b,int c=0); int a,b,c;cin>>a>>b>>c;cout<<"max(a,b,c)="<<max(a ,b,c)<<endl;cout<<"max(a,b)="<<max(a, b)<<endl;return 0;}int max(int a,int b,int c){if(b>a) a=b;if(c>a) a=c;return a;}13: 对3个变量按由小到大顺序排序,要求使用变量的引用。
#include <iostream>using namespace std;int main(){void sort(int &,int &,int &); int a,b,c,a1,b1,c1;cout<<"Please enter 3 integers:";cin>>a>>b>>c;a1=a;b1=b;c1=c;sort(a1,b1,c1);cout<<a<<" "<<b<<" "<<c<<" in sorted order is "; cout<<a1<<" "<<b1<<" "<<c1<<endl;return 0; }void sort(int &i,int &j,int &k){ void change(int &,int &);if (i>j) change(i,j);if (i>k) change(i,k);if (j>k) change(j,k);}void change(int &x,int &y){ int temp;temp=x;x=y;y=temp;}14: 编一个程序,将两个字符串连接起来,结果取代第一个字符串。
c++面向对象程序的设计课后习题答案(谭浩强版)
第一章5:#include <iostream> using namespace std;int main(){cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."<<endl; return 0;}6:#include <iostream> using namespace std;int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<endl; return 0;}7:#include <iostream> using namespace std; int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl; return 0;}int f(int x,int y,int z) {int m;if (x<y) m=x;if (z<m) m=z;return(m);}8: #include <iostream> using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl; return 0;}9:#include <iostream>using namespace std;int main(){int add(int x,int y);int a,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl; return 0;}int add(int x,int y){int c;c=x+y;return(c);}10:#include <iostream> using namespace std;int main(){void sort(int x,int y,int z); int x,y,z;cin>>x>>y>>z;sort(x,y,z);return 0;}void sort(int x, int y, int z){int temp;if (x>y) {temp=x;x=y;y=temp;} //{ }内3个语句的作用是将x和y的值互换) if (z<x) cout<<z<<','<<x<<','<<y<<endl;else if (z<y) cout<<x<<','<<z<<','<<y<<endl;else cout<<x<<','<<y<<','<<z<<endl;}11:#include <iostream>using namespace std;int main(){int max(int a,int b,int c=0);int a,b,c;cin>>a>>b>>c;cout<<"max(a,b,c)="<<max(a,b,c)<<endl;cout<<"max(a,b)="<<max(a,b)<<endl;return 0;}int max(int a,int b,int c){if(b>a) a=b;if(c>a) a=c;return a;}12:#include <iostream>using namespace std;int main(){void change(int ,int );int a,b;cin>>a>>b;if(a<b) change(a,b);cout<<"max="<<a<<" min="<<b<<endl; return 0;}void change(int ,int ){int r1,r2,temp;temp=r1;r1=r2;r2=temp;}13:#include <iostream>using namespace std;int main(){void sort(int &,int &,int &);int a,b,c,a1,b1,c1;cout<<"Please enter 3 integers:";cin>>a>>b>>c;a1=a;b1=b;c1=c;sort(a1,b1,c1);cout<<a<<" "<<b<<" "<<c<<" in sorted order is "; cout<<a1<<" "<<b1<<" "<<c1<<endl;return 0;}void sort(int &i,int &j,int &k){ void change(int &,int &);if (i>j) change(i,j);if (i>k) change(i,k);if (j>k) change(j,k);}void change(int &x,int &y){ int temp;temp=x;x=y;y=temp;}14:#include <iostream>#include <string>using namespace std;int main(){ string s1="week",s2="end";cout<<"s1="<<s1<<endl;cout<<"s2="<<s2<<endl;s1=s1+s2;cout<<"The new string is:"<<s1<<endl; return 0;}15:#include <iostream>#include <string>using namespace std;int main(){ string str;int i,n;char temp;cout<<"please input a string:";cin>>str;n=str.size();for(i=0;i<n/2;i++){temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;} cout<<str<<endl;return 0;}16:#include <iostream>#include <string>using namespace std;int main(){ int i;string str[5]={"BASIC","C","FORTRAN","C++","PASCAL"}; void sort(string []);sort(str);cout<<"the sorted strings :"<<endl;for(i=0;i<5;i++)cout<<str[i]<<" ";cout<<endl;return 0;}void sort(string s[]){int i,j;string t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (s[i]>s[i+1]){t=s[i];s[i]=s[i+1];s[i+1]=t;}}17: #include <iostream>#include <string>using namespace std;int main(){long c[5]={10100,-123567, 1198783,-165654, 3456}; int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };void sort(int []);void sort(float []);void sort(long []);sort(a);sort(b);sort(c);return 0;}void sort(int a[]){int i,j,t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(long a[]){int i,j;long t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(float a[]){int i,j;float t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}18: #include <iostream>#include <string>using namespace std;template <typename T>void sort(T a[]){int i,j,min;T t;for(i=0;i<5;i++){min=i;for (j=i+1;j<5;j++)if(a[min]>a[j]) min=j;t=a[i]; a[i]=a[min]; a[min]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}int main(){ int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };long c[5]={10100,-123567, 1198783,-165654, 3456}; sort(a);sort(b);sort(c);return 0;}第二章1#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;cin>>minute;cin>>sec;}void Time::show_time() //在main函数之前定义{cout<<hour<<":"<<minute<<":"<<sec<<endl; }int main(){Time t1;t1.set_time();t1.show_time();return 0;}2:#include <iostream>using namespace std;class Time{public:void set_time(void){cin>>hour;cin>>minute;cin>>sec;}void show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}private: int hour;int minute;int sec;};Time t;int main(){t.set_time();t.show_time();return 0;}3:#include <iostream>using namespace std;class Time{public:void set_time(void);void show_time(void);private:int hour;int minute;int sec;};void Time::set_time(void){cin>>hour;cin>>minute;cin>>sec;}void Time::show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}Time t;int main(){ t.set_time();t.show_time();return 0;}4://xt2-4-1.cpp(main.cpp)#include <iostream>using namespace std;#include "xt2-4.h"int main(){Student stud;stud.set_value();stud.display();return 0;}//xt2-4-2.cpp(即student.cpp)#include "xt2-4.h" //在此文件中进行函数的定义#include <iostream>using namespace std; //不要漏写此行void Student::display( ){ cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}void Student::set_value(){ cin>>num;cin>>name;cin>>sex;}5://xt2-5-1.cpp(file1.cpp)#include <iostream>#include "xt2-5.h"int main(){Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return 0;}//xt2-5-2.cpp(arraymax.cpp) #include <iostream> using namespace std;#include "xt2-5.h"void Array_max::set_value() { int i;for (i=0;i<10;i++)cin>>array[i];}void Array_max::max_value() {int i;max=array[0];for (i=1;i<10;i++)if(array[i]>max) max=array[i]; }void Array_max::show_value() {cout<<"max="<<max<<endl; }6:解法一#include <iostream>using namespace std;class Box{public:void get_value();float volume();void display();public:float lengh;float width;float height;};void Box::get_value(){ cout<<"please input lengh, width,height:"; cin>>lengh;cin>>width;cin>>height;}float Box::volume(){ return(lengh*width*height);}void Box::display(){ cout<<volume()<<endl;}int main(){Box box1,box2,box3;box1.get_value();cout<<"volmue of bax1 is "; box1.display();box2.get_value();cout<<"volmue of bax2 is "; box2.display();box3.get_value();cout<<"volmue of bax3 is "; box3.display();return 0;}解法二:#include <iostream>using namespace std;class Box{public:void get_value();void volume();void display();public:float lengh;float width;float height;float vol;};void Box::get_value(){ cout<<"please input lengh, width,height:"; cin>>lengh;cin>>width;cin>>height;}void Box::volume(){ vol=lengh*width*height;}void Box::display(){ cout<<vol<<endl;}int main(){Box box1,box2,box3;box1.get_value();box1.volume();cout<<"volmue of bax1 is ";box1.display();box2.get_value();box2.volume();cout<<"volmue of bax2 is "; box2.display();box3.get_value();box3.volume();cout<<"volmue of bax3 is "; box3.display();return 0;}第三章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;}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;}int num;float score;};int main(){Student stud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return 0;}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;}int num;float score;};int main(){const Studentstud(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) 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;}private:int num;float score;};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;}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;}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;};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;}第四章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;};double Complex::get_real(){return real;}double Complex::get_imag(){return imag;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator + (Complex &c1,Complex &c2){return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag()); }int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c3=";c3.display();return 0;}2:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2);Complex operator-(Complex &c2);Complex operator*(Complex &c2);Complex operator/(Complex &c2);void display();private:double real;double imag;};Complex Complex::operator+(Complex &c2) {Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;}Complex Complex::operator-(Complex &c2) {Complex c;c.real=real-c2.real;c.imag=imag-c2.imag;return c;}Complex Complex::operator*(Complex &c2){Complex c;c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;return c;}Complex Complex::operator/(Complex &c2){Complex c;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c1+c2=";c3.display();c3=c1-c2;cout<<"c1-c2=";c3.display();c3=c1*c2;cout<<"c1*c2=";c3.display();c3=c1/c2;cout<<"c1/c2=";c3.display();return 0;}3:#include <iostream> //用VC++时改为∶#include <iostream.h> using namespace std; //用VC++时为取消此行class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2);Complex operator+(int &i);friend Complex operator+(int&,Complex &);void display();private:double real;double imag;};Complex Complex::operator+(Complex &c) {return Complex(real+c.real,imag+c.imag);}Complex Complex::operator+(int &i) {return Complex(real+i,imag);}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator+(int &i,Complex &c) {return Complex(i+c.real,c.imag);}int main(){Complex c1(3,4),c2(5,-10),c3;int i=5;c3=c1+c2;cout<<"c1+c2=";c3.display();。
【免费下载】c面向对象程序设计课后习题答案谭浩强版
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术关,系电,力根通保据过护生管高产线中工敷资艺设料高技试中术卷资,配料不置试仅技卷可术要以是求解指,决机对吊组电顶在气层进设配行备置继进不电行规保空范护载高与中带资负料荷试下卷高问总中题体资,配料而置试且时卷可,调保需控障要试各在验类最;管大对路限设习度备题内进到来行位确调。保整在机使管组其路高在敷中正设资常过料工程试况中卷下,安与要全过加,度强并工看且作护尽下关可都于能可管地以路缩正高小常中故工资障作料高;试中对卷资于连料继接试电管卷保口破护处坏进理范行高围整中,核资或对料者定试对值卷某,弯些审扁异核度常与固高校定中对盒资图位料纸置试,.卷保编工护写况层复进防杂行腐设自跨备动接与处地装理线置,弯高尤曲中其半资要径料避标试免高卷错等调误,试高要方中求案资技,料术编试交写5、卷底重电保。要气护管设设装线备备置敷4高、调动设中电试作技资气高,术料课中并3中试、件资且包卷管中料拒含试路调试绝线验敷试卷动槽方设技作、案技术,管以术来架及避等系免多统不项启必方动要式方高,案中为;资解对料决整试高套卷中启突语动然文过停电程机气中。课高因件中此中资,管料电壁试力薄卷高、电中接气资口设料不备试严进卷等行保问调护题试装,工置合作调理并试利且技用进术管行,线过要敷关求设运电技行力术高保。中护线资装缆料置敷试做设卷到原技准则术确:指灵在导活分。。线对对盒于于处调差,试动当过保不程护同中装电高置压中高回资中路料资交试料叉卷试时技卷,术调应问试采题技用,术金作是属为指隔调发板试电进人机行员一隔,变开需压处要器理在组;事在同前发一掌生线握内槽图部内 纸故,资障强料时电、,回设需路备要须制进同造行时厂外切家部断出电习具源题高高电中中源资资,料料线试试缆卷卷敷试切设验除完报从毕告而,与采要相用进关高行技中检术资查资料和料试检,卷测并主处且要理了保。解护现装场置设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。
C++面向对象程序设计(谭浩强)课后习题答案text-P67
班级:B11512 姓名:陈德桥学号:30 1.以下是根据题意订正后的程序和运行结果:#include<iostream>using namespace std;class Time{public:void set_time(void);void show_time(void);int hour;int minute;int sec;};Time t;int main(){t.set_time();t.show_time();return 0;}void Time::set_time(void){cout<<"set_time:"<<endl;cin>>t.hour;cin>>t.minute;cin>>t.sec;}void Time::show_time(void){cout<<"show_time:"<<endl;cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}2.以下是根据题目要求对例2.1进行改写后的程序和运行结果:#include<iostream>using namespace std;class Timeprivate:int hour;int minute;int second;public:void settime(int h,int m,int s){tl.hour=h,tl.minute=m,tl.second=s;}void showtime(){cout<<"showtime:"<<endl;cout<<tl.hour<<":"<<tl.minute<<":"<<tl.second<<endl;}}tl;int main(){tl.settime(22,24,0);tl.showtime();return 0;}3. .以下是根据题目要求对例2.1进行改写后的程序和运行结果:#include<iostream>using namespace std;class Time{private:int hour;int minute;int second;public:void settime(int h,int m,int s);void showtime();}tl;int main(){tl.settime(22,24,0);tl.showtime();return 0;void Time::settime(int h,int m,int s){tl.hour=h,tl.minute=m,tl.second=s;}void Time::showtime(){cout<<"showtime:"<<endl;cout<<tl.hour<<":"<<tl.minute<<":"<<tl.second<<endl;}4.以下根据要求对本章2、3、3进行完善后的程序和运行结果:#include<iostream>#include<string>using namespace std;class Student{public:void display(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}void set_value1(int n);void set_value2(string m);void set_value3(char s);private:int num;string name;char sex;}Stu;int main(){Stu.set_value1(12);Stu.set_value2("liu");Stu.set_value3('F');Stu.display();return 0;}void Student::set_value1(int n)Stu.num=n;}void Student::set_value2(string m) {=m;}void Student::set_value3(char s) {Stu.sex=s;}。
(完整版)C++面向对象程序设计(谭浩强)课后习题答案text-P67
班级:B11512 姓名:陈德桥学号:30 1.以下是根据题意订正后的程序和运行结果:#include<iostream>using namespace std;class Time{public:void set_time(void);void show_time(void);int hour;int minute;int sec;};Time t;int main(){t.set_time();t.show_time();return 0;}void Time::set_time(void){cout<<"set_time:"<<endl;cin>>t.hour;cin>>t.minute;cin>>t.sec;}void Time::show_time(void){cout<<"show_time:"<<endl;cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}2.以下是根据题目要求对例2.1进行改写后的程序和运行结果:#include<iostream>using namespace std;class Timeprivate:int hour;int minute;int second;public:void settime(int h,int m,int s){tl.hour=h,tl.minute=m,tl.second=s;}void showtime(){cout<<"showtime:"<<endl;cout<<tl.hour<<":"<<tl.minute<<":"<<tl.second<<endl;}}tl;int main(){tl.settime(22,24,0);tl.showtime();return 0;}3. .以下是根据题目要求对例2.1进行改写后的程序和运行结果:#include<iostream>using namespace std;class Time{private:int hour;int minute;int second;public:void settime(int h,int m,int s);void showtime();}tl;int main(){tl.settime(22,24,0);tl.showtime();return 0;void Time::settime(int h,int m,int s){tl.hour=h,tl.minute=m,tl.second=s;}void Time::showtime(){cout<<"showtime:"<<endl;cout<<tl.hour<<":"<<tl.minute<<":"<<tl.second<<endl;}4.以下根据要求对本章2、3、3进行完善后的程序和运行结果:#include<iostream>#include<string>using namespace std;class Student{public:void display(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}void set_value1(int n);void set_value2(string m);void set_value3(char s);private:int num;string name;char sex;}Stu;int main(){Stu.set_value1(12);Stu.set_value2("liu");Stu.set_value3('F');Stu.display();return 0;}void Student::set_value1(int n)Stu.num=n;}void Student::set_value2(string m) {=m;}void Student::set_value3(char s) {Stu.sex=s;}。
C++面向对象程序设计答案1~8章_谭浩强
}
void change(int ,int )
{
int r1,r2,temp;
temp=r1;
r1=r2;
r2=temp;
}
13:
#include <iostream>
using namespace std;
int main()
{void sort(int &,int &,int &);
}
int main()
{Time t1;
t1.set_time();
t1.show_time();
return 0;
}
2:
#include <iostream>
using namespace std;
class Time
{public:
void set_time(void)
{cin>>hour;
cin>>minute;
int sec;
};
void Time::set_time() //在main函数之前定义
{
cin>>hour;
cin>>minute;
cin>>sec;
}
void Time::show_time() //在main函数之前定义
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
cout<<endl<<endl;
}
void sort(float a[])
答案-c++面向对象程序设计课后习题答案(谭浩强版)
第一章5:#include <iostream> using namespace std;int main(){cout<<"This"<<"is"; cout<<"a"<<"C++";cout<<"program."<<endl; return 0;}6:#include <iostream> using namespace std;int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return 0;}7:#include <iostream> using namespace std;int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return 0;}int f(int x,int y,int z){int m;else m=y;if (z<m) m=z;return(m);}8: #include <iostream> using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl; return 0;}9:#include <iostream>using namespace std;int main(){int add(int x,int y);int a,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl; return 0;}int add(int x,int y){int c;c=x+y;return(c);}10:#include <iostream>using namespace std;int main(){void sort(int x,int y,int z); int x,y,z;cin>>x>>y>>z;return 0;}void sort(int x, int y, int z){int temp;if (x>y) {temp=x;x=y;y=temp;} //{ }内3个语句的作用是将x和y的值互换) if (z<x) cout<<z<<','<<x<<','<<y<<endl;else if (z<y) cout<<x<<','<<z<<','<<y<<endl;else cout<<x<<','<<y<<','<<z<<endl;}11:#include <iostream>using namespace std;int main(){int max(int a,int b,int c=0);int a,b,c;cin>>a>>b>>c;cout<<"max(a,b,c)="<<max(a,b,c)<<endl;cout<<"max(a,b)="<<max(a,b)<<endl;return 0;}int max(int a,int b,int c){if(b>a) a=b;if(c>a) a=c;return a;}12:#include <iostream>using namespace std;int main(){void change(int ,int );int a,b;cin>>a>>b;if(a<b) change(a,b);cout<<"max="<<a<<" min="<<b<<endl;return 0;}void change(int ,int ){int r1,r2,temp;temp=r1;r1=r2;r2=temp;}13:#include <iostream>using namespace std;int main(){void sort(int &,int &,int &);int a,b,c,a1,b1,c1;cout<<"Please enter 3 integers:";cin>>a>>b>>c;a1=a;b1=b;c1=c;sort(a1,b1,c1);cout<<a<<" "<<b<<" "<<c<<" in sorted order is "; cout<<a1<<" "<<b1<<" "<<c1<<endl;return 0;}void sort(int &i,int &j,int &k){ void change(int &,int &);if (i>j) change(i,j);if (i>k) change(i,k);if (j>k) change(j,k);}void change(int &x,int &y){ int temp;temp=x;x=y;y=temp;}14:#include <iostream>#include <string>using namespace std;int main(){ string s1="week",s2="end";cout<<"s1="<<s1<<endl;cout<<"s2="<<s2<<endl;s1=s1+s2;cout<<"The new string is:"<<s1<<endl;return 0;}15:#include <iostream>#include <string>using namespace std;int main(){ string str;int i,n;char temp;cout<<"please input a string:";cin>>str;n=str.size();for(i=0;i<n/2;i++){temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;}cout<<str<<endl;return 0;}16:#include <iostream>#include <string>using namespace std;int main(){ int i;string str[5]={"BASIC","C","FORTRAN","C++","PASCAL"}; void sort(string []);sort(str);cout<<"the sorted strings :"<<endl;for(i=0;i<5;i++)cout<<str[i]<<" ";cout<<endl;return 0;}void sort(string s[]){int i,j;string t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (s[i]>s[i+1]){t=s[i];s[i]=s[i+1];s[i+1]=t;}}17: #include <iostream>#include <string>using namespace std;int main(){long c[5]={10100,-123567, 1198783,-165654, 3456}; int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };void sort(int []);void sort(float []);void sort(long []);sort(a);sort(b);sort(c);return 0;}void sort(int a[]){int i,j,t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(long a[]){int i,j;long t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(float a[]){int i,j;float t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}18: #include <iostream>#include <string>using namespace std;template <typename T>void sort(T a[]){int i,j,min;T t;for(i=0;i<5;i++){min=i;for (j=i+1;j<5;j++)if(a[min]>a[j]) min=j;t=a[i]; a[i]=a[min]; a[min]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}int main(){ int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };long c[5]={10100,-123567, 1198783,-165654, 3456}; sort(a);sort(b);sort(c);return 0;}第二章1#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;cin>>minute;cin>>sec;}void Time::show_time() //在main函数之前定义{cout<<hour<<":"<<minute<<":"<<sec<<endl;}int main(){Time t1;t1.set_time();t1.show_time();return 0;}2:#include <iostream>using namespace std;class Time{public:void set_time(void){cin>>hour;cin>>minute;cin>>sec;void show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}private: int hour;int minute;int sec;};Time t;int main(){t.set_time();t.show_time();return 0;}3:#include <iostream>using namespace std;class Time{public:void set_time(void);void show_time(void);private:int hour;int minute;int sec;};void Time::set_time(void){cin>>hour;cin>>minute;cin>>sec;}void Time::show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}Time t;int main(){ t.set_time();t.show_time();return 0;}//xt2-4-1.cpp(main.cpp)#include <iostream>using namespace std;#include "xt2-4.h"int main(){Student stud;stud.set_value();stud.display();return 0;}//xt2-4-2.cpp(即student.cpp)#include "xt2-4.h" //在此文件中进展函数的定义#include <iostream>using namespace std; //不要漏写此行void Student::display( ){ cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}void Student::set_value(){ cin>>num;cin>>name;cin>>sex;}5://xt2-5-1.cpp()#include <iostream>#include "xt2-5.h"int main(){Array_max arrmax;arrmax.set_value();arrmax.max_value();arrmax.show_value();return 0;}//xt2-5-2.cpp(arraymax.cpp)#include <iostream>using namespace std;#include "xt2-5.h"void Array_max::set_value()for (i=0;i<10;i++)cin>>array[i];}void Array_max::max_value(){int i;max=array[0];for (i=1;i<10;i++)if(array[i]>max) max=array[i];}void Array_max::show_value(){cout<<"max="<<max<<endl;}6:解法一#include <iostream>using namespace std;class Box{public:void get_value();float volume();void display();public:float lengh;float width;float height;};void Box::get_value(){ cout<<"please input lengh, width,height:"; cin>>lengh;cin>>width;cin>>height;}float Box::volume(){ return(lengh*width*height);}void Box::display(){ cout<<volume()<<endl;}{Box box1,box2,box3;box1.get_value();cout<<"volmue of bax1 is ";box1.display();box2.get_value();cout<<"volmue of bax2 is ";box2.display();box3.get_value();cout<<"volmue of bax3 is ";box3.display();return 0;}解法二:#include <iostream>using namespace std;class Box{public:void get_value();void volume();void display();public:float lengh;float width;float height;float vol;};void Box::get_value(){ cout<<"please input lengh, width,height:"; cin>>lengh;cin>>width;cin>>height;}void Box::volume(){ vol=lengh*width*height;}void Box::display(){ cout<<vol<<endl;}int main(){Box box1,box2,box3;box1.get_value();box1.volume();cout<<"volmue of bax1 is ";box1.display();box2.get_value();box2.volume();cout<<"volmue of bax2 is ";box2.display();box3.get_value();box3.volume();cout<<"volmue of bax3 is ";box3.display();return 0;}第三章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;}{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 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;}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;}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 Studentstud(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) 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;};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;}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;};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;}第四章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;};double Complex::get_real(){return real;}double Complex::get_imag(){return imag;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator + (Complex &c1,Complex &c2){return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag()); }int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c3=";c3.display();return 0;}2:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2);Complex operator-(Complex &c2);Complex operator*(Complex &c2);Complex operator/(Complex &c2);void display();private:double real;double imag;};Complex Complex::operator+(Complex &c2){Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;}Complex Complex::operator-(Complex &c2){Complex c;c.real=real-c2.real;c.imag=imag-c2.imag;return c;}Complex Complex::operator*(Complex &c2){Complex c;c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;return c;}Complex Complex::operator/(Complex &c2){Complex c;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}int main(){Complex c1(3,4),c2(5,-10),c3;cout<<"c1+c2=";c3.display();c3=c1-c2;cout<<"c1-c2=";c3.display();c3=c1*c2;cout<<"c1*c2=";c3.display();c3=c1/c2;cout<<"c1/c2=";c3.display();return 0;}3:#include <iostream> //用VC++时改为∶#include <iostream.h> using namespace std; //用VC++时为取消此行class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2);Complex operator+(int &i);friend Complex operator+(int&,Complex &);void display();private:double real;double imag;};Complex Complex::operator+(Complex &c){return Complex(real+c.real,imag+c.imag);}Complex Complex::operator+(int &i){return Complex(real+i,imag);}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator+(int &i,Complex &c){return Complex(i+c.real,c.imag);}int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c1+c2=";c3.display();c3=i+c1;cout<<"i+c1=";c3.display();c3=c1+i;cout<<"c1+i=";c3.display();return 0;}4:#include <iostream>using namespace std;class Matrix //定义Matrix类{public:Matrix(); //默认构造函数friend Matrix operator+(Matrix &,Matrix &); //重载运算符“+〞void input(); //输入数据函数void display(); //输出数据函数private:int mat[2][3];};Matrix::Matrix() //定义构造函数{for(int i=0;i<2;i++)for(int j=0;j<3;j++)mat[i][j]=0;}Matrix operator+(Matrix &a,Matrix &b) //定义重载运算符“+〞函数{Matrix c;for(int i=0;i<2;i++)for(int j=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}return c;}void Matrix::input() //定义输入数据函数{cout<<"input value of matrix:"<<endl;for(int i=0;i<2;i++)for(int j=0;j<3;j++)cin>>mat[i][j];}void Matrix::display() //定义输出数据函数{for (int i=0;i<2;i++){for(int j=0;j<3;j++){cout<<mat[i][j]<<" ";}cout<<endl;}}int main(){Matrix a,b,c;a.input();b.input();cout<<endl<<"Matrix a:"<<endl;a.display();cout<<endl<<"Matrix b:"<<endl;b.display();c=a+b; //用重载运算符“+〞实现两个矩阵相加cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;c.display();return 0;}5:#include <iostream.h>//using namespace std;class Matrix{public:Matrix();friend Matrix operator+(Matrix &,Matrix &);friend ostream& operator<<(ostream&,Matrix&);friend istream& operator>>(istream&,Matrix&);private:int mat[2][3];};Matrix::Matrix(){for(int i=0;i<2;i++)for(int j=0;j<3;j++)mat[i][j]=0;}Matrix operator+(Matrix &a,Matrix &b){Matrix c;for(int i=0;i<2;i++)for(int j=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}return c;}istream& operator>>(istream &in,Matrix &m){cout<<"input value of matrix:"<<endl;for(int i=0;i<2;i++)for(int j=0;j<3;j++)in>>m.mat[i][j];return in;}ostream& operator<<(ostream &out,Matrix &m){for (int i=0;i<2;i++){for(int j=0;j<3;j++){out<<m.mat[i][j]<<" ";}out<<endl;}return out;}int main(){ Matrix a,b,c;cin>>a;cin>>b;cout<<endl<<"Matrix a:"<<endl<<a<<endl;cout<<endl<<"Matrix b:"<<endl<<b<<endl;c=a+b;cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl<<c<<endl; return 0;}6:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r){real=r;imag=0;}Complex(double r,double i){real=r;imag=i;}operator double(){return real;}void display();private:double real;double imag;};void Complex::display(){cout<<"("<<real<<", "<<imag<<")"<<endl;}int main(){Complex c1(3,4),c2;double d1;d1=2.5+c1;cout<<"d1="<<d1<<endl;c2=Complex(d1);cout<<"c2=";c2.display();return 0;}7:#include <iostream>using namespace std;class Student{public:Student(int,char[],char,float);int get_num(){return num;}char * get_name(){return name;}char get_sex(){return sex;}void display(){cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\nscore:"<<score<<"\n\n";} private:int num;char name[20];char sex;float score;};Student::Student(int n,char nam[],char s,float so){num=n;strcpy(name,nam);sex=s;score=so;}class Teacher{public:Teacher(){}Teacher(Student&);Teacher(int n,char nam[],char sex,float pay);void display();private:int num;char name[20];char sex;float pay;};Teacher::Teacher(int n,char nam[],char s,float p){num=n;strcpy(name,nam);sex=s;pay=p;}Teacher::Teacher(Student& stud){num=stud.get_num();strcpy(name,stud.get_name());sex=stud.get_sex();pay=1500;}void Teacher::display(){cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\npay:"<<pay<<"\n\n";}int main(){Teacher teacher1(10001,"Li",'f',1234.5),teacher2;Student student1(20010,"Wang",'m',89.5);cout<<"student1:"<<endl;student1.display();teacher2=Teacher(student1);cout<<"teacher2:"<<endl;teacher2.display();return 0;}第五章1:#include <iostream>using namespace std;class Student。
谭浩强C++面向对象程序设计第一章习题解答
谭浩强C++面向对象程序设计第一章习题解答(共8页)--本页仅作为文档封面,使用时请直接删除即可----内页可以根据需求调整合适字体及大小--using namespace std;int main(){cout<<"This"<<"is";cout<<"a"<<"C++";cout<<"program."<<endl; return 0;}2. #include <iostream> using namespace std;int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return 0;}3. #include <iostream> using namespace std;int main(){int a,b,c;int f(int x,int y,int z);cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return 0;}int f(int x,int y,int z){int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m);}using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl;return 0;}5. #include <iostream>using namespace std;int main(){int add(int x,int y);int a,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl;return 0;}int add(int x,int y){int z;z=x+y;return(z);}6. #include <iostream>using namespace std;int main(){void sort(int x,int y,int z);int x,y,z;cin>>x>>y>>z;sort(x,y,z);return 0;}void sort(int x, int y, int z){int temp;if (x>y) {temp=x;x=y;y=temp;} include <iostream> using namespace std;int main(){int max(int a,int b,int c=0);cin>>a>>b>>c;cout<<"max(a,b,c)="<<max(a,b,c)<<endl;cout<<"max(a,b)="<<max(a,b)<<endl;return 0;}int max(int a,int b,int c){if(b>a) a=b;if(c>a) a=c;return a;}8. #include <iostream>using namespace std;int main(){void change(int &,int &);int a,b;cin>>a>>b;if(a<b) change(a,b);cout<<"max="<<a<<" min="<<b<<endl;return 0;}void change(int &r1,int &r2){int temp;temp=r1;r1=r2;r2=temp;cout<<r1<<" ddd "<<r2<<endl;}9. #include <iostream>using namespace std;int main(){void sort(int &,int &,int &);int a,b,c,a1,b1,c1;cout<<"Please enter 3 integers:";cin>>a>>b>>c;a1=a;b1=b;c1=c;sort(a1,b1,c1);cout<<a<<" "<<b<<" "<<c<<" in sorted order is "; cout<<a1<<" "<<b1<<" "<<c1<<endl;}void sort(int &i,int &j,int &k){ void change(int &,int &);if (i>j) change(i,j);if (i>k) change(i,k);if (j>k) change(j,k);}void change(int &x,int &y){ int temp;temp=x;x=y;y=temp;}10. #include <iostream>#include <string>using namespace std;int main(){ string s1="week",s2="end";cout<<"s1="<<s1<<endl;cout<<"s2="<<s2<<endl;s1=s1+s2;cout<<"The new string is:"<<s1<<endl;.return 0;}11. #include <iostream>#include <string>using namespace std;int main(){ string str;int i,n;char temp;cout<<"please input a string:";cin>>str;n=();for(i=0;i<n/2;i++){temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;} cout<<str<<endl;return 0;}12. #include <iostream>#include <string>using namespace std;int main(){ int i;string str[5]={"BASIC","C","FORTRAN","C++","PASCAL"}; void sort(string []);sort(str);cout<<"the sorted strings :"<<endl;for(i=0;i<5;i++)cout<<str[i]<<" ";cout<<endl;return 0;}void sort(string s[]){int i,j;string t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (s[i]>s[i+1]){t=s[i];s[i]=s[i+1];s[i+1]=t;}}13. #include <iostream>#include <string>using namespace std;int main(){long c[5]={10100,-123567, 1198783,-165654, 3456}; int a[5]={1,9,0,23,-45};float b[5]={, , , , };void sort(int []);void sort(float []);void sort(long []);sort(a);sort(b);sort(c);return 0;}void sort(int a[]){int i,j,t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(long a[]){int i,j;long t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(float a[]){int i,j;float t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}14. #include <iostream>#include <string>using namespace std;int main(){ int a[5]={1,9,0,23,-45};float b[5]={, , , , };long int c[5]={10100,-123567, 1198783,-165654, 3456}; void sort(int []);void sort(float []);void sort(long []);sort(a);sort(b);sort(c);return 0;}void sort(int a[]){int i,j,t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(float a[]){int i,j;float t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(long a[]){int i,j,min;long t;for(i=0;i<5;i++){min=i;for (j=i+1;j<5;j++)if(a[min]>a[j]) min=j;t=a[i]; a[i]=a[min];a[min]=t; }cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}15. #include <iostream>#include <string>using namespace std;template <typename T>void sort(T a[]){int i,j,min;T t;for(i=0;i<5;i++){min=i;for (j=i+1;j<5;j++)if(a[min]>a[j]) min=j;t=a[i]; a[i]=a[min]; a[min]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}int main(){ int a[5]={1,9,0,23,-45};float b[5]={, , , , };long c[5]={10100,-123567, 1198783,-165654, 3456}; sort(a);sort(b);sort(c);return 0;}第一题为第五题以此类推。
谭浩强C++面向对象程序设计第一章习题解答
1.#include <iostream>using namespace std;int main(){cout<<"This"<<"is";cout<<"a"<<"C++";cout<<"program."<<endl; return 0;}2. #include <iostream> using namespace std;int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return 0;}3. #include <iostream> using namespace std;int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return 0;}int f(int x,int y,int z) {int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m); }4. #include <iostream>using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl;return 0;}5. #include <iostream>using namespace std;int main(){int add(int x,int y);int a,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl;return 0;}int add(int x,int y){int z;z=x+y;return(z);}6. #include <iostream>using namespace std;int main(){void sort(int x,int y,int z);int x,y,z;cin>>x>>y>>z;sort(x,y,z);return 0;}void sort(int x, int y, int z){int temp;if (x>y) {temp=x;x=y;y=temp;} //{ }内3个语句的作用是将x和y的值互换) if (z<x) cout<<z<<','<<x<<','<<y<<endl;else if (z<y) cout<<x<<','<<z<<','<<y<<endl;else cout<<x<<','<<y<<','<<z<<endl;}7.#include <iostream>using namespace std;int main(){int max(int a,int b,int c=0);int a,b,c;cin>>a>>b>>c;cout<<"max(a,b,c)="<<max(a,b,c)<<endl; cout<<"max(a,b)="<<max(a,b)<<endl;return 0;}int max(int a,int b,int c){if(b>a) a=b;if(c>a) a=c;return a;}8. #include <iostream>using namespace std;int main(){void change(int &,int &);int a,b;cin>>a>>b;if(a<b) change(a,b);cout<<"max="<<a<<" min="<<b<<endl;return 0;}void change(int &r1,int &r2){int temp;temp=r1;r1=r2;r2=temp;cout<<r1<<" ddd "<<r2<<endl;}9. #include <iostream>using namespace std;int main(){void sort(int &,int &,int &);int a,b,c,a1,b1,c1;cout<<"Please enter 3 integers:";cin>>a>>b>>c;a1=a;b1=b;c1=c;sort(a1,b1,c1);cout<<a<<" "<<b<<" "<<c<<" in sorted order is ";cout<<a1<<" "<<b1<<" "<<c1<<endl;return 0;}void sort(int &i,int &j,int &k){ void change(int &,int &);if (i>j) change(i,j);if (i>k) change(i,k);if (j>k) change(j,k);}void change(int &x,int &y){ int temp;temp=x;x=y;y=temp;}10. #include <iostream>#include <string>using namespace std;int main(){ string s1="week",s2="end";cout<<"s1="<<s1<<endl;cout<<"s2="<<s2<<endl;s1=s1+s2;cout<<"The new string is:"<<s1<<endl;.return 0;}11. #include <iostream>#include <string>using namespace std;int main(){ string str;int i,n;char temp;cout<<"please input a string:";cin>>str;n=str.size();for(i=0;i<n/2;i++){temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;}cout<<str<<endl;return 0;}12. #include <iostream>#include <string>using namespace std;int main(){ int i;string str[5]={"BASIC","C","FORTRAN","C++","PASCAL"}; void sort(string []);sort(str);cout<<"the sorted strings :"<<endl;for(i=0;i<5;i++)cout<<str[i]<<" ";cout<<endl;return 0;}void sort(string s[]){int i,j;string t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (s[i]>s[i+1]){t=s[i];s[i]=s[i+1];s[i+1]=t;}}13. #include <iostream>#include <string>using namespace std;int main(){long c[5]={10100,-123567, 1198783,-165654, 3456};int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };void sort(int []);void sort(float []);void sort(long []);sort(a);sort(b);sort(c);return 0;}void sort(int a[]){int i,j,t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(long a[]){int i,j;long t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(float a[]){int i,j;float t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}14. #include <iostream>#include <string>using namespace std;int main(){ int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };long int c[5]={10100,-123567, 1198783,-165654, 3456}; void sort(int []);void sort(float []);void sort(long []);sort(a);sort(b);sort(c);return 0;}void sort(int a[]){int i,j,t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(float a[]){int i,j;float t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(long a[]){int i,j,min;long t;for(i=0;i<5;i++){min=i;for (j=i+1;j<5;j++)if(a[min]>a[j]) min=j;t=a[i]; a[i]=a[min];a[min]=t; }cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}15. #include <iostream>#include <string>using namespace std;template <typename T>void sort(T a[]){int i,j,min;T t;for(i=0;i<5;i++){min=i;for (j=i+1;j<5;j++)if(a[min]>a[j]) min=j;t=a[i]; a[i]=a[min]; a[min]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}int main(){ int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };long c[5]={10100,-123567, 1198783,-165654, 3456};sort(a);sort(b);sort(c);return 0;}第一题为第五题以此类推。
面向对象程序设计课后习题答案谭浩强版
第一章5:#include <iostream>using namespace std;int main(){cout<<"This"<<"is";cout<<"a"<<"C++";cout<<"program."<<endl; return 0;}6:#include <iostream>using namespace std;int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return 0;}7:#include <iostream>using namespace std;int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return 0;}int f(int x,int y,int z) {int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m);}8: #include <iostream>using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl;return 0;}9:#include <iostream>using namespace std;int main(){int add(int x,int y);int a,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl;return 0;}int add(int x,int y){int c;c=x+y;return(c);}10:#include <iostream>using namespace std;int main(){void sort(int x,int y,int z); int x,y,z;cin>>x>>y>>z;sort(x,y,z);return 0;}void sort(int x, int y, int z){int temp;if (x>y) {temp=x;x=y;y=temp;} 2-4-12-4-22-5-12-5-2Box box1Box box1core; 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;}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,;();(101,;();return 0;}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;}otal();Product::display();return 0;}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;};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<<<<"/"<<<<"/"<<<<endl;cout<<<<":"<<<<":"<<<<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<<<<"/"<<<<"/"<<<<endl;cout<<hour<<":"<<minute<<":"<<sec<<endl;}int main(){Time t1(10,13,56);Date d1(12,25,2004);(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<<()<<" is the Maximum of two integer numbers."<<endl;cout<<()<<" is the Minimum of two integer numbers."<<endl<<endl; Compare<float> cmp2,;cout<<()<<" is the Maximum of two float numbers."<<endl;cout<<()<<" is the Minimum of two float numbers."<<endl<<endl;Compare<char> cmp3('a','A');cout<<()<<" is the Maximum of two characters."<<endl;cout<<()<<" is the Minimum of two characters."<<endl;return 0;}第四章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;};double Complex::get_real(){return real;}double Complex::get_imag(){return imag;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator + (Complex &c1,Complex &c2) {return Complex()+(),()+());}int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c3=";();return 0;}2:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2);Complex operator-(Complex &c2);Complex operator*(Complex &c2);Complex operator/(Complex &c2);void display();private:double real;double imag;};Complex Complex::operator+(Complex &c2) {Complex c;=real+;=imag+;return c;}Complex Complex::operator-(Complex &c2) {Complex c;=;=;return c;}Complex Complex::operator*(Complex &c2) {Complex c;=real**;=imag*+real*;return c;}Complex Complex::operator/(Complex &c2) {Complex c;=(real*+imag*/*+*;=(imag**/*+*;return c;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c1+c2=";();c3=c1-c2;cout<<"c1-c2=";();c3=c1*c2;cout<<"c1*c2=";();c3=c1/c2;cout<<"c1/c2=";();return 0;}3:#include <iostream> ,"president","135 Beijing Road,Shanghai","(021)",;( );return 0;}10:#include <iostream>#include <cstring>using namespace std;class Teacher um<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf[i].pay<<endl;((char *)&staf[i],sizeof(staf[i]));}cout<<"please input data you want insert:"<<endl;for(i=0;i<2;i++){cin>>>>>>>>;(0,ios::end);((char *)&staf1,sizeof(staf1));}(0,ios::beg);for(i=0;i<7;i++){((char *)&staf[i],sizeof(staf[i]));cout<<staf[i].num<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf[i].pay<<endl;}bool find;cout<<"enter number you want search,enter 0 to stop.";cin>>num;while(num){find=false;(0,ios::beg);for(i=0;i<7;i++){((char *)&staf[i],sizeof(staf[i]));if(num==staf[i].num){m=();cout<<num<<" is No."<<m/sizeof(staf1)<<endl;cout<<staf[i].num<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf[i].pay<<endl;find=true;break;}}if(!find)cout<<"can't find "<<num<<endl;cout<<"enter number you want search,enter 0 to stop.";cin>>num;}();return 0;}6:解法一#include <iostream>#include <strstream>using namespace std;struct student{int num;char name[20];double score;};int main(){student stud[3]={1001,"Li",78,1002,"Wang",,1004,"Fun",90},stud1[3];char c[50];int i;ostrstream strout(c,50);for(i=0;i<3;i++)strout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<" ";strout<<ends;cout<<"array c:"<<endl<<c<<endl<<endl;istrstream strin(c,50);for(i=0;i<3;i++)strin>>stud1[i].num>>stud1[i].name>>stud1[i].score;cout<<"data from array c to array stud1:"<<endl;for(i=0;i<3;i++)cout<<stud1[i].num<<" "<<stud1[i].name<<" "<<stud1[i].score<<endl;cout<<endl;return 0;}6:解法二#include <iostream>#include <strstream>using namespace std;struct student{int num;char name[20];double score;};int main(){int i;student stud[3]={1001,"Li",78,1002,"Wang",,1004,"Fun",90},stud1[3]; char c[50];strstream strio(c,50,ios::in|ios::out);for(i=0;i<3;i++)strio<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<" ";strio<<ends;cout<<"array c:"<<endl<<c<<endl<<endl;for(i=0;i<3;i++)strio>>stud1[i].num>>stud1[i].name>>stud1[i].score;cout<<"data from array c to array stud1:"<<endl;for(i=0;i<3;i++)cout<<stud1[i].num<<" "<<stud1[i].name<<" "<<stud1[i].score<<endl; cout<<endl;return 0;}第八章1:#include <iostream>#include <cmath>using namespace std;double q(double,double,double);void main(){double a,b,c,p,x1,x2;cout<<"please enter a,b,c:";cin>>a>>b>>c;p=-b/(2*a);try{x1=p+q(a,b,c);x2=p-q(a,b,c);cout<<"x1="<<x1<<endl<<"x2="<<x2<<endl;}catch(double d){cout<<"a="<<a<<",b="<<b<<",c="<<c<<",disc="<<d<<",error!"<<endl;} cout<<"end"<<endl;}double q(double a,double b,double c){double disc;disc=b*b-4*a*c;if (disc<0) throw disc;return sqrt(disc)/(2*a);}2:#include <iostream>#include <string>using namespace std;class Student{public:Student(int n,string nam){cout<<"constructor-"<<n<<endl;num=n;name=nam;}~Student(){cout<<"destructor-"<<num<<endl;}void get_data();private:int num;string name;};void Student::get_data(){if(num==0) throw num;else cout<<num<<" "<<name<<endl;cout<<"in get_data()"<<endl;}void fun(){Student stud1(1101,"tan");();try{Student stud2(0,"Li");();}catch(int n){cout<<"num="<<n<<",error!"<<endl;}}int main(){cout<<"main begin"<<endl;cout<<"call fun()"<<endl;fun();cout<<"main end"<<endl;return 0;}3://main file#include <iostream>using namespace std;#include ""#include ""using namespace std;using namespace student1;int main(){Student stud1(1001,"Wang",18,"123 Beijing Road,Shanghua"); ();student2::Student stud2(1102,"Li",'f',;();return 0;}//,文件名为#include <string>namespace student1{class Student{public:Student(int n,string nam,int a,string addr){num=n;name=nam;age=a;address=addr;}void show_data();private:int num;string name;int age;string address;};void Student::show_data(){cout<<"num:"<<num<<" name:"<<name<<" age:"<<age<<" address:"<<address<<endl;}}//,文件名为#include <string>namespace student2{class Student{public:Student(int n,string nam,char s,float sco){num=n;name=nam;sex=s;score=sco;}void show_data();private:int num;string name;char sex;float score;};void Student::show_data(){cout<<"num:"<<num<<" name:"<<name<<" sex:"<<sex <<" score:"<<score<<endl; }}。
c--面向对象程序设计课后习题解答-谭浩强
(5)第6行末尾少了一个分号。 (6)add函数中的retrun拼写错误,应为return。编译系统把retrun作为未声明的标识符而报错 ,因为retrun(z)会被认为是函数调用的形式。 (7)变量a和b未被赋值。 改正后的程序如下: #include using namespace std; int main( {int add(int x,int y; int a,b,c; cin >> a >> b; c=add(a,b; cout <<" a+b=" << c < return 0; } int add(int x,int y {int z; z=x+y; return(z; } 运行情况如下: 5 8↙ 13 10.输入以下程序,编译并运行,分析运行结果。 #include using namespace std; int main( { void sort(int x,int y,int z; int x,y,z;
1 5 3↙ (输入3个整数
1 (输出其中最小的数
8.在你所用的C++系统上,输入以下程序,进行编译,观察编译情况,如果有错误,请修改 程序,再进行编译,直到没有错误,然后进行连接和运行,分析运行结果。
int main( ; { int a,b; c=a+b; cout >>" a+b=" >> a+b; } 【解】 上机编译出错,编译出错信息告知在第2行出错,经检查,发现第1行的末尾多了一个分号,编译 系统无法理解第2行的花括号,导致报告第2行出错。将第1行的末尾的分号去掉,重新编译,编 译出错信息告知在第5行和第6行出错。第5行出错原因是cout未经声明,因为cout不是C++语言 提供的系统的关键字,而是输出流的对象,必须使用头文件iostream。第6行出错原因是main是i nt型函数,应返回一个整型值。将程序改为
c++面对对象程序设计课后答案(谭浩强)
第一章5:#include <iostream> using namespace std;int main(){cout<<"This"<<"is"; cout<<"a"<<"C++";cout<<"program."<<endl; return 0;}6:#include <iostream> using namespace std;int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return 0;}7:#include <iostream> using namespace std;int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return 0;}int f(int x,int y,int z){int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m);}8: #include <iostream> using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl; return 0;}9:#include <iostream>using namespace std;int main(){int add(int x,int y);int a,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl; return 0;}int add(int x,int y){int c;c=x+y;return(c);}10:#include <iostream>using namespace std;int main(){void sort(int x,int y,int z); int x,y,z;cin>>x>>y>>z;sort(x,y,z);return 0;}void sort(int x, int y, int z){int temp;if (x>y) {temp=x;x=y;y=temp;} //{ }内3个语句的作用是将x和y的值互换) if (z<x) cout<<z<<','<<x<<','<<y<<endl;else if (z<y) cout<<x<<','<<z<<','<<y<<endl;else cout<<x<<','<<y<<','<<z<<endl;}11:#include <iostream>using namespace std;int main(){int max(int a,int b,int c=0);int a,b,c;cin>>a>>b>>c;cout<<"max(a,b,c)="<<max(a,b,c)<<endl;cout<<"max(a,b)="<<max(a,b)<<endl;return 0;}int max(int a,int b,int c){if(b>a) a=b;if(c>a) a=c;return a;}12:#include <iostream>using namespace std;int main(){void change(int ,int );int a,b;cin>>a>>b;if(a<b) change(a,b);cout<<"max="<<a<<" min="<<b<<endl;return 0;}void change(int ,int ){int r1,r2,temp;temp=r1;r1=r2;r2=temp;}13:#include <iostream>using namespace std;int main(){void sort(int &,int &,int &);int a,b,c,a1,b1,c1;cout<<"Please enter 3 integers:";cin>>a>>b>>c;a1=a;b1=b;c1=c;sort(a1,b1,c1);cout<<a<<" "<<b<<" "<<c<<" in sorted order is "; cout<<a1<<" "<<b1<<" "<<c1<<endl;return 0;}void sort(int &i,int &j,int &k){ void change(int &,int &);if (i>j) change(i,j);if (i>k) change(i,k);if (j>k) change(j,k);}void change(int &x,int &y){ int temp;temp=x;x=y;y=temp;}14:#include <iostream>#include <string>using namespace std;int main(){ string s1="week",s2="end";cout<<"s1="<<s1<<endl;cout<<"s2="<<s2<<endl;s1=s1+s2;cout<<"The new string is:"<<s1<<endl;return 0;}15:#include <iostream>#include <string>using namespace std;int main(){ string str;int i,n;char temp;cout<<"please input a string:";cin>>str;n=str.size();for(i=0;i<n/2;i++){temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;}cout<<str<<endl;return 0;}16:#include <iostream>#include <string>using namespace std;int main(){ int i;string str[5]={"BASIC","C","FORTRAN","C++","PASCAL"}; void sort(string []);sort(str);cout<<"the sorted strings :"<<endl;for(i=0;i<5;i++)cout<<str[i]<<" ";cout<<endl;return 0;}void sort(string s[]){int i,j;string t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (s[i]>s[i+1]){t=s[i];s[i]=s[i+1];s[i+1]=t;}}17: #include <iostream>#include <string>using namespace std;int main(){long c[5]={10100,-123567, 1198783,-165654, 3456}; int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };void sort(int []);void sort(float []);void sort(long []);sort(a);sort(b);sort(c);return 0;}void sort(int a[]){int i,j,t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(long a[]){int i,j;long t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(float a[]){int i,j;float t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}18: #include <iostream>#include <string>using namespace std;template <typename T>void sort(T a[]){int i,j,min;T t;for(i=0;i<5;i++){min=i;for (j=i+1;j<5;j++)if(a[min]>a[j]) min=j;t=a[i]; a[i]=a[min]; a[min]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}int main(){ int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };long c[5]={10100,-123567, 1198783,-165654, 3456}; sort(a);sort(b);sort(c);return 0;}第二章1#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;cin>>minute;cin>>sec;}void Time::show_time() //在main函数之前定义{cout<<hour<<":"<<minute<<":"<<sec<<endl;}int main(){Time t1;t1.set_time();t1.show_time();return 0;}2:#include <iostream>using namespace std;class Time{public:void set_time(void){cin>>hour;cin>>minute;cin>>sec;void show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}private: int hour;int minute;int sec;};Time t;int main(){t.set_time();t.show_time();return 0;}3:#include <iostream>using namespace std;class Time{public:void set_time(void);void show_time(void);private:int hour;int minute;int sec;};void Time::set_time(void){cin>>hour;cin>>minute;cin>>sec;}void Time::show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}Time t;int main(){ t.set_time();t.show_time();return 0;}//xt2-4-1.cpp(main.cpp)#include <iostream>using namespace std;#include "xt2-4.h"int main(){Student stud;stud.set_value();stud.display();return 0;}//xt2-4-2.cpp(即student.cpp)#include "xt2-4.h" //在此文件中进行函数的定义#include <iostream>using namespace std; //不要漏写此行void Student::display( ){ cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}void Student::set_value(){ cin>>num;cin>>name;cin>>sex;}5://xt2-5-1.cpp(file1.cpp)#include <iostream>#include "xt2-5.h"int main(){Array_max arrmax;arrmax.set_value();arrmax.max_value();arrmax.show_value();return 0;}//xt2-5-2.cpp(arraymax.cpp)#include <iostream>using namespace std;#include "xt2-5.h"void Array_max::set_value()for (i=0;i<10;i++)cin>>array[i];}void Array_max::max_value(){int i;max=array[0];for (i=1;i<10;i++)if(array[i]>max) max=array[i];}void Array_max::show_value(){cout<<"max="<<max<<endl;}6:解法一#include <iostream>using namespace std;class Box{public:void get_value();float volume();void display();public:float lengh;float width;float height;};void Box::get_value(){ cout<<"please input lengh, width,height:"; cin>>lengh;cin>>width;cin>>height;}float Box::volume(){ return(lengh*width*height);}void Box::display(){ cout<<volume()<<endl;}{Box box1,box2,box3;box1.get_value();cout<<"volmue of bax1 is ";box1.display();box2.get_value();cout<<"volmue of bax2 is ";box2.display();box3.get_value();cout<<"volmue of bax3 is ";box3.display();return 0;}解法二:#include <iostream>using namespace std;class Box{public:void get_value();void volume();void display();public:float lengh;float width;float height;float vol;};void Box::get_value(){ cout<<"please input lengh, width,height:"; cin>>lengh;cin>>width;cin>>height;}void Box::volume(){ vol=lengh*width*height;}void Box::display(){ cout<<vol<<endl;}int main(){Box box1,box2,box3;box1.get_value();box1.volume();cout<<"volmue of bax1 is ";box1.display();box2.get_value();box2.volume();cout<<"volmue of bax2 is ";box2.display();box3.get_value();box3.volume();cout<<"volmue of bax3 is ";box3.display();return 0;}第三章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;}{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 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;}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;}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;}解法二:#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;};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;}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;};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;}第四章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;};double Complex::get_real(){return real;}double Complex::get_imag(){return imag;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator + (Complex &c1,Complex &c2){return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag()); }int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c3=";c3.display();return 0;}2:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2);Complex operator-(Complex &c2);Complex operator*(Complex &c2);Complex operator/(Complex &c2);void display();private:double real;double imag;};Complex Complex::operator+(Complex &c2){Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;}Complex Complex::operator-(Complex &c2){Complex c;c.real=real-c2.real;c.imag=imag-c2.imag;return c;}Complex Complex::operator*(Complex &c2){Complex c;c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;return c;}Complex Complex::operator/(Complex &c2){Complex c;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c1+c2=";c3.display();cout<<"c1-c2=";c3.display();c3=c1*c2;cout<<"c1*c2=";c3.display();c3=c1/c2;cout<<"c1/c2=";c3.display();return 0;}3:#include <iostream> //用VC++时改为∶#include <iostream.h> using namespace std; //用VC++时为取消此行class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2);Complex operator+(int &i);friend Complex operator+(int&,Complex &);void display();private:double real;double imag;};Complex Complex::operator+(Complex &c){return Complex(real+c.real,imag+c.imag);}Complex Complex::operator+(int &i){return Complex(real+i,imag);}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator+(int &i,Complex &c){return Complex(i+c.real,c.imag);}int main(){Complex c1(3,4),c2(5,-10),c3;int i=5;c3=c1+c2;cout<<"c1+c2=";c3=i+c1;cout<<"i+c1=";c3.display();c3=c1+i;cout<<"c1+i=";c3.display();return 0;}4:#include <iostream>using namespace std;class Matrix //定义Matrix类{public:Matrix(); //默认构造函数friend Matrix operator+(Matrix &,Matrix &); //重载运算符“+”void input(); //输入数据函数void display(); //输出数据函数private:int mat[2][3];};Matrix::Matrix() //定义构造函数{for(int i=0;i<2;i++)for(int j=0;j<3;j++)mat[i][j]=0;}Matrix operator+(Matrix &a,Matrix &b) //定义重载运算符“+”函数{Matrix c;for(int i=0;i<2;i++)for(int j=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}return c;}void Matrix::input() //定义输入数据函数{cout<<"input value of matrix:"<<endl;for(int i=0;i<2;i++)for(int j=0;j<3;j++)cin>>mat[i][j];}void Matrix::display() //定义输出数据函数{for (int i=0;i<2;i++){for(int j=0;j<3;j++){cout<<mat[i][j]<<" ";}cout<<endl;}}int main(){Matrix a,b,c;a.input();b.input();cout<<endl<<"Matrix a:"<<endl;a.display();cout<<endl<<"Matrix b:"<<endl;b.display();c=a+b; //用重载运算符“+”实现两个矩阵相加cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;c.display();return 0;}5:#include <iostream.h>//using namespace std;class Matrix{public:Matrix();friend Matrix operator+(Matrix &,Matrix &);friend ostream& operator<<(ostream&,Matrix&);friend istream& operator>>(istream&,Matrix&);private:int mat[2][3];};Matrix::Matrix(){for(int i=0;i<2;i++)for(int j=0;j<3;j++)mat[i][j]=0;}Matrix operator+(Matrix &a,Matrix &b){Matrix c;for(int i=0;i<2;i++)for(int j=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}return c;}istream& operator>>(istream &in,Matrix &m){cout<<"input value of matrix:"<<endl;for(int i=0;i<2;i++)for(int j=0;j<3;j++)in>>m.mat[i][j];return in;}ostream& operator<<(ostream &out,Matrix &m){for (int i=0;i<2;i++){for(int j=0;j<3;j++){out<<m.mat[i][j]<<" ";}out<<endl;}return out;}int main(){ Matrix a,b,c;cin>>a;cin>>b;cout<<endl<<"Matrix a:"<<endl<<a<<endl;cout<<endl<<"Matrix b:"<<endl<<b<<endl;c=a+b;cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl<<c<<endl; return 0;}6:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r){real=r;imag=0;}Complex(double r,double i){real=r;imag=i;}operator double(){return real;}void display();private:double real;double imag;};void Complex::display(){cout<<"("<<real<<", "<<imag<<")"<<endl;}int main(){Complex c1(3,4),c2;double d1;d1=2.5+c1;cout<<"d1="<<d1<<endl;c2=Complex(d1);cout<<"c2=";c2.display();return 0;}7:#include <iostream>using namespace std;class Student{public:Student(int,char[],char,float);int get_num(){return num;}char * get_name(){return name;}char get_sex(){return sex;}void display(){cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\nscore:"<<score<<"\n\n";} private:int num;char name[20];char sex;float score;};Student::Student(int n,char nam[],char s,float so){num=n;strcpy(name,nam);sex=s;score=so;}class Teacher{public:Teacher(){}Teacher(Student&);Teacher(int n,char nam[],char sex,float pay);void display();private:int num;char name[20];char sex;float pay;};Teacher::Teacher(int n,char nam[],char s,float p){num=n;strcpy(name,nam);sex=s;pay=p;}Teacher::Teacher(Student& stud){num=stud.get_num();strcpy(name,stud.get_name());sex=stud.get_sex();pay=1500;}void Teacher::display(){cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\npay:"<<pay<<"\n\n";}int main(){Teacher teacher1(10001,"Li",'f',1234.5),teacher2;Student student1(20010,"Wang",'m',89.5);cout<<"student1:"<<endl;student1.display();teacher2=Teacher(student1);cout<<"teacher2:"<<endl;teacher2.display();return 0;}第五章1:#include <iostream>using namespace std;class Student{public:void get_value(){cin>>num>>name>>sex;}。
C++面向对象程序设计课后答案(谭浩强)
C++面向对象程序设计课后答案(1-4章)第一章:面向对象程序设计概述[1_1]什么是面向对象程序设计?面向对象程序设计是一种新型的程序设计范型。
这种范型的主要特征是:程序=对象+消息。
面向对象程序的基本元素是对象,面向对象程序的主要结构特点是:第一:程序一般由类的定义和类的使用两部分组成,在主程序中定义各对象并规定它们之间传递消息的规律。
第二:程序中的一切操作都是通过向对象发送消息来实现的,对象接受到消息后,启动有关方法完成相应的操作。
面向对象程序设计方法模拟人类习惯的解题方法,代表了计算机程序设计新颖的思维方式。
这种方法的提出是软件开发方法的一场革命,是目前解决软件开发面临困难的最有希望、最有前途的方法之一。
[1_2]什么是类?什么是对象?对象与类的关系是什么?在面向对象程序设计中,对象是描述其属性的数据以及对这些数据施加的一组操作封装在一起构成的统一体。
对象可以认为是:数据+操作在面向对象程序设计中,类就是具有相同的数据和相同的操作的一组对象的集合,也就是说,类是对具有相同数据结构和相同操作的一类对象的描述。
类和对象之间的关系是抽象和具体的关系。
类是多个对象进行综合抽象的结果,一个对象是类的一个实例。
在面向对象程序设计中,总是先声明类,再由类生成对象。
类是建立对象的“摸板”,按照这个摸板所建立的一个个具体的对象,就是类的实际例子,通常称为实例。
[1_3]现实世界中的对象有哪些特征?请举例说明。
对象是现实世界中的一个实体,其具有以下一些特征:(1)每一个对象必须有一个名字以区别于其他对象。
(2)需要用属性来描述它的某些特性。
(3)有一组操作,每一个操作决定了对象的一种行为。
(4)对象的操作可以分为两类:一类是自身所承受的操作,一类是施加于其他对象的操作。
例如:雇员刘名是一个对象对象名:刘名对象的属性:年龄:36 生日:1966.10.1 工资:2000 部门:人事部对象的操作:吃饭开车[1_4]什么是消息?消息具有什么性质?在面向对象程序设计中,一个对象向另一个对象发出的请求被称为“消息”。
C++面向对象程序设计(谭浩强)课后习题答案text-P36
班级:B11512 姓名:陈德桥学号:20114051230 11、求2个数或3个正整数中的最大数,用带有默认参数的函数实现。
#include<iostream>using namespace std;void max(int a,int b,int c=0);int main(){int a,b,c,m;cout<<"请输入要比较的数的个数:";cin>>m;if(m==2){cout<<"a=";cin>>a;cout<<"b=";cin>>b;max(a,b);}else{cout<<"a=";cin>>a;cout<<"b=";cin>>b;cout<<"c=";cin>>c;max(a,b,c);}return 0;}void max(int a,int b,int c){if(a<b)a=b;if(a<c)a=c;cout<<"比较数中最大的数是"<<a<<endl;}13.对3个变量按由小到大顺序排序,要求使用变量的引用。
#include<iostream>using namespace std;void INT(int &a,int &b,int &c);void FLO(float &a,float &b,float &c);void DOU(double &a,double &b,double &c);int main(){int a,b,c;float e,f,g;double x,y,z;cout<<"请输入三个int型变量:"<<endl;cin>>a>>b>>c;INT(a,b,c);cout<<a<<"<"<<b<<"<"<<c<<endl;cout<<"请输入三个float型变量:"<<endl;cin>>e>>f>>g;FLO(e,f,g);cout<<e<<"<"<<f<<"<"<<g<<endl;cout<<"请输入三个double型变量:"<<endl;cin>>x>>y>>z;DOU(x,y,z);cout<<x<<"<"<<y<<"<"<<z<<endl;return 0;}void INT(int &a,int &b,int &c){int temp;if(a>b)temp=a,a=b,b=temp;if(a>c)temp=a,a=b,b=temp;if(b>c)temp=b,b=c,c=temp;}void FLO(float &a,float &b,float &c){float temp;if(a>b)temp=a,a=b,b=temp;if(a>c)temp=a,a=b,b=temp;if(b>c)temp=b,b=c,c=temp;}void DOU(double &a,double &b,double &c){double temp;if(a>b)temp=a,a=b,b=temp;if(a>c)temp=a,a=b,b=temp;if(b>c)temp=b,b=c,c=temp;}15、输入一个字符串,把其中的字符按逆序输出。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
void display() {cout<<num<<" "<<score<<endl;}
otal();
Product::display();
return0;
}
10:
#include <iostream>
using namespace std;
{month=m;
day=d; year=y;
}
void display(const Date &d,const Time &t)
{
cout<<<<"/"<<<<"/"<<<<endl; cout<<<<":"<<<<":"<<<<endl;
int main()
{
Time t1(10,13,56);
if(arr[i].score>max_score) {max_score=arr[i].score;k=i;} cout<<arr[k].num<<" "<<max_score<<endl;
}
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;
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)
Date d1(12,25,2004); display(d1,t1);
return0;
}
11:
#include <iostream> using namespace std; class Time;
class Date
{public:
Date(int,int,int); friend Time;
private:
第一章
5:
#include <iostream> using namespace std;
int main()
{
cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."<<endl; return0;
}
6:
#include <iostream> using namespace std;
c=a+b;
cout<<"a+b="<<a+b<<endl; return0;
}
9:
#include <iostream>
using namespace std;
int main()
{int add(int x,int y);
int a,b,c;
cin>>a>>b;
c=add(a,b); cout<<"a+b="<<c<<endl; return0;
}
int add(int x,int y)
{int c;
c=x+y;
return(c);
}
10:
#include <iostream>
using namespace std;
int main()
{void sort(int x,int y,int z); int x,y,z;
cin>>x>>y>>z;
sort(x,y,z);
return0;
}
void sort(int x, int y, int z)
{
int temp;
if (x>y) {temp=x;x=y;y=temp;} 2-4-12-4-22-5-12-5-2Box box1Box box1core; int k=0;
for(int i=1;i<5;i++)
int main()
{
int a,b,c;
a=10;
b=23;
c=a+b; cout<<"a+b=";
cout<<c; cout<<endl;
return0;
}
7:
#include <iostream> using namespace std;
int main()
{
int a,b,c;
int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout<<c<<endl;
float score;
};
int main()
{Student stud(101,;
();
(101,;
();
return0;
}
7:解法一
#include <iostream>
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
return0;
}
int f(int x,int y,int z)
{
iቤተ መጻሕፍቲ ባይዱt m;
if (x<y) m=x;
else m=y;
if (z<m) m=z;
return(m);
}
8:#include <iostream> using namespace std;
int main()
{
int a,b,c;
cin>>a>>b;
class Date;
class Time
{public:
Time(int,int,int);
friend void display(const Date &,const Time&);private: int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s)
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:
{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)