C++深入分析讲解类的知识点_第1页
C++深入分析讲解类的知识点_第2页
C++深入分析讲解类的知识点_第3页
C++深入分析讲解类的知识点_第4页
C++深入分析讲解类的知识点_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第C++深入分析讲解类的知识点目录知识点引入类的初识1、封装2、权限3、类的定义(定义类型)4、类的成员函数与类中声明及类外定义Person类的设计设计立方体类点Point和圆Circle的关系

知识点引入

C语言中数据和方法是独立:

//c语言的思想:数据方法分开

typedefstruct

charname[32];

intage;

}Person;

typedefstruct

charname[32];

intage;

inttype;

}Dog;

voidPersonEat(Person*p)

coutp-name"正在吃饭"endl;

voidDogEat(Dog*d)

coutd-name"正在吃狗粮,汪汪"endl;

voidtest01()

Personperson={"老王",43};

Dogdog={"旺财",6};

PersonEat(person);

DogEat(dog);

//出现一个问题(数据方法独立容易造成方法调用错误数据)

DogEat((Dog*)person);

}

类的初识

1、封装

把变量(属性)和函数(操作)合成一个整体,封装在一个类中

对变量和函数进行访问控制(公有、私有、保护)

类的封装:将数据和方法封装在一起加以权限区分用户只能通过公共方法访问私有数据。

2、权限

private私有public公有protected保护

private私有:类外部不可直接访问私有数据类内部可以访问

protected保护:类外部不可直接访问私有数据类内部可以访问

public公有:类外部可以访问私有数据类内部可以访问

权限只是针对类的外部,类的内部没有权限之分

class类名{//抽象的概念系统不会为其分配空间

private://私有类的外部不可直接访问

protected://保护类的外部不可直接访问

数据

public://公有类的外部可以直接访问

方法

//在类的内部没有权限之分都可以相互访问

};

案例:

classPerson//抽象的概念

{//类的内部

private:

intm_money;//私有数据

protected:

intm_age;

public:

voiddese()

m_money=100;

m_age=18;

cout"我有房有车又年轻"m_age"岁又有钱"m_money"万美金我就爱嘚瑟"endl;

voidtest01()

//用类去实例化一个对象(就是用Person定义一个变量)

Personlucy;

//cout"兄弟你的钱:"lucy.m_moneyendl;//err内的外部不可访问

//cout"兄弟你的年龄:"lucy.m_ageendl;//err内的外部不可访问

lucy.dese();//ok公有的类的外部可用访问

//privateprotected虽然是私有、保护的类外不可访问但是用户可以借助public公有的方法

//间接的访问私有、保护的数据

}

class默认是私有的数据私有方法公有用户就可以借助公有方法间接的操作私有数据

3、类的定义(定义类型)

关键字class

#includeiostream

usingnamespacestd;

//类的定义:是不占空间只有用类实例化对象的时候系统为对象开辟空间

classData

private://私有

//inta=10;//err定义类的时候尽量不要给成员赋值

inta;//成员数据

protected://保护

intb;

public://公有

intc;

//成员函数

voiddata_show(void)

//类的内部:没有权限区分

couta","b","cendl;

//类中的数据成员拥有独立的空间

voidtest01()

//使用Data实例化一个对象名为ob的对象

Dataob;

//成员数据依赖于对象的

//cout"ob.a="ob.aendl;//erra为私有类外不能直接访问

//cout"ob.b="ob.bendl;//errb为保护类外不能直接访问

cout"ob.c="ob.cendl;//errc为公有类外可以直接访问

//对象通过公共方法间接调用私用数据

ob.data_show();

intmain(intargc,char*argv[])

test01();

return0;

}

4、类的成员函数与类中声明及类外定义

classData2

//默认为私有

inta;

public:

//类中声明类外定义

voidsetA(intv);

intgetA(void);

voidData2::setA(intv)

a=v;

return;

intData2::getA()

returna;

voidtest02()

Data2ob;

ob.setA(100);

cout"a="ob.getA()endl;

}

Person类的设计

设计一个Person类型

设计一个Person类,Person类具有name和age属性,提供初始化函数(Init),并提供对name和age的读写函数(set,get),但必须确保age的赋值在有效范围内(0-100),超出有效范围,则拒绝赋值,并提供方法输出姓名和年龄。

person.h:

#ifndefPERSON_H

#definePERSON_H

//类的头文件:一般定义成员数据声明成员函数

classPerson

private:

charm_name[32];

intm_age;

public:

//初始化m_namem_age

voidinit(char*name,intage);

//设置name

voidsetName(char*name);

//获取name

char*getName(void);

//设置age

voidsetAge(intage);

//获取age

intgetAge(void);

//显示m_namem_age

voidshowPerons(void);

#endif//PERSON_H

person.cpp:

#include"person.h"

//#includestring.h

#includecstring

#includeiostream

usingnamespacestd;

//定义类的成员函数

voidPerson::init(char*name,intage)

strcpy(m_name,name);

if(age=0age=100)

m_age=age;

else

cout"年龄输入非法"endl;

voidPerson::setName(char*name)

strcpy(m_name,name);

return;

char*Person::getName()

returnm_name;

voidPerson::setAge(intage)

if(age=0age=100)

m_age=age;

else

cout"年龄输入非法"endl;

return;

intPerson::getAge()

returnm_age;

voidPerson::showPerons()

cout"姓名:"m_name",年龄:"m_ageendl;

return;

}

main.cpp

#includeiostream

#include"person.h"

usingnamespacestd;

intmain(intargc,char*argv[])

Personob;

ob.init("lucy",18);

ob.showPerons();

ob.setName("bob");

cout"姓名:"ob.getName()endl;

ob.setAge(19);

cout"年龄:"ob.getAge()endl;

return0;

}

设计立方体类

设计立方体类(Cube),求出立方体的面积(2ab+2ac+2bc)和体积(a*b*c),分别用全局函数和成员函数判断两个立方体是否相等

cube.h

#ifndefCUBE_H

#defineCUBE_H

classCube

private:

intm_l;

intm_w;

intm_h;

public:

voidsetL(intl);

intgetL(void);

voidsetW(intw);

intgetW(void);

voidsetH(inth);

intgetH(void);

intgetS(void);

intgetV(void);

boolcompareCube(Cubeob);

#endif//CUBE_H

cube.cpp

#include"cube.h"

voidCube::setL(intl)

m_l=l;

return;

intCube::getL()

returnm_l;

voidCube::setW(intw)

m_w=w;

return;

intCube::getW()

returnm_w;

voidCube::setH(inth)

m_h=h;

return;

intCube::getH()

returnm_h;

intCube::getS()

return(m_l*m_w+m_w*m_h+m_l*m_h)*2;

intCube::getV()

returnm_l*m_w*m_h;

boolCube::compareCube(Cubeob)

if((m_l==ob.m_l)(m_w==ob.m_w)(m_h==ob.m_h))

returntrue;

else

returnfalse;

}

main.cpp

#includeiostream

#include"cube.h"

usingnamespacestd;

boolmyCompareCube(Cubeob1,Cubeob2)

if((ob1.getL()==ob2.getL())\

(ob1.getW()==ob2.getW())(ob1.getH()==ob2.getH()))

returntrue;

else

returnfalse;

intmain(intargc,char*argv[])

Cubeob1;

ob1.setL(10);

ob1.setW(10);

ob1.setH(10);

cout"面积为:"ob1.getS()endl;

cout"体积为:"ob1.getV()endl;

Cubeob2;

ob2.setL(10);

ob2.setW(20);

ob2.setH(10);

if(pareCube(ob2)==true)

cout"相等"endl;

else

cout"不相等"endl;

if(myCompareCube(ob1,ob2)==true)

cout"相等"endl;

else

cout"不相等"endl;

return0;

}

点Point和圆Circle的关系

#includeiostream

usingnamespacestd;

classPoint

private:

intm_x;

intm_y;

public:

voidsetX(intx)

m_x=x;

intgetX(void)

returnm_x;

voidsetY(inty)

m_y=y;

intgetY(void)

returnm_y;

classCircle

private:

Pointm_p;

intm_r;

public:

voidsetPoint(intx,inty)

m_p.setX(x);

m_p.setY(y);

voidsetR(intr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论