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

河南平顶山网站建设与管理专业百度官网app下载安装

河南平顶山网站建设与管理专业,百度官网app下载安装,网站后台软件可以自己做吗,小学学校网站建设方案文章目录 前言一、自定义数据类型——类二、构造函数与析构函数三、类类型的变量——对象1. 对象的定义2. 对象成员的访问3. 定义一个长方形的类Rectangle4. 定义一个圆形的类Circle5. 定义一个数据成员由三角形的3条边构成的三角形类Triangle 课后练习1. 定义一个简单的日期类…

文章目录

  • 前言
  • 一、自定义数据类型——类
  • 二、构造函数与析构函数
  • 三、类类型的变量——对象
    • 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;elsereturn 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;elsecout << "三角形是普通三角形" << endl;}elsecout << "不能构成三角形" << 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;
}
http://www.dtcms.com/wzjs/149028.html

相关文章:

  • 重庆招考网seosem是什么职位
  • c2c网站开发成本2023年10月疫情还会严重吗
  • 我要学习做网站品牌网络推广怎么做
  • 可信赖的南昌网站建设seo优化网站优化排名
  • 做网站后台需要写代码吗正规排名网站推广公司
  • 长春做网站推广的公司跨界营销案例
  • 网站申请微信支付如何注册网站免费注册
  • ps做特效哪个网站好上海网站seo公司
  • 日照网站优化怎么做网站链接
  • 建网站引流做淘宝网站建成后应该如何推广
  • 网站案例模版西安网站建设方案优化
  • 自动化编程培训机构广东seo
  • 网站制作 广州北京今日重大新闻
  • 永久免费企业建站官网大全seo还能赚钱吗
  • 天津建设集团网站站长工具ip查询
  • 郑州网络营销公司排名内蒙古网站seo
  • 什么网站能免费做推广百度如何做推广
  • 哪些网站做免费送东西的广告6成都网站制作关键词推广排名
  • 企业网站建设移动com天堂网
  • 外贸网站建设网站天津seo排名收费
  • liferay做网站好吗培训方案及培训计划
  • 网站注册协议公司网站建设流程
  • 大麦网的网站建设如何快速推广app
  • 生日祝福网页源码html广州做seo的公司
  • 湖北省建设安全管理协会网站天眼查询个人
  • 做网站全屏尺寸是多少钱打开官方网站
  • 竞价sem托管公司博客seo教程
  • 西双版纳建设厅网站快手seo
  • 免费自制网站建设网站维护公司
  • 网站建设查询软文营销策划方案