C++学习 --- 类和对象之封装
一、封装
#include <iostream> using namespace std; const double PI = 3.14; //设计一个圆类,求圆的周长 //圆周长的公式:2*PI*半径 //class 代表设计一个类 类后面紧跟着的就是类名 class Circle { //访问权限 public: //属性 int m_r; //行为 //获取圆的周长 double calculatePeri() { return 2 * PI*m_r; } }; int main() { //通过圆类,创建具体的圆(对象),实例化(通过一个类,创建一个对象的过程) Circle c1; //给圆对象的属性进行赋值 c1.m_r = 10; //2*PI*10 = 62.8 cout << "圆的周长为:" << c1.calculatePeri() << endl; system("pause"); return 0; }
学生类
#include <iostream> #include <string> using namespace std; //设计一个学生类,属性有姓名和学号 //可以给姓名和学号赋值,可以显示学生的姓名和学号 //设计学生类 //类中的属性和行为,统一称为成员 class Student { //访问权限 public: //属性:成员属性,成员变量 string m_Name; //姓名 int m_Id; //学号 void setName(string name) { m_Name = name; } void setNo(int num) { m_Id = num; } //行为:成员函数,成员方法 //显示姓名和学号 void showStudent() { cout << "姓名:" << m_Name << "\t学号:" << m_Id << endl; } }; int main() { //创建一个具体学生,实例化对象 Student s1; //给s1对象 进行属性赋值操作 //s1.m_Name = "张三"; s1.setName("李四"); //s1.m_Id = 1; s1.setNo(1); //显示学生信息 s1.showStudent(); system("pause"); return 0; }
1、封装的意义
#include <iostream> #include <string> using namespace std; //访问权限:3种 //公共权限 public 成员 类内可以访问 类外可以访问 //保护权限 protected 成员 类内可以访问 类外不可以访问 儿子可以访问父亲中的保护内容 //私有权限 private 成员 类内可以访问 类外不可以访问 儿子不可以访问父亲的私有内容 class Person { //公共权限 public: string m_Name; //姓名 //保护权限 protected: string m_Car; //汽车 //私有权限 private: int m_Password; //银行卡密码 public: void func() { m_Name = "张三"; m_Car = "拖拉机"; m_Password = 123456; } }; int main() { //实例化具体的对象 Person p1; p1.m_Name = "李四"; //p1.m_Car = "奔驰"; //保护权限的内容,在类外访问不到 //p1.m_Password = 123; //私有权限的内容,类外访问不到 system("pause"); return 0; }
2、struct 和 class 的区别
#include <iostream> #include <string> using namespace std; //struct 和 class 区别 //struct 默认权限是 公共 public //class 默认权限是 私有 private class C1 { //默认是私有权限 int m_A; }; struct C2 { //默认是公共权限 int m_A; }; int main() { C1 c1; //c1.m_A = 100; //错误,默认是私有 C2 c2; c2.m_A = 100; //正确 system("pause"); return 0; }
3、成员属性设置为私有
#include <iostream> #include <string> using namespace std; //成员属性设置为私有 //1.可以自己控制读写权限 //2.对于写可以检测数据的有效性 //设计人类 class Person { private: //姓名 读写 string m_Name; //年龄 只读 int m_Age; //亲人 只写 string m_Lover; public: //设置姓名 void setName(string name) { m_Name = name; } //获取姓名 string getName() { return m_Name; } //获取年龄 int getAge() { return m_Age; } //设置年龄 void setAge(int age) { if (age < 0 || age > 150) { m_Age = 0; cout << "你这个老妖精!" << endl; return; } m_Age = age; } //设置亲人 void setLover(string lover) { m_Lover = lover; } }; int main() { Person p; p.setName("张三"); cout << "姓名:" << p.getName() << endl; p.setAge(10); cout << "年龄:" << p.getAge() << endl; p.setLover("妈妈"); //cout << "亲人:" << p.getLover() << endl; system("pause"); return 0; }
#include <iostream> #include <string> using namespace std; //立方体类的设计 //1.创建立方体类 //2.设计属性 //3.设计行为,获取立方体面积和体积,设置获取长宽高 //4.分别利用全局函数和成员函数 判断两个立方体是否相等 class Cube { private: int m_L; //长 int m_W; //宽 int m_H; //高 public: //设置长 void setL(int l) { m_L = l; } //获取长 int getL() { return m_L; } //设置宽 void setW(int w) { m_W = w; } //获取宽 int getW() { return m_W; } //设置高 void setH(int h) { m_H = h; } //获取高 int getH() { return m_H; } //获取立方体面积 int calculateS() { return 2 * m_L*m_W + 2 * m_W*m_H + 2 * m_L*m_H; } //获取立方体体积 int calculateV() { return m_L * m_W * m_H; } //利用成员函数判断两个立方体是否相等 bool isSameByClass(Cube &c) { if (m_L == c.getL() && m_W == c.getW() && m_H == c.getH()) { return true; } else { return false; } } }; //利用全局函数判断 两个立方体是否相等 bool isSame(Cube &c1, Cube &c2) { if (c1.getL() == c2.getL() && c1.getW() == c2.getW() && c1.getH() == c2.getH()) { return true; } else { return false; } } int main() { //创建立方体对象 Cube c1; c1.setL(10); c1.setW(10); c1.setH(10); cout << "c1 的面积: " << c1.calculateS() << endl; cout << "c1 的体积: " << c1.calculateV() << endl; //创建第二个立方体 Cube c2; c2.setL(10); c2.setW(10); c2.setH(10); //利用全局函数判断 bool ret = isSame(c1, c2); if (ret) { cout << "c1 和 c2 相等 " << endl; } else { cout << "c1 和 c2 不相等 " << endl; } //利用成员函数判断,c1是已知的,只需要一个c2 ret = c1.isSameByClass(c2); //ret = c2.isSameByClass(c1); if (ret) { cout << "成员函数判断: c1 和 c2 相等 " << endl; } else { cout << "成员函数判断: c1 和 c2 不相等 " << endl; } system("pause"); return 0; }
#include <iostream> #include <string> using namespace std; //点和圆关系案例 //点类 class Point { private: int m_X; int m_Y; public: //设置x void setX(int x) { m_X = x; } //获取x int getX() { return m_X; } //设置y void setY(int y) { m_Y = y; } //获取y int getY() { return m_Y; } }; //圆类 class Circle { public: //设置半径 void setR(int r) { m_R = r; } //获取半径 int getR() { return m_R; } //设置圆心 void setCenter(Point center) { m_Center = center; } //获取圆心 Point getCenter() { return m_Center; } private: int m_R; //半径 Point m_Center; //圆心 }; //判断点和圆的关系 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; } } int main() { //创建圆 Circle c; c.setR(10); Point center; center.setX(10); center.setY(0); c.setCenter(center); //创建点 Point p; p.setX(20); p.setY(21); //判断关系 isInCircle(c,p); system("pause"); return 0; }
把类拆分成不同的文件
circle.h
#pragma once #include "point.h" #include <iostream> using namespace std; //圆类 class Circle { public: //设置半径 void setR(int r); //获取半径 int getR(); //设置圆心 void setCenter(Point center); //获取圆心 Point getCenter(); private: int m_R; //半径 Point m_Center; //圆心 };
circle.cpp
#include "circle.h" //设置半径 void Circle::setR(int r) { m_R = r; } //获取半径 int Circle::getR() { return m_R; } //设置圆心 void Circle::setCenter(Point center) { m_Center = center; } //获取圆心 Point Circle::getCenter() { return m_Center; }
point.h
#pragma once //防止头文件重复包含 #include <iostream> using namespace std; //点类 声明 class Point { private: int m_X; int m_Y; public: //设置x void setX(int x); //获取x int getX(); //设置y void setY(int y); //获取y int getY(); };
point.cpp
#include "point.h" //设置x void Point::setX(int x) { m_X = x; } //获取x int Point::getX() { return m_X; } //设置y void Point::setY(int y) { m_Y = y; } //获取y int Point::getY() { return m_Y; }
test.cpp
#include <iostream> #include <string> using namespace std; //点和圆关系案例 #include "circle.h" #include "point.h" //判断点和圆的关系 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; } } int main() { //创建圆 Circle c; c.setR(10); Point center; center.setX(10); center.setY(0); c.setCenter(center); //创建点 Point p; p.setX(10); p.setY(10); //判断关系 isInCircle(c,p); system("pause"); return 0; }