二、面向对象程序设计案例
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> #include<string> using namespace std; /* 求出立方体的面积 分别用全局函数和成员函数判断两个立方体是否相等 */ class Cube { public: // 设置长 void setLen(int l) { len = l; } // 获取长 int getLen() { return len; } // 设置宽 void setWide(int w) { wide = w; } // 获取宽 int getWide() { return wide; } // 设置高 void setHeigh(int h) { heigh = h; } // 获取高 int getHeigh() { return heigh; } // 求立方体的面积 void getCubeS() { cout << "立方体的面积是: " << 2*(len*heigh + len*wide + heigh*wide) << endl; } // 求立方体的体积 void getCubeV() { cout << "立方体的体积是: " << len * wide *heigh << endl; } // 通过成员函数来判断两个立方体是否相等 bool compareCubeByClass(Cube &cub) { bool ret = len == cub.getLen() && wide == cub.getWide() && heigh == cub.getHeigh(); return ret; } private: int len; int wide; int heigh; }; // 全局函数判断两个立方体是否相等 bool compareCube(Cube &cub1, Cube &cub2) { if (cub1.getLen() == cub2.getLen() && cub1.getWide() == cub2.getWide() && cub1.getHeigh() == cub2.getHeigh()) { return true; } return false; } void test01() { Cube c1; c1.setLen(10); c1.setWide(10); c1.setHeigh(10); c1.getCubeS(); c1.getCubeV(); // 通过全局函数判断两个立方体是否相等 Cube c2; c2.setLen(10); c2.setWide(10); c2.setHeigh(10); bool ret = compareCube(c1, c2); if (ret) { cout << "两个立方体是相等的" << endl; } else { cout << "两个立方体是不相等的" << endl; } // 通过成员函数来判断两个立方体是否相等 ret = c1.compareCubeByClass(c2); if (ret) { cout << "两个立方体是相等的" << endl; } else { cout << "两个立方体是不相等的" << endl; } } int main() { test01(); return EXIT_SUCCESS; }
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include "point.h" #include<iostream> using namespace std; // 圆类 class Circle { public: void setR(int r); /*{ R = r; }*/ int getR(); /*{ return R; }*/ // 设置圆心 void setCenter(Point p); /*{ Center = p; }*/ // 获取圆心 Point getCenter(); /*{ return Center; }*/ // 利用成员函数判断点和圆心 void isInCircleByClass(Point &p); /*{ int distance = (Center.getX() - p.getX()) * (Center.getX() - p.getX()) + (Center.getY() - p.getY())* (Center.getY() - p.getY()); int rDistance = R * R; if (distance == rDistance) { cout << "点在圆上" << endl; } else if (distance < rDistance) { cout << "点在圆内" << endl; } else { cout << "点在圆外" << endl; } }*/ private: int R; // 半径 Point Center; // 圆心 };
#include "circle.h" #include "point.h" void Circle::setR(int r) { R = r; } int Circle::getR() { return R; } // 设置圆心 void Circle::setCenter(Point p) { Center = p; } // 获取圆心 Point Circle::getCenter() { return Center; } // 利用成员函数判断点和圆心 void Circle::isInCircleByClass(Point &p) { int distance = (Center.getX() - p.getX()) * (Center.getX() - p.getX()) + (Center.getY() - p.getY())* (Center.getY() - p.getY()); int rDistance = R * R; if (distance == rDistance) { cout << "点在圆上" << endl; } else if (distance < rDistance) { cout << "点在圆内" << endl; } else { cout << "点在圆外" << endl; } }
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; // 点 类 class Point { public: void setX(int a); /*{ X = a; }*/ void setY(int b); /*{ Y = b; }*/ int getX(); /*{ return X; }*/ int getY(); /*{ return Y; }*/ private: int X; int Y; };
#include "point.h" void Point::setX(int a) { X = a; } void Point::setY(int b) { Y = b; } int Point::getX() { return X; } int Point::getY() { return Y; }
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> #include "circle.h" #include "point.h" using namespace std; /* 设计一个圆形类 (AdvCircle), 和一个类 (Point), 计算点和圆的关系 假如圆心坐标为 (x0, y0), 半径为 r, 点的坐标为 (x1, y1) 1) 点在圆上:(x1-x0)*(x1-x0) +(y1-y0)*(y1-y0) == r*r 2) 点在圆内:(x1-x0)*(x1-x0) +(y1-y0)*(y1-y0) < r*r 3) 点在圆外:(x1-x0)*(x1-x0) +(y1-y0)*(y1-y0) > r*r */ // 利用全局函数来判断点和圆的关系 void isInCircle(Circle &c, Point &p) { // 获取圆心和点的距离的平方 int distance = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) + (c.getCenter().getY() - p.getY())* (c.getCenter().getY() - p.getY()); int rDistance = c.getR() * c.getR(); if (distance == rDistance) { cout << "点在圆上" << endl; } else if (distance < rDistance) { cout << "点在圆内" << endl; } else { cout << "点在圆外" << endl; } } void test01() { Point p1; p1.setX(10); p1.setY(10); Circle c1; Point center; center.setX(10); center.setY(0); c1.setCenter(center); c1.setR(10); // 全局函数判断 isInCircle(c1, p1); // 利用成员函数来判断 c1.isInCircleByClass(p1); } int main() { test01(); return EXIT_SUCCESS; }
#include "MyArray.h" void test01() { // 堆区创建数组 MyArray * array = new MyArray(30); MyArray * array2 = new MyArray(*array); // new方式指定调用拷贝构造 MyArray array3 = *array; // 构造函数返回的本体 MyArray * array4 = array; // 这是一个指针, 和array的地址相同,所以不会调用拷贝构造 delete array; // 尾插法的测试 for (int i = 0; i < 10; i++) { array2->pushBack(i); } // 获取值的测试 for (int i = 0; i < 10; i++) { cout << array2->getData(i) << endl; } // 设置值的测试 array2->setData(0, 100); cout << "array2 的第一个元素是: " << array2->getData(0) << endl; // 获取数据大小测试 cout << "array2的数组大小是 " << array2->getSize() << endl; // 获取数据容量测试 cout << "array2的数组容量是 " << array2->getCapacity() << endl; array3.pushBack(1000); cout << array3.getData(0) << endl; // [] 重载过后获取值 cout << "array3 的第一个元素是: " << array3[0] << endl; array3[0] = 100; cout << "array3 的第一个元素是: " << array3.getData(0) << endl; delete array2; } int main() { test01(); return 0; }
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> #include<string> using namespace std; class MyArray { public: MyArray(); MyArray(int capacity); MyArray(const MyArray & array); ~MyArray(); // 尾插发 void pushBack(int val); // 根据索引获取值 int getData(int index); // 根据索引设置值 void setData(int index, int value); // 获取数组的大小 int getSize(); // 获取数组的容量 int getCapacity(); // [] 运算符重载, 用来模拟[]取值,取代int getData(int index);的功能 int& operator[](int index); private: int *pAddress; // 指向真正存储数据的指针 int Size; //数组的大小 int Capacity; };
#include "MyArray.h" // 默认构造 MyArray::MyArray() { this->Capacity = 100; this->Size = 0; this->pAddress = new int[this->Capacity]; } // 有参构造 MyArray::MyArray(int capacity) { cout << "有参构造调用" << endl; this->Capacity = capacity; this->Size = 0; this->pAddress = new int[this->Capacity]; } // 拷贝构造 MyArray::MyArray(const MyArray& array) { cout << "拷贝构造调用" << endl; this->pAddress = new int[array.Capacity]; this->Size = array.Size; this->Capacity = array.Capacity; for (int i = 0; i < array.Size; i++) { this->pAddress[i] = array.pAddress[i]; } } // 析构函数 MyArray::~MyArray() { if (this->pAddress != NULL) { cout << "析构函数调用" << endl; delete[] this->pAddress; this->pAddress = NULL; } } // 尾插值 void MyArray::pushBack(int val) { // 判断越界不需要,用户自己处理 this->pAddress[this->Size] = val; this->Size++; } // 根据索引获取值 int MyArray::getData(int index) { return this->pAddress[index]; } // 根据索引设置值 void MyArray::setData(int index, int val) { this->pAddress[index] = val; } //获取数组的大小 int MyArray::getSize() { return this->Size; } // 获取数组的容量 int MyArray::getCapacity() { return this->Capacity; } // [] 重载 int& MyArray::operator[](int index) { return this->pAddress[index]; }