C++第3讲:类和对象的介绍,开始上硬菜

这篇博客探讨了C++中的类与结构体的区别,强调了封装的重要性。通过实例展示了如何在类中定义私有和公共成员,以及如何使用getter和setter方法实现数据访问。还涵盖了面向对象编程的概念,如继承和多文件组织。最后,文章通过立方体比较和点在圆内的判断展示了类的实际应用。
摘要由CSDN通过智能技术生成

在这里插入图片描述
这里定义在类的private下,在类外是没有访问权限的
在这里插入图片描述
在这里插入图片描述

#include<iostream>

#pragma warning(disable:4996) //注意这一句的使用,可以解决C语言中出现的一些兼容性问题
using namespace std;


struct Hero
{
	char name[10];
	int sex;

};

//这个一定要写在全局中才能实现对于结构体的打印,但是对于类而言,并不是这样的,还可以写在类里面
void print_info(struct Hero &s)
{
	cout << "name = " << s.name << endl;
	cout<< "sex = " << s.sex << endl;
}


class ADVhero
{
public:
	char name[10];
	int sex;

	void print_info()
	{
		cout << "name = " << name << endl;
		cout << "sex = " << sex << endl;
	}
};

class Animal
{
	//{}以内是内部 
public:
	//在public下面定义的成员变量和函数,是能够在类的内部和外部都可以进行访问的
	char kind[64];
	char color[64];

	//printAnimal在Animal的内部
	void printAnimal()
	{
		cout << "kind = " << kind << endl;
		cout << "color" << color << endl;
	}

	void write()
	{
		cout << kind << "开始写字了" << endl;
	}

	void run()
	{
		cout << kind << "开始跑不了" << endl;
	}
	//在private下面定义的成员变量和方法只能在类的内部进行访问
private:
	char kind1[64];
	char color1[64];


};
int main(void)
{

	Hero h;
	strcpy(h.name, "zhx");
	h.sex = 1;
	print_info(h);

	ADVhero advH;
	strcpy(advH.name, "zhx");
	advH.sex = 2;
	advH.print_info();


	cout << "----------------------" << endl;
	Animal dog;
	//这里访问kind和green,因为这两个在类的public下定义的,是可以直接进行访问的
	strcpy(dog.kind,"dog");
	strcpy(dog.color, "green"); 
	dog.printAnimal();

	Animal sheep;
	strcpy(sheep.kind, "sheep");
	strcpy(sheep.color, "white");
	sheep.printAnimal();
	
	dog.write();
	sheep.run();

	system("pause");
	return 0;
}

类的封装

#include<iostream>

#pragma warning(disable:4996) //注意这一句的使用,可以解决C语言中出现的一些兼容性问题
using namespace std;


class Hero
{
	int year;//public不写,默认的是private
};

//一个结构体默认的访问控制权是public
struct Hero2
{
	int year;
	void  print()
	{

	}
};


class MyDate
{
public:
	void init_date()
	{
		cout << "year,month,day" << endl;
		cin >> year;
		cin >> month;
		cin >> day;
	}

	void print_date()
	{
		cout << year << "年" << month << "月" << day << "日" << endl;
	}

	bool is_leap_year()
	{
		if( ((year % 4 == 0) && (year % 100 == 0)) || (year % 400 == 0))
		{
			return true;
		}
		return false;
	}

	int get_year()
	{
		return year;
	}

	void set_year(int new_year)
	{
		year = new_year;
	}
protected://保护控制权限,在类的继承中更private有区别,在单个类中,跟private是一样的
private:
	int year;
	int month;
	int day;
};
int main(void)
{

	MyDate my_date;
	my_date.init_date();
	my_date.print_date();
	if (my_date.is_leap_year() == true)
	{
		cout << "是闰年!" << endl;
	}
	else
	{
		cout << "不是闰年!" << endl;
	}
	//cout<<my_date.year 这个是不行的//所以思考,能否在类的内部定义一个成员函数,将需要访问的成员变量输出来

	//getter方法
	cout << my_date.get_year() << endl;

	//setter方法 
	//my_date.get_year = 2000;显然是不可以的
	my_date.set_year(2000);
	cout << my_date.get_year() << endl;

	system("pause");
	return 0;
}

面向对象案例学习:
在这里插入图片描述

#include<iostream>

#pragma warning(disable:4996) //注意这一句的使用,可以解决C语言中出现的一些兼容性问题
using namespace std;


class Rect
{

	 
};

//周长
double getCircleGirth(double r)
{
	return 2 * 3.14*r;
}

//面积
double getCircleArea(double r)
{
	return 3.14*r*r;
}

//用面向对象进行实现
class  Circle
{
public:
	void setR(double r)
	{
		m_r = r;
	}

	double getR()
	{
		return m_r;

	}
	double getGirth()
	{
		return 2 * 3.14*m_r;
	}

	double getCircleArea()
	{
		return 3.14*m_r*m_r;
	}

private:
	double m_r;//圆的私有成员
};



int main(void)
{

	double r = 10;//半径
	
	double g = 0;
	double a = 0;
	g = getCircleGirth(r);
	a = getCircleArea(r);
	cout << "面积:" << a << endl;
	cout << "周长:" <<g << endl;

	cout << "-------------------------" << endl;

	Circle circle;
	circle.setR(10);
	cout << "圆的半径" << circle.getR() << endl;
	cout << "面积:" <<circle.getCircleArea() << endl;
	cout << "周长:" << circle.getGirth() << endl;
	system("pause");
	return 0;
}

多文件使用,将上面的拆开到不同的文件中
主函数:

#include<iostream>
#include"circle.h"
using namespace std;

int main(void)
{


	Circle circle;
	circle.setR(10);
	cout << "圆的半径" << circle.getR() << endl;
	cout << "面积:" << circle.getCircleArea() << endl;
	cout << "周长:" << circle.getGirth() << endl;
	system("pause");
	return 0;
}

头文件函数

#pragma once
#ifndef __MAIN_H__
#define __MAIN_H__
#endif

//用面向对象进行实现
class  Circle
{
public:

	void setR(double r);//设置半径
	double getR();//得到半径
	double getGirth();//得到周长
	double getCircleArea();//得到面积


private:
	double m_r;//圆的私有成员
};


类定义文件

#include"circle.h"
#pragma warning(disable:4996) //注意这一句的使用,可以解决C语言中出现的一些兼容性问题


void Circle::setR(double r)
{
	m_r = r;
}

double Circle::getR()
{
	return m_r;
}
double Circle::getGirth()
{
	return 2 * 3.14*m_r;
}
double Circle::getCircleArea()
{
	return 3.14*m_r*m_r;
}



在这里插入图片描述
由上可知,分开进行书写,一方面方便阅读,一方面还可以进行接口的封装等。

判断两个立方体是否相等
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include"circle.h"
using namespace std;

//立方体类
class Cube
{
public:
	void setABC(int a, int b, int c)
	{
		m_a = a;
		m_b = b;
		m_c = c;
	}

	int getArea()
	{
		return (m_a*m_b) * 2 + (m_a*m_c) * 2 + (m_b*m_c) * 2;
	}

	int getVolume()
	{
		return (m_a*m_b*m_c);
	}

	int getA()
	{
		return m_a;
	}

	int getB()
	{
		return m_b;
	}

	int getC()
	{
		return m_c;
	}

	//同类直接无私处
	bool judgeCube(Cube &another) //another.getA可以换成another.m_a 。。。。。
	{
		if (m_a == another.getA() &&
			m_b == another.getB() &&
			m_c == another.getC())
		{
			return true;
		}
		else
		{
			return false;
		}
 
	}

private:
	int m_a;
	int m_b;
	int m_c;

};

//全局函数(在类外进行定义的)
bool judgeCube(Cube &c1, Cube &c2)
{
	if (c1.getA() == c2.getA() && c1.getB() == c2.getB() && c1.getC() == c2.getC())
	{
		return true;
	}
	else
	{
		return false;
	}
}
int main(void)
{
	Cube c1;
	c1.setABC(10, 20, 30);

	Cube c2;
	c2.setABC(10, 20, 30);

	cout << "c1的面积:" << c1.getArea() << endl;
	cout << "c1的体积:" << c1.getVolume() << endl;

	if (judgeCube(c1,  c2) == true)
		cout << "相等" << endl;
	else
	{
		cout << "不相等" << endl;
	}

	cout << "---------------------" << endl;
	if (c1.judgeCube(c2) == true)
		cout << "相等" << endl;
	else
	{
		cout << "不相等" << endl;
	}


	system("pause");
	return 0;
} 

在这里插入图片描述
在这里插入图片描述

#include<iostream>
using namespace std;


//点
class Point
{
public:
	void setXY(int x, int y)
	{
		m_x = x;
		m_y = y;
	}

	int getX()
	{
		return m_x;
	}

	int getY()
	{
		return m_y;
	}

private:
	int m_x;
	int m_y;
};


//一个比较真实的情况,我把point放在circle的下面了,然后一直报错,搞了半天没整明白,
//最后发现是在circle中使用了point中的数据,但是在使用前却没有定义,需要进行注意。
//圆类
class Circle
{
public:
	void setXY(int x, int y)
	{
		x0 = x;
		y0 = y;
	}

	void setR(int r)
	{
		m_r = r;
	}

	//提供一个判断点是否在圆内
	//true 在
	//false 不在
	bool judgePoint(Point &p)
	{
		int dd;
		dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0);
		if (dd > m_r*m_r)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

private:
	int x0;
	int y0;
	int m_r;
};




int main(void)
{

	Circle c;
	c.setXY(2, 2);
	c.setR(4);

	Point p1;
	p1.setXY(8, 8);

	if (c.judgePoint(p1) == true)
	{
		cout << "圆的内部" << endl;
	}
	else
	{
		cout << "圆的外部" << endl;
	}

	system("pause");
	return 0;
}


这里将上面的圆与点的关系拆分成为多个文件
在这里插入图片描述
头文件:
circle.h
point.h
源文件:
circle.cpp
point.cpp
main.cpp

**circle.h

#pragma once
#include"point.h"
//一个比较真实的情况,我把point放在circle的下面了,然后一直报错,搞了半天没整明白,
//最后发现是在circle中使用了point中的数据,但是在使用前却没有定义,需要进行注意。
//圆类
class Circle
{
public:
	void setXY(int x, int y);

	void setR(int r);

	//提供一个判断点是否在圆内
	//true 在
	//false 不在
	bool judgePoint(Point &p);

private:
	int x0;
	int y0;
	int m_r;
};

point.h

#pragma once
//点
class Point
{
public:
	void setXY(int x, int y);

	int getX();

	int getY();

private:
	int m_x;
	int m_y;
};

circle.cpp

#include"circle.h"
//一个比较真实的情况,我把point放在circle的下面了,然后一直报错,搞了半天没整明白,
//最后发现是在circle中使用了point中的数据,但是在使用前却没有定义,需要进行注意。
//圆类

void Circle::setXY(int x, int y)
{
	x0 = x;
	y0 = y;
}

void Circle::setR(int r)
{
	m_r = r;
}

//提供一个判断点是否在圆内
//true 在
//false 不在
bool Circle::judgePoint(Point &p)
{
	int dd;
	dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0);
	if (dd > m_r*m_r)
	{
		return false;
	}
	else
	{
		return true;
	}

}

point.cpp

#include"point.h"
//点

void Point::setXY(int x, int y)
{
	m_x = x;
	m_y = y;
}

int Point::getX()
{
	return m_x;
}

int Point::getY()
{
	return m_y;
}

main.cpp**

#include<iostream>
using namespace std;

#include"circle.h"
#include"point.h"

int main(void)
{

	Circle c;
	c.setXY(2, 2);
	c.setR(4);

	Point p1;
	p1.setXY(8, 8);

	if (c.judgePoint(p1) == true)
	{
		cout << "圆的内部" << endl;
	}
	else
	{
		cout << "圆的外部" << endl;
	}

	system("pause");
	return 0;
}

完整代码是为拆分的时候
C++类相关的学习(本讲完!)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值