基于数组构造的程序设计
1、调用相关函数实现将一组double型数据的小数部分之和输出到屏幕上。
#include<iostream> using namespace std; #define M 5 void PrintData(double sum); void ReadData(double tt[M]); double Acc(int k, double dd[]) {//实现计算数组元素的小数部分之和 //写入你的代码 double sm = 0.0; for (int k = 0; k < M; k++) { sm += dd[k] - (int)dd[k]; } return sm; } void PrintData(double sum) { //把sum数据输出到屏幕上 //写入你的代码 cout << sum; } void ReadData(double tt[M]) { //从键盘中输入5个double数据存于tt数据中 //写入你的代码 for (int i = 0; i < M; i++) { cin >> tt[i]; } } void main() { int m = 0; double sm = 0.0; double tt[M]; system("cls");//清除屏幕数据 ReadData(tt); //读取数据 sm = Acc(m, tt); //此处添加求小数部分之和的函数调用语句 PrintData(sm); //结果显示在屏幕上 system("pause"); }
2、(1)对象数组的使用
对象数组的声明:类名 数组名[常量表达式]
Point p[2]; //定义了2个Point类型的对象(由C++运行时系统自动创建)
或者
Point p[2]={Point(),Point(1,2)};
对象数组的访问:数组名[下标].成员名
p[1].move(1,2);
#include <iostream> using namespace std; class Point {//定义Point 类 public: Point(); Point(int xx, int yy); ~Point(); void Move(int x, int y); int GetX() { return X; } int GetY() { return Y; } private: int X, Y; }; Point::Point() { X = Y = 0; cout << "Default Constructor called." << endl; } Point::Point(int xx, int yy) { X = xx; Y = yy; cout << "Constructor called." << endl; } Point ::~Point() { cout << "Destructor called." << endl; } void Point::Move(int x, int y) { cout << "moving the point to(" << x << "," << y << ")" << endl; X = x; Y = y; } int main() { int i; cout << "Entering main..." << endl; Point A[2]; //定义Point类型的对象数组 for (i = 0; i < 2; i++) A[i].Move(i + 10, i + 20);//通过下标使用对象数组元素 cout << "print A..." << endl; Point B[2] = { Point(),Point(1,2) }; //定义时初始化(注意无参对象的写法) for (i = 0; i < 2; i++) B[i].Move(i + 10, i + 20); cout << "Exiting main..." << endl; return 0; }
注:(重点)
对象数组
Point A[2];//定义Point类型的对象数组 for (int i = 0; i < 2; i++) { A[i].Move(i + 10, i + 20);//通过下标使用对象数组元素 } Point B[2] = { Point(),Point(1,2) };//定义时初始化(注意无参对象的写法) for (int i = 0; i < 2; i++) { B[i].Move(i + 10, i + 20); }
注意哪种情况调用无参数构造函数,哪种情况调用有参数构造函数;以及析构函数的调用时间。
3、定义一个圆类Circle,具有数据成员r;有参和无参构造函数;成员函数float area()求面积。主函数中定义1个Circle类型具有2个元素的对象数组,并调用相关方法求每个对象的面积。
#include<iostream> using namespace std; #define PI 3.1415 class Circle { public: Circle(); Circle(double radius); float area(); private: double r; }; Circle::Circle() { r = 0; cout << "调用无参数构造函数" << endl; } Circle::Circle(double radius) { r = radius; cout << "调用有参数构造函数" << endl; } float Circle::area() { return (r * r * PI); } int main() { Circle c[2] = { 1,2 }; for (int i = 0; i < 2; i++) cout << c[i].area() << " " << endl; Circle c1[2]; for (int i = 0; i < 2; i++) cout << c1[i].area() << " " << endl; return 0; }
4、观察运算符new和delete的用法,仿照例子利用动态申请内存方式完成后面的编程题。
#include<iostream> using namespace std; class Point { public: Point() { X=Y=0; cout<<"Default Constructor called.\n"; } Point(int xx,int yy) { X=xx; Y=yy; cout<< "Constructor called.\n"; } ~Point() { cout<<"Destructor called.\n"; } int GetX(){ return X; } int GetY(){ return Y; } void Move ( int x, int y ) { X=x; Y=y; } private: int X,Y; }; int main() { cout<<"Step One:"<<endl; Point *Ptr1=new Point();//new 类名:调用无参构造函数 delete Ptr1; cout<<"Step Two:"<<endl; Ptr1=new Point(1,2);//new 类名(实参):调用有参构造函数 Cout<<Ptr1->GetX()<<endl; //通过指针访问公有成员 delete Ptr1; return 0; }
注意:
new和delete的用法: 动态申请内存创建对象
类型名 *ptr=new 类型名T(初值列表)
释放对象空间:
delete 对象指针;
编程题:仿照以上的new和delete的使用,设计长方形类Crectangle,包含数据成员:length、width,分别通过构造函数赋值;成员函数void setvalue(int x,int y)分别对成员length、width赋值;float area()计算面积。要求:用动态分配方式创建对象,用指针调用成员函数
注意:一定要用delete释放对象
#include<iostream> using namespace std; class Crectangle { int length, width; public: Crectangle(int l, int w) { length = l; width = w; } Crectangle() {} void setvalue(int x, int y); int area() { return length * width; } }; void Crectangle::setvalue(int x, int y) { length = x; width = y; } int main() { Crectangle* Ptr1 = new Crectangle(); delete Ptr1; Ptr1 = new Crectangle(6, 2);//new 类名(实参):调用有参构造函数 cout << "面积为" << Ptr1->area() << endl; Ptr1->setvalue(2, 4); cout << "面积为" << Ptr1->area() << endl; delete Ptr1; system("pause"); }
重点:用动态分配方式创建对象,用指针调用成员函数:
Crectangle* ptr1 = new Crectangle(); delete ptr1; ptr1 = new Crectangle(6, 2);//new 类名(实参):调用有参构造函数 cout << "面积为:" << ptr1->area() << endl; ptr1->setvalue(2, 4); cout << "面积为:" << ptr1->area() << endl; delete ptr1;
5、定义ArrayData类,包含数据成员:整型量count、整型指针arr,分别记录数组元素的个数、保存数组指针;成员函数setdata(),对count赋值、用new动态创建数组,并把指针赋于arr;int sum(),返回数组元素之和;void sort(),对数组排序。
#include <iostream> using namespace std; class ArrayData { private: int count; int* arr; public: void setdata(int ar[], int m) { int i; count = m; arr = new int[count]; for (i = 0; i < count; i++) arr[i] = ar[i]; } int sum() { //补充代码 int sm = 0; for (int i = 0; i < 5; i++) { sm += arr[i]; } cout << "sum is " << sm << endl; return sm; } void sort() { //补充代码 for (int i = 0; i < 5; i++) { int tem = 0; for (int j = i + 1; j < 5; j++) { if (arr[i] > arr[j]) { tem = arr[j]; arr[j] = arr[i]; arr[i] = tem; } } } } void output() { for (int i = 0; i < count; i++) cout << arr[i] << " "; } ~ArrayData() { delete[] arr; } }; void main() { int a[5], i; ArrayData X; cout << "input data" << endl; for (i = 0; i < 5; i++) cin >> a[i]; X.setdata(a, 5); X.sum(); X.sort(); X.output(); system("pause"); }
▲ 在析构函数中要使用delete[ ] arr;(注意中括号位置)
▲ 如果不用析构函数释放动态分配的数组,动态分配的数组在程序结束后,会被释放吗?
不会。在C++中一般用new/delete来做。new/delete与malloc/free的关键区别在于:new/delete可以用于对象(当然也可以用于int/float/char之类的基本类型) new会申请内存并自动调用构造函数,delete会自动调用析构函数。如果不用析构函数,那么动态分配的数组在程序结束后不会被释放。