目录
C语言中 数据 和 方法 是独立:
- //c语言的思想:数据 方法 分开
- //人
- typedef struct
- {
- char name[32];
- int age;
- }Person;
-
- //动物
- typedef struct
- {
- char name[32];
- int age;
- int type;
- }Dog;
-
- void PersonEat(Person *p)
- {
- cout<<p->name<<"正在吃饭"<<endl;
- }
- void DogEat(Dog *d)
- {
- cout<<d->name<<"正在吃狗粮, 汪汪"<<endl;
- }
-
- void test01()
- {
- Person person = {"老王",43};
- Dog dog={"旺财",6};
-
- PersonEat(&person);
- DogEat(&dog);
-
- // 出现一个问题(数据 方法独立 容易造成 方法 调用错误数据)
- DogEat((Dog *)&person);
- }

把变量(属性)和函数(操作)合成一个整体,封装在一个类中
对变量和函数进行访问控制(公有、私有、保护)
类的封装:将数据和方法封装在一起 加以权限区分 用户只能通过公共方法 访问私有数据。
private私有 public公有 protected保护
private私有:类外部不可直接访问私有数据 类内部可以访问
protected保护:类外部不可直接访问私有数据 类内部可以访问
public公有:类外部可以访问私有数据 类内部可以访问
权限只是针对 类的外部 , 类的内部 没有权限之分
- class 类名{//抽象的概念 系统不会为其分配空间
- private://私有 类的外部 不可直接访问
- protected://保护 类的外部 不可直接访问
- 数据
- public://公有 类的外部 可以直接访问
- 方法
-
- //在类的内部 没有权限之分 都可以相互访问
- };
案例:
- class Person//抽象的概念
- {//类的内部
- private:
- int m_money;//私有数据
- protected:
- int m_age;
- public:
- void dese()
- {
- m_money = 100;
- m_age = 18;
- cout<<"我有房 有车 又年轻"<<m_age<<"岁又有钱"<<m_money<<"万美金 我就爱嘚瑟"<<endl;
-
- }
- };
- void test01()
- {
- //用类 去 实例化 一个对象(就是用Person定义一个变量)
- Person lucy;
- //cout<<"兄弟你的钱:"<<lucy.m_money<<endl;//err 内的外部不可访问
- //cout<<"兄弟你的年龄:"<<lucy.m_age<<endl;//err 内的外部不可访问
-
- lucy.dese();//ok 公有的类的外部可用访问
- //private protected虽然是私有、保护的 类外不可访问 但是用户可以借助 public公有的方法
- //间接的访问私有、保护的数据
- }
class默认是私有的 数据私有 方法公有 用户就可以借助 公有方法 间接的操作 私有数据
关键字 class
- #include <iostream>
-
- using namespace std;
- //类的定义:是不占空间 只有用类实例化 对象的时候 系统为对象开辟空间
- class Data
- {
- private://私有
- // int a=10;//err 定义类的时候 尽量不要 给成员赋值
- int a;//成员 数据
- protected://保护
- int b;
- public://公有
- int c;
-
- //成员函数
- void data_show(void)
- {
- //类的内部:没有权限区分
- cout<<a<<", "<<b<<", "<<c<<endl;
- }
- };
- //类中的数据成员 拥有 独立的空间
- void test01()
- {
- //使用Data实例化一个对象名为ob的对象
- Data ob;
- //成员数据依赖于对象的
- //cout<<" ob.a = "<<ob.a<<endl;//err a为私有 类外不能直接访问
- //cout<<" ob.b = "<<ob.b<<endl;//err b为保护 类外不能直接访问
- cout<<" ob.c = "<<ob.c<<endl;//err c为公有 类外可以直接访问
-
- //对象 通过公共方法 间接调用私用数据
- ob.data_show();
- }
-
- int main(int argc, char *argv[])
- {
- test01();
- return 0;
- }
- class Data2
- {
- //默认为私有
- int a;
- public:
- //类中声明 类外定义
- void setA(int v);
-
- int getA(void);
- };
- void Data2::setA(int v)
- {
- a = v;
- return;
- }
-
- int Data2::getA()
- {
- return a;
- }
-
-
- void test02()
- {
- Data2 ob;
- ob.setA(100);
- cout<<"a = "<<ob.getA()<<endl;
- }

设计一个Person类型
设计一个Person类,Person类具有name和age属性,提供初始化函数(Init),并提供对name和age的 读写函数(set,get),但必须确保age的赋值在有效范围内(0-100),超出有效范围,则拒绝赋值,并提供 方法输出姓名和年龄。
person.h:
- #ifndef PERSON_H
- #define PERSON_H
-
- //类的头文件:一般定义成员数据 声明成员函数
- class Person
- {
- private:
- char m_name[32];
- int m_age;
- public:
- //初始化m_name m_age
- void init(char *name, int age);
- //设置name
- void setName(char *name);
- //获取name
- char *getName(void);
- //设置age
- void setAge(int age);
- //获取age
- int getAge(void);
- //显示m_name m_age
- void showPerons(void);
- };
-
- #endif // PERSON_H
person.cpp:
- #include "person.h"
- //#include <string.h>
- #include<cstring>
- #include<iostream>
- using namespace std;
- //定义类的成员函数
- void Person::init(char *name, int age)
- {
-
- strcpy(m_name, name);
- if(age>=0 && age<=100)
- {
- m_age = age;
- }
- else
- {
- cout<<"年龄输入非法"<<endl;
- }
- }
-
- void Person::setName(char *name)
- {
- strcpy(m_name, name);
- return;
- }
-
- char *Person::getName()
- {
- return m_name;
- }
-
- void Person::setAge(int age)
- {
- if(age>=0 && age<=100)
- {
- m_age = age;
- }
- else
- {
- cout<<"年龄输入非法"<<endl;
- }
- return;
- }
-
- int Person::getAge()
- {
- return m_age;
- }
-
- void Person::showPerons()
- {
- cout<<"姓名:"<<m_name<<", 年龄:"<<m_age<<endl;
- return;
- }
main.cpp
- #include <iostream>
- #include "person.h"
- using namespace std;
-
- int main(int argc, char *argv[])
- {
- Person ob;
-
- ob.init("lucy", 18);
- ob.showPerons();
-
- ob.setName("bob");
- cout<<"姓名:"<<ob.getName()<<endl;
-
- ob.setAge(19);
- cout<<"年龄:"<<ob.getAge()<<endl;
-
- return 0;
- }
设计立方体类(Cube),求出立方体的面积( 2ab + 2ac + 2bc )和体积( a * b * c),分别用全局函数和成员 函数判断两个立方体是否相等
cube.h
- #ifndef CUBE_H
- #define CUBE_H
-
-
- class Cube
- {
- private:
- int m_l;
- int m_w;
- int m_h;
- public:
- void setL(int l);
- int getL(void);
- void setW(int w);
- int getW(void);
- void setH(int h);
- int getH(void);
-
- int getS(void);
- int getV(void);
- bool compareCube(Cube &ob);
- };
-
- #endif // CUBE_H
cube.cpp
- #include "cube.h"
-
-
- void Cube::setL(int l)
- {
- m_l = l;
- return;
- }
-
- int Cube::getL()
- {
- return m_l;
- }
-
- void Cube::setW(int w)
- {
- m_w = w;
- return;
- }
-
- int Cube::getW()
- {
- return m_w;
- }
-
- void Cube::setH(int h)
- {
- m_h = h;
- return;
- }
-
- int Cube::getH()
- {
- return m_h;
- }
-
- int Cube::getS()
- {
- return (m_l*m_w+m_w*m_h+m_l*m_h)*2;
- }
-
- int Cube::getV()
- {
- return m_l*m_w*m_h;
- }
-
- bool Cube::compareCube(Cube &ob)
- {
- if((m_l==ob.m_l) &&( m_w==ob.m_w) &&(m_h == ob.m_h))
- return true;
- else
- return false;
- }
main.cpp
- #include <iostream>
- #include "cube.h"
- using namespace std;
- bool myCompareCube(Cube &ob1, Cube &ob2)
- {
- if((ob1.getL() == ob2.getL()) && \
- (ob1.getW() == ob2.getW()) &&(ob1.getH() == ob2.getH()))
- {
- return true;
- }
- else
- return false;
- }
-
- int main(int argc, char *argv[])
- {
- Cube ob1;
- ob1.setL(10);
- ob1.setW(10);
- ob1.setH(10);
-
- cout<<"面积为:"<<ob1.getS()<<endl;
- cout<<"体积为:"<<ob1.getV()<<endl;
-
-
- Cube ob2;
- ob2.setL(10);
- ob2.setW(20);
- ob2.setH(10);
- if(ob1.compareCube(ob2) == true)
- {
- cout<<"相等"<<endl;
- }
- else
- {
- cout<<"不相等"<<endl;
- }
-
- if(myCompareCube(ob1, ob2) == true)
- {
- cout<<"相等"<<endl;
- }
- else
- {
- cout<<"不相等"<<endl;
- }
-
- return 0;
- }

- #include <iostream>
-
- using namespace std;
-
- class Point
- {
- private:
- int m_x;
- int m_y;
- public:
- void setX(int x)
- {
- m_x = x;
- }
- int getX(void)
- {
- return m_x;
- }
- void setY(int y)
- {
- m_y = y;
- }
- int getY(void)
- {
- return m_y;
- }
- };
-
- class Circle
- {
- private:
- Point m_p;
- int m_r;
- public:
- void setPoint(int x, int y)
- {
- m_p.setX(x);
- m_p.setY(y);
- }
-
- void setR(int r)
- {
- m_r = r;
- }
- int getR(void)
- {
- return m_r;
- }
-
- int PointIsOnCircle(Point &p)
- {
- int tmp_x = (m_p.getX()-p.getX())*(m_p.getX()-p.getX());
- int tmp_y = (m_p.getY()-p.getY())*(m_p.getY()-p.getY());
- if((tmp_x+tmp_y) > (m_r*m_r))//圆外
- {
- return 1;
- }
- else if((tmp_x+tmp_y) == (m_r*m_r))//圆上
- {
- return 0;
- }
- else if((tmp_x+tmp_y) < (m_r*m_r))//圆内
- {
- return -1;
- }
- }
- };
-
-
- int main(int argc, char *argv[])
- {
- Point p1;
- p1.setX(5);
- p1.setY(5);
-
- Circle c1;
- c1.setPoint(2,2);
- c1.setR(5);
- int ret = c1.PointIsOnCircle(p1);
- if( ret== 0)
- {
- cout<<"圆上"<<endl;
- }
- else if(ret == 1)
- {
- cout<<"圆外"<<endl;
- }
- else if(ret == -1)
- {
- cout<<"圆内"<<endl;
- }
-
- return 0;
- }
