第22课 类和对象


前言

`本课介绍了以下内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值