目录
总结:class默认是私有的,struct默认是共有的。因此在c++中通过对类的使用,可以赋予客户端访问数据的一致性。可以细微划分控制,通过使用成员函数可使得我们对变量的控制处理更加精细。
1、类和对象的基本概念
1.1、c和c++中struct区别
c语言中struct只有变量,c++中struct既有变量,也有函数
1.2、c语言中的结构体
typedef struct{
char name[32];
int age;
}Person;
typedef struct {
char name[32];
int age;
char type[32];
}Dog;
void PersonEat(Person *p) {
cout << p->name << "正在吃饭" << endl;
}
void DogEat(Dog* d) {
cout << d->name << "正在吃饭" << endl;
}
void test() {
Person person = { "老王",20 };
Dog dog = { "旺仔",12,"哈士奇" };
PersonEat(&person);
DogEat(&dog);
}
int main() {
test();
}
运行结果:
总结:在上述代码中,会出现一个问题,通过结构体实现上述功能,数据和方法是相互独立的,这样会造成方法调用错误数据。
2、类的封装
封装:
1、把变量(属性) 和函数合成一个整体,封装在一个类中。
2、对变量和函数进行访问控制(public、private、protected)
3、在类的内部(作用范围内),没有访问权限之分,所有成员可以相互访问
4、在类的外部(作用范围外),访问权限才有意义
3、类的初识
class 类名{
private://私有的 类的外部不可以直接访问
protected://受保护的 类的外部不可以直接访问
数据
public://公有的 类的外部可以直接访问
方法
//在类的内部 没有权限之分 都可以相互访问
};
案例:
类的定义和实例化:
class Person {//类的内部
private:
int money;
protected:
int age;
public:
void myfunc() {
cout << "我" << age << "有" << money << "万" << endl;
}
};
void test() {
//实例化对象 简单说就是用Person定义一个变量
Person lucy;
}
int main() {
test();
}
由于money是private私有的,所以在类的外部是不允许直接访问赋值的,同样age是protected受保护的,所以在类的外部也不允许直接访问和赋值,但是myfunc()是public公共的,因此在类的内外都可以直接访问。
那怎样在类的外部使用那些私有的和受保护的数据呢?
1、通过公共函数为私有成员进行赋值。
class Person {//类的内部
private:
int money;
protected:
int age;
public:
void myfunc() {
money = 100;
age = 18;
cout << "我" << age << "有" << money << "万" << endl;
}
};
void test() {
Person lucy;
lucy.myfunc();
}
int main() {
test();
}
运行结果:
总结:private protect虽然是私有、受保护 类外部不可访问,但是用户可以借助共有的方法,间接访问私有、受保护的数据。
2、利用get() 和 set() 函数访问私有数据成员
class Person {//类的内部
private:
int money;
protected:
int age;
public:
void myfunc() {
cout << "我" << age << "有" << money << "万" << endl;
}
public:
void setMoney(int m) {
money = m;
}
void setAge(int a) {
age = a;
}
int getMoney() {
return money;
}
int getAge() {
return age;
}
};
void test() {
Person lucy;
lucy.setMoney(100);
lucy.setAge (18);
lucy.myfunc();
}
int main() {
test();
}
运行结果:
总结:class默认是私有的,struct默认是共有的。因此在c++中通过对类的使用,可以赋予客户端访问数据的一致性。可以细微划分控制,通过使用成员函数可使得我们对变量的控制处理更加精细。
4、Person类的设计
代码示例:
class Person {
private:
std::string name;
int age;
public:
void setName(std::string name) {
this->name = name;
}
void setAge(int age) {
if (age > 0 && age < 100) {
this->age = age;
}
else {
cout << "age无效" << endl;
}
}
std::string getName() {
return name;
}
int getAge() {
return age;
}
};
void Init() {
Person per;
per.setName("小明");
per.setAge(18);
cout << "姓名" << per.getName() << ",年龄" << per.getAge() << endl;
}
int main() {
Init();
}
运行结果:
5、立方体类的设计
计算立方体体积和面积,并且判断两立方体是否相等
class Cub {
private:
int l;
int w;
int h;
public:
void setL(int l) {
this->l = l;
}
void setW(int w) {
this->w = w;
}
void setH(int h) {
this->h = h;
}
int getL() {
return l;
}
int getW() {
return w;
}
int getH() {
return h;
}
int area() {
return 2 * l * w + 2 * l * h + 2 * w * h;
}
int v() {
return l* w* h;
}
bool ISCUB(Cub &ob) {
if (l == ob.l && h == ob.h && w == ob.w) {
cout << "两立方体相等" << endl;
return true;
}
else {
cout << "两立方体不相等" << endl;
return false;
}
}
};
//全局函数
bool isCub(Cub &c1,Cub &c2) {
if (c1.area() == c2.area() && c2.v() == c1.v()) {
cout << "两立方体相等" << endl;
return true;
}
else {
cout << "两立方体不相等" << endl;
return false;
}
}
void Init() {
Cub cub,cub2;
int S1, V1,S2,V2;
cub.setL(1);
cub.setW(2);
cub.setH(3);
S1 = cub.area();
V1 = cub.v();
cout << "cub的面积为" << cub.area() << endl;
cout << "cub的体积为" << cub.v() << endl;
cub2.setL(1);
cub2.setW(2);
cub2.setH(3);
S2 = cub2.area();
V2 = cub2.v();
cout << "cub2的面积为" << cub2.area() << endl;
cout << "cub2的体积为" << cub2.v() << endl;
isCub(cub,cub2);
cub.ISCUB(cub2);
}
int main() {
Init();
}
运行结果: