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

汽车b2c网站建设移动营销

汽车b2c网站建设,移动营销,做plc课程设计的网站,陕西 网站备案文章目录 前言一、自定义数据类型——类二、构造函数与析构函数三、类类型的变量——对象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/219305.html

相关文章:

  • wordpress文章默认经典上海搜索seo
  • 怎样做后端数据传输前端的网站seo基础教程视频
  • 国际网站怎么建设腾讯中国联通
  • 电脑上怎么创建wordpress指定关键词seo报价
  • 做一个搜索引擎网站要多少钱如何快速搭建网站
  • 电子行业网站东莞做网站公司首选
  • 网站后台添加关键词站长统计幸福宝
  • 一个叫mit做app的网站三只松鼠口碑营销案例
  • 采集软件seo企业推广案例
  • 深圳外贸soho网站建设站长之家域名查询
  • 自己做网站排名谷歌外贸网站推广
  • adobe 网站开发软件有哪些百度浏览器网址链接
  • 做网站素材图片大连百度关键词优化
  • 新乡公司做网站百度收录需要多久
  • 电工应用技术网站资源建设商业软文怎么写
  • b2b 网站系统营销网站建设创意
  • 哪个网站做美食视频网站好网站优化的方法与技巧
  • adobe网站制作网站域名查询地址
  • 专门做二手书网站或app百度竞价排名公司
  • 福州哪家专业网站设计制作最好磁力狗最佳搜索引擎
  • 广州学习网站建设搜索网
  • 网站文字超链接怎么做百度官方网首页
  • 优秀原创设计网站百度搜索简洁版网址
  • 邯郸整站优化互联网营销师考试题库
  • 网页设计尺寸用1440还是1920白山seo
  • 网站建设报价方案模板外链推广软件
  • wordpress simplepie网络seo首页
  • ui设计的网站女教师网课入侵录屏冫
  • 门户网站和搜索网站的区别东莞seo推广公司
  • 城乡建设和住房建设官网文山seo