1、格式
#include <iostream> using namespace std;const double PI =3.14;//设计一个圆类,求圆的周长 class Circle {//访问权限//公共权限 public://属性//半径int m_r;//行为//获取圆的周长double calculateZC() {return 2 * PI * m_r;} };int main() {//通过圆类创造具体的圆Circle c1;c1.m_r = 10;cout << "圆的周长:" << c1.calculateZC() << endl;system("pause");return 0; }
2、案例:设计学生类
设计一个学生类,属性有姓备和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
#include <iostream> using namespace std; #include <string>class Studentt { public:string m_Name;int m_Id;void showStudent() {cout << "姓名:" << m_Name << " 学号:" << m_Id << endl;}//给姓名赋值void setName(string name) {m_Name = name;}//给学号赋值void setId(int id) {m_Id = id;} };int main() {Studentt s1;//s1.m_Name = "张三";s1.setName("张三");s1.setId(1);s1.showStudent();Studentt s2;s2.m_Name = "李四";s2.m_Id = 2;s2.showStudent();system("pause");return 0; }
3、权限访问
1. public公共权限
2. protected 保护权限
3. private私有权限
#include <iostream> using namespace std; #include <string>//1. public公共权限 成员 类内可以访问 类外也可以访问 //2. protected保护权限 成员 类内可以访问 类外不可以访问 儿子可以访问父亲中的保护内容 //3. 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 = 23456;//私有权限内容,在类外访问不到system("pause");return 0; }
4、class和struct区别
- struct 默认权限为公共
- class默认权限为私有
#include <iostream> using namespace std; #include <string>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; }
5、成员属性私有化
#include <iostream> using namespace std; #include <string>class Person {public://设置姓名void setName(string name) {m_Name = name;}//获取姓名string getName() {return m_Name;}int getAge() {m_Age = 10;return m_Age;}void setLover(string lover) {m_Lover = lover;}private:string m_Name;//可读可写int m_Age;//只读string m_Lover;//只写 };int main() {Person p;p.setName("张三");cout << "姓名:"<<p.getName()<< endl;cout << "年龄:" << p.getAge() << endl;p.setName("梨花");system("pause");return 0; }
6、案例设计:立方体类
设计立方体类(Cube)
求出立方体的面积和体积
分别用全局函数和成员函数判断两个立方体是否相等#include <iostream> using namespace std; #include <string>class Cube {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_H * m_L * m_W;}//利用成员函数判断两个立方体是否相等bool isSameByClass(Cube& c) {if (m_L == c.getL() && m_W == c.getW() && m_H == c.getH()) {return true;}return false; }private:int m_L;int m_W;int m_H; };//利用全局函数判断 两个立方体是否相等 bool isSame(Cube& c1, Cube& c2) {if (c1.getL() == c2.getL() && c1.getW() == c2.getW() && c1.getH() == c2.getH()) {return true;}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);cout << "c2的面积为:" << c2.calculateS() << endl;cout << "c2的体积为:" << c2.calculateV() << endl;bool ret=isSame(c1, c2);if (ret) {cout << "c1,c2相等" << endl;}else {cout << "c1,c2不相等" << endl;}int ret2= c1.isSameByClass(c2);if (ret2) {cout << "c1,c2相等" << endl;}else {cout << "c1,c2不相等" << endl;}int ret3 = c2.isSameByClass(c1);if (ret3) {cout << "c1,c2相等" << endl;}else {cout << "c1,c2不相等" << endl;}system("pause");return 0; }
7、案例设计:点和圆的关系
设计一个圆形类(Circle),和一个点类 (Point) ,计算点和圆的关系
#include <iostream> using namespace std; #include <string>class Point { public:void setX(int x) {m_X = x;}int getX() {return m_X;}void setY(int y) {m_Y = y;}int getY() {return m_Y;}private:int m_X;int 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(10);c.setCenter(center);//创建点Point p;p.setX(10);p.setY(11);//判断关系isInCircle(c, p);system("pause");return 0; }
point.h
#pragma once//防止头文件重复包含 #include <iostream> using namespace std;class Point { public:void setX(int x);int getX();void setY(int y);int getY(); private:int m_X;int m_Y; };
point.cpp
#include "point.h"void Point:: setX(int x) {m_X = x; } int Point:: getX() {return m_X; }void Point:: setY(int y) {m_Y = y; } int Point:: getY() {return m_Y; }
circle.h
#pragma once #include <iostream> using namespace std; #include "point.h"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;}
面向对象.cpp
#include <iostream> using namespace std; #include <string> #include "point.h" #include "circle.h"//class Point { //public: // void setX(int x) { // m_X = x; // } // int getX() { // return m_X; // } // // void setY(int y) { // m_Y = y; // } // int getY() { // return m_Y; // } // //private: // int m_X; // int 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(10);p.setY(10);//判断关系isInCircle(c, p);system("pause");return 0; }