1、C++基础知识实验
编写C++程序完成“矩阵”以下功能:
(1) 假定矩阵大小为 4×5(整型);
(2) 矩阵空间采用 new 动态申请,保存在指针中;
(3) 定义矩阵初始化函数,可以从 cin 中输入矩阵元素;
(4) 定义矩阵输出函数,将矩阵格式化输出到 cout;
(5) 定义矩阵相加的函数,实现两个矩阵相加的功能,结果保存在另一个矩阵中;
(6) 定义矩阵相减的函数,实现两个矩阵相减的功能,结果保存在另一个矩阵中;
(7) 动态申请三个矩阵:A1、A2、A3;
(8) 初始化 A1、A2;
(9) 计算并输出 A3 = A1 加 A2,A3 = A1 减 A2;
(10) 释放矩阵空间。
#include <iostream>
#include <iomanip>
using namespace std;
#define MATRIX_ROW 4
#define MATRIX_COL 5
void initMatrix(int (*matrix)[MATRIX_COL]){
for(int i = 0; i < MATRIX_ROW; ++i){
for(int j = 0; j < MATRIX_COL; ++j){
cin >> *(*(matrix + i) + j);
}
}
};
void printMatrix(int (*matrix)[MATRIX_COL]){
for(int i = 0; i < MATRIX_ROW; ++i){
for(int j = 0; j < MATRIX_COL; ++j){
cout << setw(5) << *(*(matrix + i) + j) << " ";
}
cout << endl;
}
};
void matrixPlus(int (*matrix1)[MATRIX_COL], int (*matrix2)[MATRIX_COL], int (*matrix3)[MATRIX_COL]){
int **matrix = new int *[MATRIX_COL];
for(int i = 0; i < MATRIX_ROW; ++i){
for(int j = 0; j < MATRIX_COL; ++j){
*(*(matrix3 + i) + j) = matrix1[i][j] + matrix2[i][j];
}
}
};
void matrixMinus(int (*matrix1)[MATRIX_COL], int (*matrix2)[MATRIX_COL], int (*matrix3)[MATRIX_COL]){
int **matrix = new int *[MATRIX_COL];
for(int i = 0; i < MATRIX_ROW; ++i){
for(int j = 0; j < MATRIX_COL; ++j){
*(*(matrix3 + i) + j) = *(matrix1 + i)[j] - matrix2[i][j];;
}
}
};
int main(){
system("chcp 65001"); //解决输出中文乱码
// 数组指针
int (*matrix_A1)[MATRIX_COL] = new int [MATRIX_ROW][MATRIX_COL];
int (*matrix_A2)[MATRIX_COL] = new int [MATRIX_ROW][MATRIX_COL];
int (*matrix_A3)[MATRIX_COL] = new int [MATRIX_ROW][MATRIX_COL];
cout << "请输入矩阵A1(4×5)的元素:" << endl;
initMatrix(matrix_A1);
cout << "请输入矩阵A2(4×5)的元素:" << endl;
initMatrix(matrix_A2);
cout << "矩阵A1(4×5)如下所示:" << endl;
printMatrix(matrix_A1);
cout << "矩阵A1(4×5)如下所示:" << endl;
printMatrix(matrix_A2);
cout << "矩阵A1和矩阵A2相加结果:" << endl;
matrixPlus(matrix_A1, matrix_A2, matrix_A3);
printMatrix(matrix_A3);
cout << "矩阵A1和矩阵A2相减结果:" << endl;
matrixMinus(matrix_A1, matrix_A2, matrix_A3);
printMatrix(matrix_A3);
delete matrix_A1;
delete matrix_A2;
delete matrix_A3;
return 0;
}
2、类与对象实验
2.1编写C++程序完成“圆形”以下功能:
(1) 定义一个 Point 类,其属性包括点的坐标,提供计算两点之间距离的方法; (要求当用户不输入坐标数据时,能够默认为坐标原点(0,0))
(2) 定义一个圆形类,其属性包括圆心和半径;
(3) 创建两个圆形对象,提示用户输入圆心坐标和半径,判断两个圆是否相交, 并输出结果;
(4) 观察圆形对象以及 Point 类成员的构造函数与析构函数的调用。
(提示及要求:1,可通过在构造与析构函数中加入输出提示信息的方式观察 相关调用;可以使用 system("pause")进行程序的暂停;2,能够理解并说明每 一次构造与析构函数调用是哪个对象的调用,并观察和解释相关调用顺序及 其原因)
#include <iostream>
#include <math.h>
using namespace std;
class Point{
public:
Point();
Point(int x, int y);
Point(const Point& p);
~ Point();
double distance(Point p);
int getX();
int getY();
void setX(int x);
void setY(int y);
private:
int x;
int y;
};
Point::Point(){
x = 0;
y = 0;
cout << "调用无参构造函数Point()"<<endl;
// cout << "调用无参构造函数Point()"<< x << y <<endl;
system("pause");
}
Point::Point(int x, int y){
this->x = x;
this->y = y;
cout << "调用构造函数Point(double x, double y)"<<endl;
// cout << "调用构造函数Point(double x, double y)"<< x << y <<endl;
system("pause");
}
Point::Point(const Point& p){
x = p.x;
y = p.y;
cout << "调用拷贝构造函数Point(Point& p)" <<endl;
// cout << "调用拷贝构造函数Point(Point& p)" << x << y <<endl;
system("pause");
}
Point::~Point(){
cout << "调用析构函数~Point()" <<endl;
// cout << "调用析构函数~Point()" << x << y <<endl;
system("pause");
}
double Point::distance(Point p){
return sqrt( pow(p.x - x, 2) + pow(p.y - y, 2) );
}
int Point::getX(){
return x;
}
int Point::getY(){
return y;
}
void Point::setX(int x){
this->x = x;
}
void Point::setY(int y){
this->y = y;
}
class Circle{
public:
Circle();
Circle(Point center, int redius);
Circle(const Circle & c);
~Circle();
Point* getCenter();
int getRadius();
void setRadius(int radius);
private:
Point center;
int radius;
};
Circle::Circle(){
cout << "调用无参构造函数Circle()" <<endl;
center = Point();
// center = Point(1,1);
radius = 0;
system("pause");
}
Circle::Circle(Point center, int radius):center(center){
radius = radius;
cout << "调用构造函数Circle(Point center, double redius)" <<endl;
system("pause");
}
Circle::Circle(const Circle & c){
center = c.center;
radius = c.radius;
cout << "调用拷贝构造函数Circle(Circle & c)" <<endl;
system("pause");
}
Circle::~Circle(){
cout << "调用析构函数~Circle()" <<endl;
system("pause");
}
Point* Circle::getCenter(){
return ¢er;
}
int Circle::getRadius(){
return radius;
}
void Circle::setRadius(int radius){
this->radius = radius;
}
int main()
{
system("chcp 65001"); //解决输出中文乱码
cout<< "开始创建c1" <<endl;
Circle c1;
cout<< "开始创建c2" <<endl;
Circle c2;
Point *c1Center = c1.getCenter();
Point *c2Center = c2.getCenter();
int tempX, tempY, tempR;
cout<< "请输入c1的圆心和半径:" <<endl;
cin>> tempX >> tempY >> tempR;
(*c1Center).setX(tempX);
(*c1Center).setY(tempY);
c1.setRadius(tempR);
cout<< "请输入c2的圆心和半径:" <<endl;
cin>> tempX >> tempY >> tempR;
(*c2Center).setX(tempX);
(*c2Center).setY(tempY);
c2.setRadius(tempR);
bool intersect = (*c1Center).distance(*c2Center) <= c1.getRadius() + c2.getRadius();
if(intersect){
cout<<"两圆相交!"<<endl;
}else{
cout<<"两圆不相交!"<<endl;
}
return 0;
}
2.2 编写C++程序完成“矩阵”类以下功能:
(1) 用类来实现矩阵,定义一个矩阵的类,属性包括:
- 矩阵大小,用 lines, rows(行、列来表示);
- 存贮矩阵的数组指针,根据矩阵大小动态申请(new)。
(2) 矩阵类的方法包括:
- 构造函数,参数是矩阵大小,需要动态申请存贮矩阵的数组;
- 析构函数,需要释放矩阵的数组指针;
- 拷贝构造函数,需要申请和复制数组;
- 输入,可以从 cin 中输入矩阵元素;
- 输出,将矩阵格式化输出到 cout;
- 矩阵相加的函数,实现两个矩阵相加的功能,结果保存在另一个矩阵类, 但必须矩阵大小相同;
- 矩阵相减的函数,实现两个矩阵相减的功能,结果保存在另一个矩阵类, 但必须矩阵大小相同。
(3) 定义三个矩阵:A1、A2、A3;
(4) 初始化 A1、A2; 2
(5) 计算并输出 A3 = A1 加 A2,A3=A1 减 A2;(要求及提示:最好能实现对赋值 操作符“=”的重载;注意检查“自赋值”、释放“旧元素”)
(6) 用 new 动态创建三个矩阵类的对象:pA1、pA1、pA3;
(7) 初始化 pA1、pA2;
(8) 计算并输出 pA3=pA1 加 pA2,pA3=pA1 减 pA2;
(9) 释放 pA1、pA2、pA3。
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix{
public:
Matrix(int row, int col, int *matrix);
Matrix(Matrix& m);
Matrix operator=(const Matrix &m);
Matrix operator+(Matrix &m);
Matrix operator-(Matrix &m);
~ Matrix();
int getRow();
int getCol();
void initMatrix();
Matrix matrixPlus(Matrix m);
Matrix matrixMinus(Matrix m);
void printMatrix();
private:
int row;
int col;
int *matrix;
};
Matrix::Matrix(int row = 0, int col = 0, int *matrix = nullptr){
this->row = row;
this->col = col;
this->matrix = new int[row*col];
if(matrix != nullptr){
for(int i=0;i<row*col;++i){
this->matrix[i] = matrix[i];
}
}
}
Matrix::Matrix(Matrix& m){
row = m.row;
col = m.col;
matrix = new int[row*col];
for(int i=0;i<row*col;++i){
matrix[i] = m.matrix[i];
}
}
Matrix Matrix::operator=(const Matrix &m){
if(this == &m){
return *this; //自赋值
}
delete[] this->matrix; //释放“旧元素”
this->row = m.row;
this->col = m.col;
this->matrix = new int[this->row*this->col];
for(int i=0;i<row*col;++i){
matrix[i] = m.matrix[i];
}
return *this;
}
Matrix::~Matrix(){
delete[] matrix;
}
int Matrix::getRow(){
return row;
}
int Matrix::getCol(){
return col;
}
void Matrix::initMatrix(){
cout << "请输入矩阵的行数和列数:" << endl;
cin >> row;
cin >> col;
delete[] this->matrix;
matrix = new int[row*col];
cout << "请输入矩阵"<<row<<"×"<<col<<"的元素:" << endl;
for(int i=0;i<row*col;++i){
cin >> matrix[i];
}
}
Matrix Matrix::operator + (Matrix& m){
if(row != m.row || col != m.col){
cout<< "矩阵大小不相同,无法进行计算!" <<endl;
return m;
}
Matrix res(row,col);
for(int i=0;i<row*col;++i){
res.matrix[i] = matrix[i] + m.matrix[i];
}
return res;
}
Matrix Matrix::operator - (Matrix& m){
if(row != m.row || col != m.col){
cout<< "矩阵大小不相同,无法进行计算!" <<endl;
return m;
}
Matrix res(row,col);
for(int i=0;i<row*col;++i){
res.matrix[i] = matrix[i] - m.matrix[i];
}
return res;
}
void Matrix::printMatrix(){
for(int i = 0; i <row; ++i){
for(int j = 0; j < col; ++j){
cout << setw(5) << matrix[i*col+j]<< " ";
}
cout << endl;
}
};
int main()
{
system("chcp 65001"); //解决输出中文乱码
Matrix A1,A2,A3;
A1.initMatrix();//初始化
A2.initMatrix();
cout << "矩阵A1("<<A1.getRow()<<"×"<<A1.getCol()<<")如下所示:" << endl;
A1.printMatrix();
cout << "矩阵A2("<<A2.getRow()<<"×"<<A2.getCol()<<")如下所示:" << endl;
A2.printMatrix();
A3 = A1 + A2;
cout << "矩阵A1和矩阵A2相加结果:" << endl;
A3.printMatrix();
A3 = A1 - A2;
cout << "矩阵A1和矩阵A2相减结果:" << endl;
A3.printMatrix();
Matrix* pA1 = new Matrix();
Matrix* pA2 = new Matrix();
Matrix* pA3 = new Matrix();
(*pA1).initMatrix();
(*pA2).initMatrix();
cout << "矩阵pA1("<<(*pA1).getRow()<<"×"<<(*pA1).getCol()<<")如下所示:" << endl;
(*pA1).printMatrix();
cout << "矩阵pA2("<<(*pA2).getRow()<<"×"<<(*pA2).getCol()<<")如下所示:" << endl;
(*pA2).printMatrix();
(*pA3)=(*pA1)+(*pA2);
cout << "矩阵pA1和矩阵pA2相加结果:" << endl;
(*pA3).printMatrix();
(*pA3)=(*pA1)-(*pA2);
cout << "矩阵pA1和矩阵pA2相减结果:" << endl;
(*pA3).printMatrix();
delete pA1;
delete pA2;
delete pA3;
return 0;
}
3、继承与派生实验
编写C++程序完成“形状”的以下功能:
(1) 声明一个基类 Shape(形状),其中包含一个方法来计算面积;
(2) 从 Shape 派生两个类:矩形类和圆形类;
(3) 从矩形类派生正方形类;
(4) 分别实现派生类构造函数、析构函数和其他方法;
(5) 创建派生类的对象,观察构造函数、析构函数调用次序;
(提示及要求:1,可通过在构造与析构函数中加入输出提示信息的方式观察 相关调用;可以使用 system("pause")进行程序的暂停;2,能够理解并说明每 一次构造与析构函数调用是哪个对象的调用,并观察和解释相关调用顺序及 其原因)
(6) 对不同对象计算面积。
#include <iostream>
using namespace std;
# define PI 3.14159
class Shape{
public:
Shape();
double area();
};
Shape::Shape(){
}
double Shape::area(){
cout<<"调用基函数的面积计算方法!"<<endl;
return 0;
}
class Rectangle : public Shape{
public:
Rectangle();
Rectangle(int l, int w);
~Rectangle();
double area();
private:
int length;
int width;
};
Rectangle::Rectangle(){
cout << "调用无参构造函数Rectangle()"<<endl;
system("pause");
}
Rectangle::Rectangle(int l, int w){
length = l;
width = w;
cout << "调用构造函数Rectangle(int l, int w)"<<endl;
system("pause");
}
Rectangle::~Rectangle(){
cout << "调用析构函数~Rectangle()" <<endl;
system("pause");
}
double Rectangle::area(){
cout << "长方形的长和宽分别为:" << length << "," << width << "。" <<endl;
cout << "长方形的面积为:" << length * width <<endl;
return length * width;
}
class Circle : public Shape{
public:
Circle();
Circle(int r);
~Circle();
double area();
private:
int radius;
};
Circle::Circle(){
cout << "调用无参构造函数Circle()"<<endl;
system("pause");
}
Circle::Circle(int r){
radius = r;
cout << "调用构造函数Circle(int r)"<<endl;
system("pause");
}
Circle::~Circle(){
cout << "调用析构函数~Circle()" <<endl;
system("pause");
}
double Circle::area(){
cout << "圆形的半径为:" << radius <<endl;
cout << "圆形的面积为:" << PI * radius * radius <<endl;
return PI * radius * radius;
}
class Square : public Rectangle{
public:
Square();
Square(int l);
~Square();
double area();
private:
int length;
};
Square::Square(){
cout << "调用无参构造函数Square()"<<endl;
system("pause");
}
Square::Square(int l){
length = l;
cout << "调用构造函数Square(int l)"<<endl;
system("pause");
}
Square::~Square(){
cout << "调用析构函数~Square()" <<endl;
system("pause");
}
double Square::area(){
cout << "正方形的边长为:" << length <<endl;
cout << "正方形的面积为:" << length * length <<endl;
return length * length;
}
int main(){
system("chcp 65001"); //解决输出中文乱码
Rectangle r(10,5);
Circle c(5);
Square s(7);
r.area();
c.area();
s.area();
return 0;
}
4、I/O 流实验
编写 C++程序完成猜价格游戏的以下功能:
(1) 假定有一件商品,程序用随机数指定该商品的价格(1-1000 的整数);
(2) 提示用户猜价格,并输入:若用户猜的价格比商品价格高或低,对用户作出 相应的提示;
(3) 直到猜对为止,并给出提示。
(提示及要求:1,要求使用 C++的输入输出方式(cin, cout),不能使用 C 语 言的 printf 等;2,注意检查输入的合法性)
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
system("chcp 65001"); //解决输出中文乱码
srand(time(0));
int price = rand()%1000 + 1;
int guess = 0;
int low = 1;
int high = 1000;
while(true){
cout << "请输入猜测的价格(" << low << "~" << high <<"):" <<endl;
cin >> guess;
if(guess > price){
cout << "猜测的价格高了!" <<endl;
high = guess;
}else if (guess < price)
{
cout << "猜测的价格低了!" <<endl;
low = guess;
}else{
cout << "猜对了!" <<endl;
break;
}
}
system("pause");
return 0;
}
5、重载实验
5.1 虚函数
针对题目3的“形状”类,编写C++程序完成以下功能:
(1) 将【形状】 中的基类计算面积的方法定义为虚函数,比较与【形状(A)】程 序的差异;
#include <iostream>
using namespace std;
# define PI 3.14159
class Shape{
public:
Shape();
virtual double area();
};
Shape::Shape(){
}
double Shape::area(){
cout<<"调用基函数的面积计算方法!"<<endl;
return 0;
}
class Rectangle : public Shape{
public:
Rectangle();
Rectangle(int l, int w);
~Rectangle();
virtual double area();
private:
int length;
int width;
};
Rectangle::Rectangle(){
cout << "调用无参构造函数Rectangle()"<<endl;
system("pause");
}
Rectangle::Rectangle(int l, int w){
length = l;
width = w;
cout << "调用构造函数Rectangle(int l, int w)"<<endl;
system("pause");
}
Rectangle::~Rectangle(){
cout << "调用析构函数~Rectangle()" <<endl;
system("pause");
}
double Rectangle::area(){
cout << "长方形的长和宽分别为:" << length << "," << width << "。" <<endl;
cout << "长方形的面积为:" << length * width <<endl;
return length * width;
}
class Circle : public Shape{
public:
Circle();
Circle(int r);
~Circle();
virtual double area();
private:
int radius;
};
Circle::Circle(){
cout << "调用无参构造函数Circle()"<<endl;
system("pause");
}
Circle::Circle(int r){
radius = r;
cout << "调用构造函数Circle(int r)"<<endl;
system("pause");
}
Circle::~Circle(){
cout << "调用析构函数~Circle()" <<endl;
system("pause");
}
double Circle::area(){
cout << "圆形的半径为:" << radius <<endl;
cout << "圆形的面积为:" << PI * radius * radius <<endl;
return PI * radius * radius;
}
class Square : public Rectangle{
public:
Square();
Square(int l);
~Square();
virtual double area();
private:
int length;
};
Square::Square(){
cout << "调用无参构造函数Square()"<<endl;
system("pause");
}
Square::Square(int l){
length = l;
cout << "调用构造函数Square(int l)"<<endl;
system("pause");
}
Square::~Square(){
cout << "调用析构函数~Square()" <<endl;
system("pause");
}
double Square::area(){
cout << "正方形的边长为:" << length <<endl;
cout << "正方形的面积为:" << length * length <<endl;
return length * length;
}
int main(){
system("chcp 65001"); //解决输出中文乱码
Rectangle r(10,5);
Circle c(5);
Square s(7);
// Shape shape; //不是抽象类,只实现了虚函数,可以创建对象。
Shape &sh = s;
sh.area(); //此处Shape的对象sh调用的是Square类中的area(),实现了动态联编
r.area();
c.area();
s.area();
return 0;
}
(2) 将【形状】中的基类定义为抽象类,比较与【形状(A)】程序的差异。
#include <iostream>
using namespace std;
# define PI 3.14159
class Shape{
public:
virtual double area()=0;
};
class Rectangle : public Shape{
public:
Rectangle();
Rectangle(int l, int w);
~Rectangle();
double area();
private:
int length;
int width;
};
Rectangle::Rectangle(){
cout << "调用无参构造函数Rectangle()"<<endl;
system("pause");
}
Rectangle::Rectangle(int l, int w){
length = l;
width = w;
cout << "调用构造函数Rectangle(int l, int w)"<<endl;
system("pause");
}
Rectangle::~Rectangle(){
cout << "调用析构函数~Rectangle()" <<endl;
system("pause");
}
double Rectangle::area(){
cout << "长方形的长和宽分别为:" << length << "," << width << "。" <<endl;
cout << "长方形的面积为:" << length * width <<endl;
return length * width;
}
class Circle : public Shape{
public:
Circle();
Circle(int r);
~Circle();
double area();
private:
int radius;
};
Circle::Circle(){
cout << "调用无参构造函数Circle()"<<endl;
system("pause");
}
Circle::Circle(int r){
radius = r;
cout << "调用构造函数Circle(int r)"<<endl;
system("pause");
}
Circle::~Circle(){
cout << "调用析构函数~Circle()" <<endl;
system("pause");
}
double Circle::area(){
cout << "圆形的半径为:" << radius <<endl;
cout << "圆形的面积为:" << PI * radius * radius <<endl;
return PI * radius * radius;
}
class Square : public Rectangle{
public:
Square();
Square(int l);
~Square();
double area();
private:
int length;
};
Square::Square(){
cout << "调用无参构造函数Square()"<<endl;
system("pause");
}
Square::Square(int l){
length = l;
cout << "调用构造函数Square(int l)"<<endl;
system("pause");
}
Square::~Square(){
cout << "调用析构函数~Square()" <<endl;
system("pause");
}
double Square::area(){
cout << "正方形的边长为:" << length <<endl;
cout << "正方形的面积为:" << length * length <<endl;
return length * length;
}
int main(){
system("chcp 65001"); //解决输出中文乱码
Rectangle r(10,5);
Circle c(5);
Square s(7);
// Shape shape; //抽象类不可以实例化, 编译不通过,会报错。
Shape &sh = s; //抽象类不可以实例化,但可以创建指针或引用。抽象类的引用可以访问派生类的虚函数
sh.area();
r.area();
c.area();
s.area();
return 0;
}
5.2 对Point类重载++和--运算符
编写C++程序完成以下功能:
(1) Point 类的属性包括点的坐标(x,y);
(2) 实现 Point 类重载++和--运算符:
- ++p,--p,p++,p--;
- ++和--分别表示 x,y 增加或减少 1
#include <iostream>
#include <math.h>
using namespace std;
class Point{
public:
Point();
Point(int x, int y);
Point(const Point& p);
Point operator ++(); //前置“++”重载,++point
Point operator ++(int); //后置“++”重载,point++
Point operator --(); //前置“--”重载,--point
Point operator --(int); //后置“--”重载,point--
void print();
private:
int x;
int y;
};
Point::Point(){
x = 0;
y = 0;
}
Point::Point(int x, int y){
this->x = x;
this->y = y;
}
Point::Point(const Point& p){
x = p.x;
y = p.y;
}
Point Point::operator ++(){
x++;
y++;
return *this;
}
Point Point::operator ++(int){
Point temp(*this);
x++;
y++;
return temp;
}
Point Point::operator --(){
x--;
y--;
return *this;
}
Point Point::operator --(int){
Point temp(*this);
x--;
y--;
return temp;
}
void Point::print(){
cout<<"圆心:("<< x <<","<< y << ")" <<endl;
}
int main()
{
system("chcp 65001"); //解决输出中文乱码
Point p(5,6);
Point p2;
cout<<"p的";
p.print();
p2 = ++p;
cout<<"++p的";
p.print();
cout<<"p2 = ++p的";
p2.print();
p2 = p++;
cout<<"p++的";
p.print();
cout<<"p2 = p++的";
p2.print();
p2 = --p;
cout<<"--p的";
p.print();
cout<<"p2 = --p的";
p2.print();
p2 = p--;
cout<<"p--的";
p.print();
cout<<"p2 = p--的";
p2.print();
return 0;
}