凡科网 免费网站,成都旅游景点排名前十,视频直播平台哪个好,网站关键词写在哪里作业#xff1a;
1.整理思维导图 2.整理课上代码
3.把课上类的三个练习题的构造函数写出来
函数全部类内声明#xff0c;类外定义 定义一个矩形类Rec#xff0c;包含私有属性length、width#xff0c;包含公有成员方法#xff1a; void set_length(int l); //设置长度v…作业
1.整理思维导图 2.整理课上代码
3.把课上类的三个练习题的构造函数写出来
函数全部类内声明类外定义 定义一个矩形类Rec包含私有属性length、width包含公有成员方法 void set_length(int l); //设置长度void set_width(int w); //设置宽度int get_length(); //获取长度将长度的值返回给调用处int get_width(); //获取宽度将宽度的值返回给调用处void show(); //输出周长和面积
#include iostream
using namespace std;class Rec {
private:int length; // 私有属性长度int width; // 私有属性宽度
public:void setLength(int l); // 设置长度void setWidth(int w); // 设置宽度int getLength(); // 获取长度int getWidth(); // 获取宽度void show(); // 输出周长和面积
};void Rec::setLength(int l) {length l;
}void Rec::setWidth(int w) {width w;
}int Rec::getLength() {return length;
}int Rec::getWidth() {return width;
}void Rec::show() {cout 周长: 2 * (length width) endl;cout 面积: length * width endl;
}int main() {Rec a;int l, w;cout 输入长度;cin l;a.setLength(l);cout 输入宽度;cin w;a.setWidth(w);cout 矩形的周长和面积为: endl;a.show();return 0;
}定义一个圆类包含私有属性半径r公有成员方法 void set_r(int r); //获取半径void show //输出周长和面积show函数中需要一个提供圆周率的参数PI该参数有默认值3.14
#include iostream
#include iomanip // 用于设置输出格式
using namespace std;class Circle {
private:double radius; // 私有属性半径
public:void setR(double r); // 设置圆的半径void show(double PI 3.14); // 输出周长和面积
};void Circle::setR(double r) {radius r;
}void Circle::show(double PI) {double circumference 2 * PI * radius; // 计算周长double area PI * radius * radius; // 计算面积cout 周长: fixed setprecision(2) circumference endl;cout 面积: fixed setprecision(2) area endl;
}int main() {Circle c; // 创建圆类的对象 cdouble r;cout 输入圆的半径: ;cin r;c.setR(r); // 设置半径cout 圆的周长和面积为: endl;c.show();return 0;
}定义一个Car类包含私有属性颜色color品牌brand速度speed包含公有成员方法 void display(); //显示汽车的品牌颜色和速度void acc(int a); //加速汽车set函数设置类中的私有属性
#include iostream
#include string // 引入字符串库
using namespace std;class Car {
private:string color; // 汽车颜色string brand; // 汽车品牌int speed; // 汽车速度
public:void set(string c, string b, int s); // 设置汽车的属性void display(); // 显示汽车信息void acc(int a); // 加速汽车
};void Car::set(string c, string b, int s) {color c;brand b;speed s;
}void Car::display() {cout 品牌: brand , 颜色: color , 速度: speed km/h endl;
}void Car::acc(int a) {speed a; // 增加速度cout 加速 a km/h, 当前速度: speed km/h endl;
}int main() {Car myCar; // 创建Car类的对象myCarmyCar.set(红色, 奥迪, 0); // 设置汽车的初始属性cout 初始状态: endl;myCar.display();myCar.acc(50); // 加速50 km/hmyCar.acc(30); // 再加速30 km/hreturn 0;
}