C++程序设计课后习题和答案

//出自51博客:www.Amanda0928.51.com

第一章

一、选择题

1.B; (typedef ,typeid ,typename,都为保留字);

2.C; (标识符,应该以字母或,下划线开头);

3.C; (标识符中有的特殊符号,只能有下划线);

二、填空题

1. cin,cout

2. new,delete

3. int a(55);

三、改错题

1.没有定义变量num;

2.不能给变量x,声明指向常量的指针const int *p=&x; 如果吧x定义为常量const,*p不能当作“左值”。

3.p为常量指针,不能吧p作为“左值”,p=&y,错误。

四、编程题

1. 分别用字符和ASCII码形式输出整数值65和66.

#include < iostream >

using namespace std;

void main()

{

}

2.编写一个int型变量分配100个整形空间的程序。

#include < iostream >

using namespace std;

void main() char a='A',b='B'; int ascii_1=53,ascii_2=54;//ASCII码中的,5和6 cout<<

{

}

3.编写完整的程序,它读入15个float值,用指针把它们存放在一个存储快里,然后输出这些值和以及最小值。

#include < iostream >

#include < algorithm > //用于数组排列的头文件

using namespace std;

void main()

{

}

4.声明如下数组: float *p; p = new float[15]; cout<<

int a[] = {1 ,2 ,3, 4, 5, 6, 7, 8};

先查找4的位置,讲数组a复制给数组b,然后将数组a的内容反转,再查找4的位置,最后分别输出数组a和b的内容。

#include < iostream>

#include < algorithm>

#include < functional>

using namespace std;

void main()

{

}

int a[]={1,2,3,4,5,6,7,8},b[8]; cout<<

[color=#FF0000]第二章 [/color]

一、单项选择

1.D; 2.D;

二、作图题

1. 已知一个学生类具有性别和年龄两个属性,男学生张明的年龄为12岁,女学生李红的年龄为11岁。给出这个学生类的类图和它们的对象图。

(类)Student (对象)张明 (对象)李红

string sex; sex(男); sex(女);

int age; age(12); age(11);

方法„ 方法„ 方法„

2. 一个圆具有圆心坐标和半径两个属性,并且能够给出圆面积,请画出这个圆类的类图。 (类) Circularity (类)Point

Point p; float x;

float radii; float y;

float getX();

float getAcreage(); float getY();

3. 画出一个班级类的类图,为它设计必要的属性以表示这个类的特征。

(类) PubClass

string no;//编号

int num;//人数

4. 画出一种电话卡的类图,为它设计必要的属性。

(类) Card

long no;//编号

float balance;//余额

5. 为上题的电话卡设计必要的成员函数,以便提供基本服务。

(类) Card

long no;//编号

float balance;//余额

float getBalance();//显示余额

三、编程题

1.使用多种方法编写将两个字符串连接在一起的程序。

#include < iostream >

#include < string >

using namespace std;

void main()

{

//使用string类定义字符串,完成字符串连接 string str1(

}

i++; k++; } i=0; while(c2[i]!='\0') { } cout<< c3<< endl; c3[k]=c2[i]; i++; k++;

2.已知一个string的对象str的内容为“We are here!”,使用多种方法输出“h”。

#include < iostream >

#include < functional >

#include < algorithm >

#include < string >

using namespace std;

void main()

{

string str1(

cout<< str1[7]<< endl;//通过数组

}

[color=#F70909] string str2=str1.substr(7,1);//通过得到子字符串 cout<< str2<< endl; char *p=find(str1.begin(),str1.end(),'h');//通过find函数 if(p) cout<<*p<< endl;

第三章 [/color]

一、填空题

1.函数原型声明;

2.inline

3.传值,传引用

4.函数func返回引用

5.int *fun(char,int);

二、单项选择题

1.A; 2.C; 3.D;

三、改错题

1.y = x * x - T; 错误,T是类型,不是变量,不能参加运算;

2.y没有类型。

T max(T x, T y)

{

return (x>y) ? (x) : (y) ;

}

3.函数change 的参数定义成了常量,只能使用参数,而无权修改他。

void change (string & s)

{

s = s +

}

四、编程题

1.编写一个求方程ax2 + bx + c = 0的根 的程序,用3个函数分别求当b2-4ac大于零、等于零、和小于零时的方程的根。要求从主函数输入a,b,c的值并输出结果。

#include < iostream.h >

#include < math.h >

void equation_1 (int a, int b, int c)

{

double x1, x2, temp; temp = b*b - 4 * a * c; x1 = (-b + sqrt(temp) ) / (2 * a * 1.0); x2 = (-b - sqrt(temp) ) / (2 * a * 1.0); cout<<

cout<<

}

void equation_2 (int a, int b, int c)

{

}

void equation_3 (int a, int b, int c)

{

double temp, real1, real2, image1, image2; temp = - (b*b - 4 * a * c); real1 = -b / (2 * a *1.0); real2 = real1; image1 = sqrt(temp); image2 = - image1; cout<<

}

void main()

{

int a, b, c;

}

2.定义函数up(ch),如字符变量ch是小写字母就转换成大写字母并通过up返回,否则字符ch不改变。要求在短小而完全的程序中显示这个程序是怎样被调用的。

double temp; cout<<

#include < iostream >

using namespace std;

char up (char c)

{

}

void main()

{

}

3.编写主程序条用带实数r和整数n两个参数的函数并输出r的n次幂。

#include < iostream.h >

#include < math.h >

double power(double a, int b)

{

int i; double result = 1.0; for(i=0;i< b;i++) result = result * a; return result; int i; char c[15] = {'A','v','e','t','E','T','%','&','4','Y','e','i','@','9','^'}; for(i = 0 ; i < 15 ; i++) cout<< up(c[i])<<

}

void main()

{

}

double r; int n; cout<<

4.编写有字符型参数C和整形参数N的函数,让他们显示出由字符C组成的三角形。其方式为第1行有1个字符C,第2行有2个字符C ,等等。

#include < iostream >

using namespace std;

void print_triangle(char c, int n)

{

int i, j; for(i=0; i< n; i++) { for(j=0; j<=i; j++) { cout<< c; } cout<< endl;

}

}

void main()

{

print_triangle('a',10);

}

5.编写一个ieqiu字符串长度的函数,strlen(),再用strlen()函数编写一个函数revers(s)的倒序递归程序,使字符串s逆序。

#include < iostream >

#include < string >

using namespace std;

int strlen(char *str)

{

}

{

char c;

int j, len;

len=strlen(b); int len = 0; while(str[len] != '\0') { } return len; len++; void revers(char *b)

j=len/2-1;

while(j>=0)

{

c=*(b+j);

*(b+j)=*(b+len-j-1);

*(b+len-j-1)=c;

j--;

}

void main()

{

}

6.用函数模板实现3个数值中按最小值到最大值排序的程序。

#include < iostream >

using namespace std;

template

void sort(T a, T b, T c)

{

T array[3],temp; int i,j; array[0] = a; array[1] = b; array[2] = c; for(i=0;i<3;i++) { for(j=0;j<2;j++) if(array[j]>array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; char str[]={

}

void main()

{

sort(5,1,9);

}

7.利用函数模板设计一个求数组元素中和的函数,并检验之。

#include < iostream >

using namespace std;

template

T sum (T a[],int n)

{

}

void main ()

{

int a[5]={1,2,3,4,5};

int s = sum(a,5);

} cout<< s<< endl; int i; T s=0; for(i=0;i< n;i++) s = s + a[i]; return s;

8.重载上题中的函数模板,使他能够进行两个数组的求和。

#include < iostream >

using namespace std;

template

T sum (T a[], int n)

{

int i; T s=0; for(i=0;i< n;i++) s = s + a[i]; return s;

}

template //重载上面的模板

T sum (T a[], int n, T b[], int m)

{

return sum(a,n)+sum(b,m);

}

void main ()

{

int a[5]={1,2,3,4,5};

int b[10]={1,2,3,4,5,6,7,8,9,10};

int s1 = sum(a, 5);

int s2 = sum(b, 10);

int s3= sum(a, 5, b, 10);

cout<< s1<< endl;

cout<< s2<< endl;

cout<< s3<< endl;

}

[color=#EE1111]第四章[/color]

一、填空题

1.数据成员、成员函数;

2.类、重载、1;

3.fun:fun(fun &)、fun:fun(const fun &);

二、单项选择题

1.C。2.C。3.没又答案,应该是A::~A(void)、或A::~A()。4.B。

三、改错题

1.return m;---错误,没又定义变量m;

2.A.init(24,56);---错误,应该先定义A对象:Point A;

四、完成程序题

1.

#include < iostream >

using namespace std;

class base

{

private : //私有数据成员

int a, b;

public :

void init(int x, int y)//公有函数

{ 5.C。 6.C。7.D

}; b = y; } void print() { } cout<<

void main()

{

}

2.

#include

using namespace std;

class Point

{

private : public : int m, n; Point(int, int);//整型变量,为参数的构造函数 Point(Point&);//复制构造函数的原型 print() { cout<<

};

Point::Point(int a, int b)

{

m = a; n = b;

}

Point::Point(Point & t)//复制构造函数的定义

{

m = t.m;

n = t.n;

}

void main()

}

五、程序分析题

1.没有结果,因为没有main函数

如果加main函数

void main()

{

base b(10, 20); Point a(10,89); Point b(a); a.print(); b.print();

}

输出:

初始化...10,20

Destory...10,20

2.

输出:

55

六、编程题

1.设计一个点类Point,再设计一个矩形类,矩形类使用Point类的两个坐标点作为矩形的对角顶点。并可以输出4个坐标值和面积。使用测试程序验证程序。

#include

using namespace std;

class Point //点类

{

private: int x, y;//私有成员变量,坐标 public : Point()//无参数的构造方法,对xy初始化 { } x = 0; y = 0;

Point(int a, int b)//又参数的构造方法,对xy赋值 { x = a; y = b; } void setXY(int a, int b)//设置坐标的函数 { x = a; y = b; } int getX()//得到x的方法 { return x; } int getY()//得到有的函数 { } return y;

};

class Rectangle

{

//矩形类 private: Point point1, point2, point3, point4;//私有成员变量,4个点的对象

public :

Rectangle();//类Point的无参构造函数已经对每个对象做初始化啦,这里不用对每个点多初始化了

角顶点

Rectangle(Point one, Point two)//用点对象做初始化的,构造函数,1和4为对{ point1 = one; point4 = two;

init();

}

Rectangle(int x1, int y1, int x2, int y2)//用两对坐标做初始化,构造函数,1和4为对角顶点

{ point1.setXY(x1, y1); point4.setXY(x2, y2); init(); } void init()//给另外两个点做初始化的函数 { point2.setXY(point4.getX(), point1.getY() );

} void printPoint()//打印四个点的函数 { cout<<

};

void main()

{

Point p1(-15, 56), p2(89, -10);//定义两个点

Rectangle r1(p1, p2);//用两个点做参数,声明一个矩形对象r1

}

2.使用内联函数设计一个类,用来表示直角坐标系中的任意一条直线并输出它的属性。

#include < iostream.h >

#include < math.h >

class Line Rectangle r2(1, 5, 5, 1);//用两队左边,声明一个矩形对象r2 cout<<

};

inline Line::Line(int a, int b, int c, int d)

{

x1 = a; y1 = b; x2 = c; y2 = d; private: int x1, y1, x2, y2; public : Line(); Line(int =0, int =0, int =0, int=0 ); void printPoint(); double getLength();

}

inline void Line::printPoint()

{

cout<<

cout<<

}

inline double Line::getLength()

{

double length;

length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ); return length;

}

void main()

{

}

Line line(10,80,-10,12); line.printPoint(); cout<< line.getLength() << endl;

[color=#FF0000]第五章 [/color]

一、填空题

1.常成员函数;

2.常量成员函数;

3.const

二、单项选择题

1.B; 2.A; 3.C; 4.A;

三、改错题

1.static int getn(){return number;}错误

静态成员函数,只允许访问静态成员变量,number不是静态成员变量

2.void main()

{

test *two[2] = {new test(4, 5), test(6 ,8)}; for( i=0; i<2; i++) delete two[i];

}

四、完成程序题

#include < iostream >

using namespace std;

class test

{

int x; public : test(int a) { x = a; } int GetX() { } return x;

};

void main()

{

} int i;//填空一,声明变量i test *p, a[2][3] = {{1, 2, 3}, {4, 5, 6}}; for( p=&a[0][0], i=0; i<=6; i++, p++)//填空2,初始化p,i { } if((p-a[0])%3 == 0) cout<< endl; cout<< p->GetX() <<

五、编程题

1.声明复数的类,complex,使用友元函数add实现复数加法。

#include < iostream >

using namespace std;

class Complex

{

};

Complex add(Complex c1, Complex c2)//定义友元函数 private: double real, image; public : Complex(){} Complex(double a,double b) { } { real = a; image = b; } double getReal() { return real; real = a; image = b; void setRI(double a, double b) } double getImage() { } return image; void print() { if(image>0) cout<<

}

void main()

{

Complex c1(19, 0.864), c2, c3; c2.setRI(90,125.012); c3 = add(c1, c2); cout<<

}

2.例子5.8,114页例子不错;

3.编写一个程序,该程序建立一个动态数组,为动态数组的元素赋值,显示动态数组的值并删除动态数组。

#include < iostream >

using namespace std;

void main()

{

}

int i, n, temp=0; cout<<

4.定义一个Dog类,它用静态数据成员Dogs记录Dog的个体数目,静态成员函数GetDogs用来存取Dogs。设计并测试这个类。

#include < iostream >

using namespace std;

class Dog

{

private: static int dogs;//静态数据成员,记录Dog的个体数目 public : Dog(){} void setDogs(int a) { } dogs = a; static int getDogs() { return dogs; }

};

int Dog :: dogs = 25;//初始化静态数据成员

void main()

{

cout<<

}

Dog a, b; cout<<

[color=#FF0000]

第六章[/color]

一、填空题

1.单一继承; 2.private protected public

二、单项选择

1.D;2.A;3.C;4.D;

三、改错题

1.类derived和base中均没变量b,derived的构造函数中的m(b)错误;

2.Derived类中重载show()方法

void Show()

{

Base1::Show();Base2::Show();

}

四、编程题

1.设计一个基类,从基类派生圆柱,设计成员函数输出它们的面积和体积;

#include < iostream >

using namespace std;

class Basic//基类

{

protected: double r; public : Basic(){ r = 0; }

Basic(double a):r(a){}

};

class Circular : public Basic//从基类派生圆类

{

protected: public double area; : Circular(double a) { r = a; area = area = 3.1415926 * r * r; } double getArea()//返回圆面积 { } return area;

};

class Column : public Circular//从圆类派生圆柱类

{

protected: double h; double cubage; public : Column(double a, double b) : Circular(a) { } h = b; cubage = getArea() * h;

double getCubage()//返回圆柱体积函数 { } return cubage;

};

{

} void main() Circular circular(45); Column column(12, 10); cout<<

3.定义一个线段类作为矩形的基类,基类有起点和终点坐标,有输出左边和长度以及线段和x轴的夹角的成员函数。矩线段对象的两个坐标作为自己一条边的位置,它具有另外一条边,能输出矩形的4个顶点坐标。给出类的定义并用程序验证它们的功能。

#include < iostream >

#include < cmath >

using namespace std;

class Point//点类

{

};

class Line

{

protected: Point p1, p2;//Point对象做成员 double length, angle; public: Line(double a, double b, double c, double d):p1(a, b), p2(c, d)//用两对坐标初始化protected: double x, y; public : Point(){} Point(double a, double b) { } x = a; y = b; double getX() {return x;} double getY() {return y;}

线段

{ init(); } Line(Point a, Point b)//用两个点的对象初始化线段 { p1 = a; p2 = b; init(); } void init()//计算线段长度,以及和x轴的夹角的度数 { double x1 = p1.getX(), y1 = p1.getY(); double x2 = p2.getX(), y2 = p2.getY(); length = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); angle = atan( (y2-y1) / (x2-x1) ); angle = angle *180/3.141592653; } void printXY() { cout<<

};

class Rectangle : public Line

{

protected: Line *line; public: Rectangle(double a, double b, double c, double d, double e, double f, double g, double h):Line(a,b,c,d)

{

line = new Line(e,f,g,h); } Rectangle(Point a, Point b, Point c, Point d) : Line(a, b)//4个点对象,初始化 { line = new Line(c, d);

} void printPoint() { cout<<

};

void main()

{

Point p1(0, 0), p2(4, 3), p3(12, 89), p4(10, -50); Line l1(0,0,4,3); l1.printXY(); l1.printLength(); l1.printAngle(); Line l2(p1, p2); l2.printXY(); l2.printLength(); l2.printAngle(); Rectangle r1(12,45,89,10,10,23,56,1); r1.printPoint(); Rectangle r2(p1, p2, p3, p4); r2.printPoint();

}

4.基类是使用极坐标的点类,从它派生一个圆类,圆类用点类的左边作圆心,圆周通过极坐标原点,圆类有输出圆心直、圆半径和面积的成员函数。完成类的设计并验证之。

#include < iostream >

#include < cmath >

using namespace std;

class Point//点类

{

protected:

int x, y; Point(){} public :

};

class Circular : public Point//圆类,继承点类

{

protected: double r, area; public :

Circular(int a, int b) { } x = a; y = b; r = sqrt( x * x + y * y ); area = 3.1415926 * r * r; void printPoint() { cout<<

};

void main()

{

}

5.设计一个线段基类,当创建五参数对象时,才要求用户输入长度。同样,其派生的直角三角形类也是在产生对象时要求输入两个直角边的长度。直角三角形在派生矩形类,矩形类的参数也由键盘输入。设计这些类并测试他们的功能。

#include < iostream >

#include < cmath >

using namespace std;

class Line//线段基类

{ Circular c(10,25); c.printPoint(); c.printRadius(); c.printArea();

double sizeA; public : Line() { cout<<

};

class Triangle : public Line//三角形类

{

protected: double sizeB, sizeC; public : Triangle() { cout<<

};

class Rectangle : public Triangle//矩形类

{

protected: double sizeD; Rectangle() { sizeC = sizeA; public :

}; } void printSize() { cout<<

void main()

{

/* Line *l = new Line();

cout<<

*/

}

/*Triangle *t = new Triangle(); t->printSize();*/ Rectangle *r = new Rectangle(); r->printSize();

[color=#FF0000]第七章 [/color]

一、单项选择

1.A; 2.A; 3.B; 4.D; 5.D

二、填空题

1.rbegin(), insert(iterator it, const T& );

2.size(), 2;

3.typedef vector< 数据类型 >::reverse_iterator reverse_iterator

三、改错题

1.第六行的返回类型

T getx()

{ return x;

}

2.类Point的构造方法中的参数类型是int,所以在Line构造方法中的a,b应该是int型;

四、编程题

1.使用类模板演示复制兼容性规则。

#include < iostream>

using namespace std;

template

class Point

{

protected: T x, y; public : Point(T a, T b) { x = a; y = b; } void show() { } cout<<

};

template

class Rectangle : public Point

{

private: T h, w; public : Rectangle(T a, T b, T c, T d) : Point(a, b)

{ } h = c; w = d; void show() { } cout<<

};

{

void main() Point a(3, 4); Rectangle b(5.1, 6.2, 7.3, 8.4); a.show();

b.show(); Point & ra = b;//子类对象 初始化父类的引用 ra.show(); Point * p = &b;//子类对象的地址,赋给指向父类的指针 p->show(); Rectangle * pb = &b;//子类指针pb pb->show(); a = b; //派生类对象的属性值,更新父类对象的属性值

a.show();

}

//134页,例6.3 赋值兼容规则的例子

2.设计一个点的类模板,分别使用继承、包含的方法设计线段类模板,要求演示构造函数和复制构造函数的设计方法,并用主程序验证之。

#include < iostream>

using namespace std;

template class Point

{

public :

T x, y; Point(T a=0, T b=0) { x = a; y = b; } void show() { } cout<<

};

template class Line_1 : public Point // 继承Point类模板, 的线段类模板

{

protected: T x1, y1; public : Line_1(T a, T b, T c, T d) : Point(a, b)

}; x1 = c; y1 = d; } Line_1(const Line_1 & );//复制构造函数 void show() { } cout<<

template Line_1 :: Line_1(const Line_1 & t) : Point(t.x, t.y)

{

x1 = t.x1; y1 = t.y1;

}

template class Line_2 //包含point类模板,的线段类

{

protected:

Point p1, p2; : Line_2(T a, T b, T c, T d) { p1.x = a; p1.y = b; p2.x = c; p2.y = d; public } Line_2(const Line_2 &);//复制构造函数 void show() { } cout<<

};

template Line_2 :: Line_2(const Line_2 & t)

{

}

void main()

{

Line_1 L1(1,2,3,4); cout<<

}

Line_1 L2(L1); //用现有的对象,初始化新对象 cout<<

3.已知有一个整型数组a,其内容为1 3 5 7 9 2 4 6 8 10.先对数组进行升序排列,再使用它产生向量b,然后再在向量的尾部追加11,并按降序排列输出向量的内容和capacity()的内容。 #include < iostream >

#include < vector >

#include < algorithm >

using namespace std;

void main()

{

}

[color=#F70909]第八章[/color]

一、单项选择题

1.A; 2.B;

二、分析程序题

1.

2. int a[] = {1,3,5,7,9,2,4,6,8,10}; sort(a, a+10);//先对数组进行升序排序 copy(a, a+10, ostream_iterator(cout,

三、查错题

print函数的参数应该是引用

void print(base & p)

{

p.show();

}

四、完成程序题

#include < iostream >

using namespace std;

class base

{

int i;

int j; public: base(int I, int J) : i(I),j(J) { display(); } int getI() const { return i; } int getJ() const { return j; } void display() const { }

cout<<

};

class derived : public base

{

int k; public:

}; { display(); } void display() const { } cout<<

void main()

{

}

base b3(8, 9); derived d1(10, 20, 5);

[color=#FF0000]

第九章[/color]

一、单项选择

1.B; 2.A; 3.C; 4.B 5.D 6.B

二、填空题

1.输出数据按输出域右边对齐输出

2.cin.ignore(3)

3.ofstream fout(

三、分析程序题

1.

2.

四、完成程序题

1.

#include < iostream >

#include < iomanip >

using namespace std;

void main()

{

cout.precision(6);

cout<< scientific << showpos;

cout<< -25.89F <<

cout<< 25.89f << endl;

}

2.

class Fun

{

};

void main()

{

Fun fun;

cout<< setfill('*') << setw(10) <<12345<<

} cout<< fun << setw(10) << 54321 << endl; friend ostream & operator<<(ostream & os, Fun f) { } os.setf(ios::left); return os;;

五、编程题

1.利用流格式控制,进行成绩和名字的输出,要求名字左对齐,分数右对齐。

#include < iostream >

#include < string >

using namespace std;

class Student

{

private : string name; float score; public : Student(){} Student(string n, float s)

{ name = n; score = s; } string getName() { return name; } float getScore() { } return score;

};

void main()

{

} Student s1(

2.编写一个产生文本文件的程序。

#include < iostream>

#include < fstream >

using namespace std;

void main()

{

} char *p = {

3.编写一个程序,要求输入三角形的3条边,然后判断是否合理,如果不合理,给出信息并要求重新输入;如果合理,计算其面积并将结果存入文件中。

//我调试这个程序的时候,发现必须关掉卡巴斯基才可以,不知道为什么

#include < iostream >

#include < fstream >

#include < cmath >

#include < vector >

#include < iomanip >

#include < string >

using namespace std;

class Triangle

{

double sizeA, sizeB, sizeC, area; public: Triangle(){} void setArea() { double p = (sizeA + sizeB + sizeC) *0.5; area = sqrt( p * (p - sizeA) * (p - sizeB) * (p - sizeC) ); } void setSizeA(double a) { } void setSizeB(double b) { sizeB = b; } void setSizeC(double c) { sizeC = c; } void set(vector &); sizeA = a;

};

//***************************************

//* 成员函数:set

//* 参 数 :向量对象的引用

//* 返回值 :无

//* 功能 :为向量赋值并将向量存入文件

//***************************************

void Triangle :: set(vector & v )

{

Triangle t; double a, b, c; while(1) { cout<<

cin>>a; if(a == -1)//结束符为-1 { ofstream writeFile; char fileName[20]; cout<<

writeFile<< v[i].sizeA <<

writeFile.close();

cout<<

}

void main()

{

vector tri; Triangle triangle; } return; } cout<<

triangle.set(tri);

}

4.改写上题的程序,使程序反复计算,直到输入结束符号为止。要求在停止计算后,询问要保存的文件名,然后讲结果一次写入制定文件中。

//需要关掉卡巴斯基

#include < iostream >

#include < fstream >

#include < cmath >

#include < vector>

#include < iomanip >

#include < string >

using namespace std;

class Triangle

{

double sizeA, sizeB, sizeC, area; public: Triangle(){} void setArea() { } double p = (sizeA + sizeB + sizeC) *0.5; area = sqrt( p * (p - sizeA) * (p - sizeB) * (p - sizeC) ); void setSizeA(double a) { sizeA = a; } void setSizeB(double b) { sizeB = b; } void setSizeC(double c) { }

void set(vector &); sizeC = c;

};

//***************************************

//* 成员函数:set

//* 参 数 :向量对象的引用

//* 返回值 :无

//* 功能 :为向量赋值并将向量存入文件

//***************************************

void Triangle :: set(vector & v )

{

Triangle t;

double a, b, c;

while(1) { cout<<

writeFile.close();

cout<<

return; } cout<<

cout<<

}

}

void main()

{

}

5.从文件TEST中读出字符并写入TEST1里,要求均附加错误检查。

#include

#include

using namespace std;

void main()

{

} ifstream txt1(

6.从键盘输入一个字符串,将其中的大写字母全部转换成小写字母,然后存入到文件名为“text”的磁盘文件中保存。输入的字符串以“$”结束。

//需要关掉卡巴斯基

#include < iostream >

#include < fstream >

using namespace std;

void main()

{

}

char a[100]; ofstream writeFile(

[color=#FF0000]第十章[/color]

一、单项选择题

1.D; 2.A; 3.B; 4.D

二、填空题

1.过程抽象和数据抽象

2.对象

3.问题域、系统边界、系统责任

4.我觉得应该是,类的成员有(数据成员)和(成员函数)两打类。

5.

四、编程题

1.取消设计实例中的Cow属性,练习使用模板实现包含的设计方法。 #include < iostream >

#include < cmath >

using namespace std;

//********************

//* 声明Point类

//********************

template

class Point

{

T x, y; public: Point(T a= 0, T b= 0) : x(a), y(b){} Point(Point & a) { x = a.x; y = a.y; } void Display(); T Distance(Point &); T getX(){return x;}

T getY(){return y;}

};

//***************

//*成员函数:Point :: Display()

//*功能 :打印点坐标

template

void Point :: Display()

{

cout<< x <<

}

//**************************

//*成员函数:Point :: Distance

//*参数 :Point对象的引用

//*返回值 :两点间距离

//*返回类型:T

template

T Point :: Distance(Point & a)

{

return sqrt( (x - a.x)*(x - a.x) + (y - a.y) * (y - a.y) );

}

//**********************

//* 声明Line

//**********************

template

class Line

{

};

//*************************

//*成员函数:Line::Dispaly

//*参数 :无

//*功能 :打印线段每个点的坐标

template

void Line :: Display()

{

a.Display();

} b.Display(); Point a, b; public: Line(Point & a1, Point & a2) : a(a1), b(a2){} Line(Line & s) : a(s.a), b(s.b){} void Display(); T Distance(); T getArea();

//*************************

//*成员函数:Line::Distance

//*参数 :无

//*返回值 :线段长度

template

T Line :: Distance()

{

} T x = a.getX() - b.getX(); T y = a.getY() - b.getY(); return sqrt( x * x + y * y );

void main()

{

Point a;

Point b(7.8, 9.8); Point c(34.5, 67.8); a = c; a.Display(); b.Display(); cout<<

}

2.取消设计实例中的Cow属性,练习使用模板实现继承的设计方法。 #include < iostream >

#include < cmath >

using namespace std;

//*********************

//*声明Point类

//********************

template

class Point

{

protected:

T x, y; public: Point(T a = 0, T b = 0): x(a), y(b){} Point(Point &a) { } virtual void Display() { } T Distance(Point &); T getX(){return x;} T getY(){return y;} cout<<

//****************************

//*成员函数: Point::Distance

//*参数 : Point对象的引用

//*返回值 : 两点间距离

template

T Point :: Distance(Point &a)

{

}

//***********************

//*声明Line类

//***********************

template

class Line : public Point

{

double x2, y2;

public: Line(T, T, T, T ); Line(Line & ); void Display(); T Distance(); T getX2(){return x2;} T getY2(){return y2;} friend void Disp(Line & t){cout< return sqrt( (x - a.x) * (x - a.x) + (y - a.y) * (y - a.y) );

Line :: Line(T a1, T a2, T a3, T a4):Point(a1, a2), x2(a3), y2(a4){}

template

Line :: Line(Line &s) : Point(s.x, s.y), x2(s.x2), y2(s.y2){}

//***************************

//*成员函数:Line::Distance()

//*返回值 :线段长度

template

T Line::Distance()

{

}

//***************************

//*成员函数:Line::Display() T x = x2 - x; T y = y2 - y; return sqrt(x*x + y*y);

//*功能 :打印线段两个端点坐标

template

void Line::Display()

{

cout<<

//****************************

//*友元函数:operator<<

//*返回值 :ostream &

//*功能 :重载“<< ”

template

ostream & operator<<(ostream & stream, Line obj)

{

stream<<

stream<< obj.getX() <<

}

void main()

{

Pointa;

Line s(7.8, 9.8, 34.5, 67.8); Disp(s); Line s1(s); cout<<

} cout<<

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,511评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,495评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,595评论 0 225
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,558评论 0 190
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,715评论 3 270
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,672评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,112评论 2 291
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,837评论 0 181
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,417评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,928评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,316评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,773评论 2 234
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,253评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,827评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,440评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,523评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,583评论 2 249

推荐阅读更多精彩内容