当前位置: 首页 > news >正文

第22课 类和对象

文章目录

  • 前言
  • 一、自定义数据类型——类
  • 二、构造函数与析构函数
  • 三、类类型的变量——对象
    • 1. 对象的定义
    • 2. 对象成员的访问
    • 3. 定义一个长方形的类Rectangle
    • 4. 定义一个圆形的类Circle
    • 5. 定义一个数据成员由三角形的3条边构成的三角形类Triangle
  • 课后练习
    • 1. 定义一个简单的日期类Date
    • 3. 定义一个简单的时间类Time


前言

`本课介绍了以下内容。

  1. 类的定义
  2. 构造函数和析构函数
  3. 对象的定义
  4. 对象的访问
  5. 关键字:class, private, protected, public

一、自定义数据类型——类

程序如何实现模块化?我们已经学过函数和结构体,它们一个是算法的模块化,一个是数据的模块化,二者能否结合呢?自然是可以的,类就是一种这样的自定义类型,它里面既可以有数据又可以又函数。
表22-1 成员权限说明

关键字权限说明
private私有成员,可以被同类的其他成员、友元访问通常将数据成员设定为私有,以保护数据
protected保护成员,可被同类的其他成员、友元和子类访问权限介于私有和公有之间
public公有成员,可被任何能看到这个类的地方访问通常将成员函数设定为公有

二、构造函数与析构函数

构造函数是一种特殊的成员函数,一般用来完成成员变量的初始化。
特殊1:构造函数的名称就是类的名称。
特殊2:构造函数没有返回值类型说明。
特殊3:类中可以不定义构造函数,C++编译器会自动添加。

三、类类型的变量——对象

1. 对象的定义

2. 对象成员的访问

3. 定义一个长方形的类Rectangle

#include<iostream>
using namespace std;

class Rectangle {
	private:
		float width, height;
	public:
		Rectangle() {
		}	//无参构造函数
		Rectangle(float w, float h) {
			width = w;
			height = h;
		}	//带参构造函数
		void set_values(float w, float h) {
			width = w;
			height = h;
		}
		float area() {
			return width*height;
		}	// 公有成员函数
};

int main() {
	float a, b;
	cout << "Input the width and height of the rectangle: " << endl;
	cin >> a >> b;
	Rectangle rectangle1;
	rectangle1.set_values(a, b);
	cout << "The area of the rectangle is: ";
	cout << rectangle1.area() << endl;
	Rectangle rectangle2(4, 5);
	cout << "The area of the next rectangle is: ";
	cout << rectangle2.area() << endl;

	return 0;
}

4. 定义一个圆形的类Circle

#include<iostream>
#include<cmath>	// M_PI
using namespace std;

class Circle {
	private:
		double r;
	public:
		void set_r(double x) {
			r = x;
		}
		double circumference() {
			return 2*M_PI*r;
		}
		double area() {
			return M_PI*r*r;
		}
};

int main() {
	Circle circle1;
	double r;
	cout << "Input the circle radius: ";
	cin >> r;
	circle1.set_r(r);
	cout << "Circumference: " << circle1.circumference() << endl;
	cout << "Area: " << circle1.area() << endl;

	return 0;
}

5. 定义一个数据成员由三角形的3条边构成的三角形类Triangle

#include<iostream>
#include<iomanip>

class Triangle {
	private:
		float a,b,c;
	public:
		Triangle(float edge1, float edge2,float edge3) {
			if(edge1+edge2>edge3 && edge2+edge3>edge1 && edge1+edge3>edge2) {
				a = edge1;
				b = edge2;
				c = edge3;
			} else {
				a = 0;
				b = 0;
				c = 0;
			}
		}
		float area() {
			float s = (a+b+c)/2;
        	                      return (float)sqrt(s*(s-a)*(s-b)*(s-c));
		}
		bool isRightAngle() {
			if((a*a+b*b==c*c) || (a*a+c*c==b*b) || (b*b+c*c==a*a)) 
				return true;
			else
				return false;
		}
		bool isIsosceles() {
			if(a==b || a==c || b==c) return true;
			else return false;
		}
		bool isEquilateralTriangle() {
			if(a==b && a==c) return true;
			else return false;
		}
};

int main() {
    float a,b,c;
    cout << "请输入三角形的三条边:" << endl;
    cin >> a >> b >> c;
    if(a+b>c && b+c>a && a+c>b)		{
    	Triangle triangle1(a, b, c);
        cout << "三角形的面积是:" << fixed << setprecision(2) << triangle1.area() << endl;
        if(triangle1.isEquilateralTriangle())
            cout << "三角形是等边三角形." << endl;
        else if(triangle1.isIsosceles())
            cout << "三角形是等腰三角形." << endl;
        else if(triangle1.isRightAngle())
            cout << "三角形是直角三角形." << endl;
        else
            cout << "三角形是普通三角形" << endl;
    }
    else
        cout << "不能构成三角形" << endl;
    return 0;
}



课后练习

1. 定义一个简单的日期类Date

#include<iostream>
using namespace std;

class Date {
	private:
		int day, month, year;
	public:
		void setDay(int d);
		void setMonth(int m);
		void setYear(int y);
		void setDate(int d, int m, int y);
		void printDateA();
		void printDateE();
};

void Date::setDay(int d) {
	day = d;
}
void Date::setMonth(int m) {
	month = m;
}
void Date::setYear(int y) {
	year = y;
}

void Date::setDate(int d, int m, int y) {
	day = d;
	month = m;
	year = y;
}

void Date::printDateA() {
	cout << month << '/' << day << '/' << year << endl; // American
}

void Date::printDateE() {
	cout << day << '/' << month << '/' << year << endl; // English
}

int main() {
	Date date;
	date.setDate(25, 12, 2023);
	date.printDateA();
	date.printDateE();
	return 0;
}

3. 定义一个简单的时间类Time

#include<iostream>
using namespace std;

class Time {
	private:
		int hour=0, minute=0, second=0;
	public:
		Time() {
		}
		Time(int h, int m, int s) {
			hour = h;
			minute = m;
			second = s;
		}
		void printTime() {
			cout << hour << ':' << minute << ':' << second << endl;
		}
};

int main() {
	Time time1(9, 0, 0), time2(17, 30, 9);
	time1.printTime();
	time2.printTime();

	return 0;
}

相关文章:

  • 浅谈数字孪生的应用与发展
  • 苹果账号被禁用怎么办?
  • 2023十大编程语言及未来展望
  • 大模型LLM的微调技术:LoRA
  • 软件测试面试题——如果保证测试用例覆盖率
  • cpp_07_类型转换构造_析构函数_深拷贝_静态成员
  • lodash源码分析每日一练 - 数组 - intersection / intersectionBy / intersectionWith
  • mp4视频转rosbag文件(图片压缩格式)
  • Flink导入StarRocks
  • 【音视频 ffmpeg 学习】 跑示例程序 持续更新中
  • Python圣诞树代码
  • 记录一下imx6ull linux 5.10.9多点电容触摸屏驱动报错问题解决方法
  • HTML---JavaScript基础
  • Go语言学习第二天
  • 云计算IaaS、PaaS和SaaS之
  • nodejs微信小程序+python+PHP的冷链物流配送系统-计算机毕业设计推荐
  • 查看ios app运行日志
  • 微服务(2)
  • 系列十一、解压文件到指定目录
  • Unity中Shader裁剪空间推导(在Shader中使用)
  • 家国万里·时光故事会|科学家伉俪,用玉米书写家国情怀
  • 俄需要达成怎样的特别军事行动结果?普京:包含四个方面
  • 哈马斯与以色列在多哈举行新一轮加沙停火谈判
  • 2025年上海科技节开幕,人形机器人首次登上科学红毯
  • 一个留美学生的思想转向——裘毓麐的《游美闻见录》及其他
  • 江苏省委组织部副部长高颜已任南京市委常委、组织部部长