当单一变量无法完成描述需求的时候,结构体类型解决了这一问题。可以将多个类型打包成一体,形成新的类型。这是 c 语言中封装的概念
对C语言中结构体的操作,都是通过外部函数来实现的。 一般在描述数据成员时,使用结构体,当既有数据成员又有数据函数时,使用类class
struct 中所有行为和属性都是 public 的(默认)。 C++中的 class 可以 指定 行为和属性的访问方式。 默认private
封装,可以达到,对内开放数据,对外屏蔽数据,对外提供接口。
protected 保护控制权限 在类的继承中跟private有区别,在单类中,跟private是一样的 public 公有 修饰成员变量和成员函数在类内类外都可以访问 private 私有 修饰成员变量和成员函数只能在类内访问
封装有2层含义(把属性和方法进行封装 对属性和方法进行访问控制)
面向过程:就是分析出解决问题所需要的步骤,然后把这些步骤一步一步实现,使用时,依次调用。
面向对象:是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描述某些事物在在整个解决问题的步骤中的行为。
当类定义一个对象时,内存会开辟空间,在未初始化之前,内存中存储的是一个随机数,需要通过成员函数为 变量 赋值 进行初始化。
同类对象之间无私处
类内声明,类外定义实现,要注意加作用域
例:求圆的周长和面积 用结构化方法编程
#include<iostream> using namespace std; int main (void) { double r, girth,area ; const double PI = 3.1415; cout << "Please input radius:\n"; //操作符重载 cin >> r ; //输⼊ girth = 2 * PI * r; area = PI * r * r; cout << "radius = "<< r << endl; cout << "girth = " << girth << endl; cout << "area = " << area << endl; return 0; }用面向对象方法
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //圆的周长 double getCircleGirth(double r) { return 2 * 3.14*r; } //圆的面积 double getCircleArea(double r) { return 3.14*r*r; } //用面向对象实现 //圆类 class Circle { public: void setR(double r) { m_r = r; } double getR() { return m_r; } double getGirth() { return 2 * 3.14 *m_r; } double getArea() { return m_r*m_r*3.14; } private: double m_r; //圆的私有成员 半径 }; class Circle2 { public: void setR(double r) { m_r = r; } double getR() { return m_r; } double getArea() { m_area = m_r*m_r*3.14; return m_area; } double getGirth() { m_girth = m_r * 2 * 3.14; return m_girth; } private: double m_r; double m_girth; //周长 double m_area;//面积 }; int main(void) { double r = 10; //圆的半径 double g = 0; double a = 0; g = getCircleGirth(r); a = getCircleArea(r); cout << "圆的半径是" << r << endl; cout << "圆的周长是" << g << endl; cout << "圆的面积是" << a << endl; cout << "------" << endl; Circle c; c.setR(10); cout << "圆的半径是" << c.getR() << endl; cout << "圆的周长是" << c.getGirth() << endl; cout << "圆的面积是" << c.getArea() << endl; cout << "------------" << endl; Circle2 c2; c2.setR(10); cout << "圆的半径是" << c2.getR() << endl; cout << "圆的周长是" << c2.getGirth() << endl; cout << "圆的面积是" << c2.getArea() << endl; return 0; }例:设计立方体类,求出立方体的面积和体积 求两个立方体是否相等(全局函数和成员函数)
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //立方体类 class Cube { public: void setABC(int a, int b, int c) { m_a = a; m_b = b; m_c = c; } int getArea() { return (m_a*m_b) * 2 + (m_a*m_c) * 2 + (m_b*m_c) * 2; } int getVolume() { return (m_a*m_b*m_c); } int getA() { return m_a; } int getB() { return m_b; } int getC() { return m_c; } //同类之间无私处 类内成员函数 bool judgeCube(Cube &another) { if (m_a == another.m_a && m_b == another.getB() && m_c == another.getC()) { return true; } else { return false; } } private: int m_a; int m_b; int m_c; }; //全局函数 类外函数 bool judgeCube(Cube &c1, Cube &c2) { if (c1.getA() == c2.getA() && c1.getB() == c2.getB() && c1.getC() == c2.getC()) { return true; } else { return false; } } int main(void) { Cube c1; c1.setABC(10, 20, 30); Cube c2; c2.setABC(10, 20, 30); cout << "c1 的体积是" << c1.getVolume() << endl; cout << "c1 的面积是" << c1.getArea() << endl; //调用全局函数 if (judgeCube(c1, c2) == true) { cout << "相等" << endl; } else { cout << "不相等" << endl; } cout << " ------ " << endl; //调用类内函数 if (c1.judgeCube(c2) == true) { cout << "相等" << endl; } else { cout << "不相等" << endl; } return 0; }例:求点和圆的关系(圆内和圆外)
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //点类 class Point { public: void setXY(int x, int y) { m_x = x; m_y = y; } int getX() { return m_x; } int getY() { return m_y; } private: int m_x; int m_y; }; //圆类 class Circle { public: void setXY(int x, int y) { x0 = x; y0 = y; } void setR(int r) { m_r = r; } //提供一个判断点是否在圆内 //true 在内部 //false 在外部 bool judgePoint(Point &p) { int dd; dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0); if (dd > m_r*m_r) { return false; } else { return true; } } private: int x0; int y0; int m_r; }; int main(void) { Circle c; c.setXY(2, 2); c.setR(4); Point p; p.setXY(8, 8); if (c.judgePoint(p) == true) { cout << "圆的内部" << endl; } else { cout << "圆的外部" << endl; } return 0; }定义一个点类,其属性包括点的坐标,提供计算两点之间距离的方法 定义一个圆形类,其属性包括圆心和半径 判断两个圆是否相交
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cmath> using namespace std; //点类 class Point { public: void setXY(int x, int y) { m_x = x; m_y = y; } //计算两点距离的方法 double pointDistance(Point &another) { int d_x = m_x - another.m_x; int d_y = m_y - another.m_y; double dis = sqrt(d_x*d_x + d_y*d_y); return dis; } private: int m_x; int m_y; }; //圆类 class Circle { public: void setR(int r) { m_r = r; } void setXY(int x, int y) { p0.setXY(x, y); } //判断圆是否跟我相交 bool isIntersection(Circle &another) { //两个半径之和 int rr = m_r + another.m_r; //两圆心之间距离 double dis = p0.pointDistance(another.p0); if (dis <= rr) { //相交 return true; } else { return false; } } private: int m_r; Point p0; }; int main(void) { Circle c1, c2; int x, y, r; cout << "请输入第一个圆的半径" << endl; cin >> r; c1.setR(r); cout << "请输入第一个圆的x" << endl; cin >> x; cout << "请输入第一个圆的y" << endl; cin >> y; c1.setXY(x, y); cout << "请输入第2个圆的半径" << endl; cin >> r; c2.setR(r); cout << "请输入第2个圆的x" << endl; cin >> x; cout << "请输入第2个圆的y" << endl; cin >> y; c2.setXY(x, y); if (c1.isIntersection(c2) == true) { cout << "相交" << endl; } else { cout << "不想交" << endl; } return 0; }定义一个Tree类,有成员ages(树龄),成员函数grow(int years)对ages加上years,age()显示tree对象的ages的值
#include<iostream> using namespace std; class Tree { public: //类内函数 int grow(int years) { cout << "输入树龄:" << endl; cin >> ages; ages = ages + years; return ages; } void showage() { cout << "该树的年龄是" << ages << endl; } #if 0 int grow(int years); void showage(); #endif private: int ages; }; //类外函数 #if 0 int Tree::grow(int years) { cout << "输入树龄:" << endl; cin >> ages; ages = ages + years; return ages; } void Tree::showage() { cout << "该树的年龄是" << ages << endl; } #endif int main() { Tree ages; ages.grow(9); ages.showage(); return 0; }