第二章
1、当从键盘上输入"23.56(空格)10(空格)90<回车>"时,写出下面程序的运行结果
#include<iostream>
using namespace std;
int main(){
int a,b,c;
char ch;
cin >> a >> ch >> b >> c;
cout << a << endl << ch << endl << b << endl << c;
return 0;
}
运行结果如下:
23
.
56
10
3、写出下面程序的运行结果:
#include<iostream>
using namespace std;
int i = 0;
int main() {
int i = 5;
{
int i = 7;
cout << "::i=" << ::i << endl;
cout << "i=" << i << endl;
::i = 1;
cout << "::i=" << i << endl;
}
cout << "i=" << i << endl;
cout << "Please input x,y: ::i=" << ::i << endl;
i += ::i;
::i = 100;
cout << "i=" << i << endl;
cout << "::i" << ::i << endl;
return 0;
}
运行结果如下:
::i=0
i=7
::i=7
i=5
Please input x,y: ::i=1
i=6
::i=100
6、写出下面程序的运行结果
#include<iostream>
using namespace std;
void fun(int x, int& y) {
x += y;
y += x;
}
int main() {
int x = 5, y = 10;
fun(x, y);
fun(y, x);
cout << "x=" << x << ",y=" << y << endl;
return 0;
}
运行结果如下:
x=35,y=25
注意fun函数里面引用的y是可以传出函数的,而x不可以
第三章
2、写出下面程序的运行结果
#include<iostream>
using namespace std;
class Sample {
int x;
public:
void setx(int i) {
x = i;
}
int getx() {
return x;
}
};
int main() {
Sample a[3], * p;
int i = 0;
for (p = a; p < a + 3; p++) {
p->setx(i++);
}
for (i = 0; i < 3; i++) {
p = &a[i];
cout << p->getx() << " ";
}
return 0;
}
运行结果如下
0 1 2
编程题1、
定义一个学生类,设计私有数据成员
年龄 age;
姓名 string name;
公有成员函数
构造函数:带参数的构造函数Student(int m,string n)
不带参数的构造函数Student();
改变数据成员值函数void SetName(int m,string n)
获取数据成员函数int Getage();String Getname()
在main()中定义一个有3个元素的对象数组并分别初始化,然后输出对象数组的信息。
代码编写如下
#include<iostream>
#include<string.h>
using namespace std;
class Student {
private:
int age;
string name;
public:
Student() { name = "无名氏", age = 0; }
Student(int m, string n) { age = m; name = n; }
void SetName(int m, string n) { age = m; name = n; }
int Getage() { return age; }
string GetName() { return name; }
};
int main() {
Student s[3];
for (int i = 0; i < 3; i++) {
int m; string n;
cin >> m >> n;
s[i].SetName(m, n);
}
for (int i = 0; i < 3; i++) {
cout << s[i].GetName() <<" - "<< s[i].Getage() << endl;
}
}
第四章
编程题
要求先定义一个Point类,用来产生平面上的点对象.两点决定一条线段,即线段由点构成.因此,Line类使用Point类的对象作为数据成员,然后Line类的构造函数中求出线段的长度.
class Point{
private:
double X,Y;
public:
Point(double a,double b);
Point(Point &p);
double GetX();
double GetY();
};
class Line{
private:
Point A,B;
double length;
public:
Line(Point p1,Point p2);
double GetLength();
};
在main()函数中定义线段的两个端点,并输出线段的长度.
代码编写如下
#include<iostream>
using namespace std;
class Point {
private:
double X, Y;
public:
friend class Line;
Point(double a, double b);
Point() {};
Point(Point& p);
double GetX();
double GetY();
};
class Line {
private:
Point A, B;
double length;
public:
Line(Point p1, Point p2);
double GetLength();
};
Point::Point(double a, double b) {
X = a; Y = b;
}
Point::Point(Point& p) {
X = p.X; Y = p.Y;
}
double Point::GetX() {
return X;
}
double Point::GetY() {
return Y;
}
Line::Line(Point p1, Point p2) {
A = p1; B = p2;
length = sqrt(abs(A.X * A.X - B.X * B.X) + abs(A.Y * A.Y - B.Y * B.Y));
}
double Line::GetLength() {
return length;
}
int main() {
Point A(1,1), B(2,2);
Line l(A, B);
cout << "length is :" << l.GetLength();
}
2、定义一个学生类,有如下的基本成员.
私有数据:年龄 int age;姓名 string name;
公有静态数据成员:学生人数 static int count;
公有成员函数:构造函数,带参数的构造函数Student(int m,string n),不带参数的构造函数Student();
析构函数~Student();
输出函数:void Print() const;
主函数的定义及程序的运行结果如下,请完成类的定义及类中各函数的实现代码,补充成一个完整的程序
int main(){
cout<<"cout="<<Student::count<<endl;
Student s1,*p=new Student(23,"ZhangHong");
s1.Print();
p->Print();
delete p;
s1.Print();
Student Stu[4];
cout<<"cout="<<Student::count<<endl;
return 0;
}
运行结果:
count=0
2
Name = NoName , age=0
2
Name = ZhangHong , age=23;
1
Name = NoName , age=0
count=5;
运行代码如下:
#include<iostream>
#include<string.h>
using namespace std;
class Student {
private:
int age;
string name;
public:
static int count;
Student(int m, string n) {
age = m; name = n; count++;
}
Student() { age = 0; name = "NoName"; count++; }
~Student() { count--; }
void Print() {
cout << count << endl;
cout << "Name = " << name << " , age = " << age << endl;
}
};
int Student::count = 0;
int main() {
cout << "cout=" << Student::count << endl;
Student s1, * p = new Student(23, "ZhangHong");
s1.Print();
p->Print();
delete p;
s1.Print();
Student Stu[4];
cout << "cout=" << Student::count << endl;
return 0;
}
第五章
编程题1、
定义一个三维空间坐标的点,并重载下列运算符,主函数定义类对象并调用重载的运算符
(1)插入运算符<<,按(x,y,z)格式输出该点坐标(坐标为整型)。
(2)关系运算符>,如果A点到原点(0,0,0)的距离大于B点到原点的距离,则A>B为真,否则为假。
代码编写如下
#include<iostream>
using namespace std;
class Point {
private:
int X, Y, Z;
public:
Point() { X = 0, Y = 0, Z = 0; }
Point(int x, int y, int z) { X = x; Y = y; Z = z; }
friend ostream& operator <<(ostream& out, const Point& p);
bool operator >(const Point& p);
};
bool Point::operator>(const Point& p) {
if (X * X + Y * Y + Z * Z > p.X * p.X + p.Y * p.Y + p.Z * p.Z) {
return true;
}
else {
return false;
}
}
ostream& operator <<(ostream& out, const Point& p) {
out << "(" << p.X << "," << p.Y << "," << p.Z << ")";
out << endl;
return out;
}
int main() {
Point A(10, 10, 10);
Point B(2, 2, 2);
if (A > B) {
cout << "A>B" << endl;
}
else {
cout << "A<=B" << endl;
}
cout << "Point A:" << A << endl;
cout << "Point B:" << B << endl;
return 0;
}
编程题2、
设计一个矩阵类,要求在矩阵类中重载运算符加(+)、减(-)、乘(*)、赋值(=)和加赋值(+=),主函数定义类对象并调用重载的运算符
代码编写如下:
#include<iostream>
using namespace std;
class Matrix {
private:
double width, length;
public:
Matrix() { width = 0; length = 0; }
Matrix(double w, double l) { width = w; length = l; }
Matrix& operator = (const Matrix& m);
Matrix& operator +=(const Matrix& m);
Matrix operator +(const Matrix& m);
Matrix operator -(const Matrix& m);
Matrix operator *(const Matrix& m);
void Print();
};
Matrix& Matrix::operator = (const Matrix& m) {
width = m.width;length = m.length;
return *this;
}
Matrix& Matrix::operator+=(const Matrix& m) {
width += m.width;
length += m.length;
return *this;
}
Matrix Matrix::operator + (const Matrix& m) {
Matrix result;
result.width = width + m.width;
result.length = length + m.length;
return result;
}
Matrix Matrix::operator-(const Matrix& m) {
Matrix result;
result.width = abs(width - m.width);
result.length = abs(length - m.length);
return result;
}
Matrix Matrix::operator*(const Matrix& m) {
Matrix result;
result.width = width * m.width;
result.length = length * m.length;
return result;
}
void Matrix::Print() {
cout << "width is : " << width << " , length is : " << length << endl;
}
int main() {
Matrix m1(10, 10), m2(5, 5),m3;
cout << "m1 : ";
m1.Print();
cout << "m2 : ";
m2.Print();
cout << "m3 = m1 : ";
m3 = m1;
m3.Print();
cout << "m1 + m2 : ";
m3 = m1 + m2;
m3.Print();
cout << "m1 - m2 : ";
m3 = m1 - m2;
m3.Print();
cout << "m1 * m2 : ";
m3 = m1 * m2;
m3.Print();
cout << "m2 += m1 : ";
m2 += m1;
m2.Print();
}