C++ 面向对象

在编程的世界里,有一句话叫: 万物皆对象。

正是因为这个认知,我们在计算机里看到的世界越来越接近现实了。

类 ,简单理解就是一类事物,是具有共同属性事物的统一概念。就比如人类,指的是人的总称,这是个抽象的概念。

对象 ,属于某个类里面的一个具体实体。比如你、我、我们每个人都是人类概念下一个具体的人。

image

我们在C++里做面向对象,就是要给某一类事物的概念做一个描述。

人类的代码描述

老规矩,先上代码:

#include<bits/stdc++.h>
using namespace std;

//定义一个名为Person的类,代表人
class Person{
//public,表示这个属性是对外公开的。其他还有private和protected
public:
	//一个人会有很多属性,比如名字、生日、年龄
	string name;
	int birthyear,birthmonth,birthday;
	int age;
};

int main(){
	
	//接下来我们创造一个具体的人,这人叫张三……
	Person zhangsan;
	zhangsan.name="张三";
	zhangsan.birthyear=2003;
	zhangsan.birthmonth=7;
	zhangsan.birthday=20;
	
	return 0;
} 

你们还能想到的可以封装成class的事物吗。
来看看各部分的概念。

image

成员函数

除了代表属性的成员变量,我们还可以给类做成员函数,表示一些共同的动作,比如我想输出这个人的基本信息:

#include<bits/stdc++.h>
using namespace std;

class Person{
	
public:
	
	string name;
	int birthyear,birthmonth,birthday;
	int age;
	
	//添加一个成员函数,用来输出属性信息 
	void showinfo(){
		cout<<"name:"<<this->name<<endl;
		printf("birthday:%04d-%02d-%02d\n",this->birthyear,this->birthmonth,this->birthday);
	}
	
};
int main(){
	
	Person zhangsan;
	zhangsan.name="张三";
	zhangsan.birthyear=2003;
	zhangsan.birthmonth=7;
	zhangsan.birthday=20;
	
	//直接调用函数 
	zhangsan.showinfo(); 
	
	return 0;
} 

这里要注意一下this

image

小试牛刀

我们来做个小练习,做一个函数,根据这人的生日返回他的星座是啥

image

点击查看代码
#include<bits/stdc++.h>
using namespace std;

class Person{
	
public:
	
	string name;
	int birthyear,birthmonth,birthday;
	int age;
	
	//添加一个成员函数,用来输出属性信息 
	void showinfo(){
		cout<<"name:"<<this->name<<endl;
		
		printf("birthday:%04d-%02d-%02d\n",this->birthyear,this->birthmonth,this->birthday);
	}
	
	// 星座判断示例
	string xingzuo() {
	    if ((this->birthmonth == 3 && this->birthday >=21) || (this->birthmonth ==4 && this->birthday <=19)) 
	        return "白羊座";
	    else if ((this->birthmonth ==4 && this->birthday >=20) || (this->birthmonth ==5 && this->birthday <=20)) 
	        return "金牛座";
	    else if ((this->birthmonth ==5 && this->birthday >=21) || (this->birthmonth ==6 && this->birthday <=21)) 
	        return "双子座";
	    else if ((this->birthmonth ==6 && this->birthday >=22) || (this->birthmonth ==7 && this->birthday <=22)) 
	        return "巨蟹座";
	    else if ((this->birthmonth ==7 && this->birthday >=23) || (this->birthmonth ==8 && this->birthday <=22)) 
	        return "狮子座";
	    else if ((this->birthmonth ==8 && this->birthday >=23) || (this->birthmonth ==9 && this->birthday <=22)) 
	        return "处女座";
	    else if ((this->birthmonth ==9 && this->birthday >=23) || (this->birthmonth ==10 && this->birthday <=23)) 
	        return "天秤座";
	    else if ((this->birthmonth ==10 && this->birthday >=24) || (this->birthmonth ==11 && this->birthday <=22)) 
	        return "天蝎座";
	    else if ((this->birthmonth ==11 && this->birthday >=23) || (this->birthmonth ==12 && this->birthday <=21)) 
	        return "射手座";
	    else if ((this->birthmonth ==12 && this->birthday >=22) || (this->birthmonth ==1 && this->birthday <=19)) 
	        return "摩羯座";
	    else if ((this->birthmonth ==1 && this->birthday >=20) || (this->birthmonth ==2 && this->birthday <=18)) 
	        return "水瓶座";
	    else if ((this->birthmonth ==2 && this->birthday >=19) || (this->birthmonth ==3 && this->birthday <=20)) 
	        return "双鱼座";
	}
	
};
int main(){
	
	Person zhangsan;
	zhangsan.name="张三";
	zhangsan.birthyear=2003;
	zhangsan.birthmonth=7;
	zhangsan.birthday=20;
	
	//直接调用函数 
	zhangsan.showinfo(); 
	cout<<zhangsan.xingzuo();
	
	return 0;
} 

构造和析构函数

image

执行后的输出是
image

类的这个特性,在需要我们手动管理内存的地方非常有用。

posted @ 2025-04-30 17:00  一亩食堂  阅读(80)  评论(0)    收藏  举报