c++类和对象

本文详细介绍了C++中类和对象的基础概念,比较了C和C++中struct的区别,探讨了类的封装机制,包括如何通过公共函数访问私有数据成员。通过实例展示了Person和立方体类的设计,以及如何使用get()和set()函数进行数据控制。
摘要由CSDN通过智能技术生成

目录

1、类和对象的基本概念

1.1、c和c++中struct区别

1.2、c语言中的结构体

2、类的封装

3、类的初识

1、通过公共函数为私有成员进行赋值。

2、利用get() 和 set() 函数访问私有数据成员

总结:class默认是私有的,struct默认是共有的。因此在c++中通过对类的使用,可以赋予客户端访问数据的一致性。可以细微划分控制,通过使用成员函数可使得我们对变量的控制处理更加精细。

4、Person类的设计

5、立方体类的设计


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();
}

 运行结果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚笛诶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值