一.
1.按下列要求编程:
(1)定义一个描述矩形的类Rectangle,包括的数据成员有宽(width)和长(length);
(2)计算矩形周长;
(3)计算矩形面积;
(4)改变矩形大小。
代码示例一
#include<iostream>
using namespace std;
//定义一个描述矩形的类Rectangle
class Rectangle {
//设置为私有数据成员
private:
double width; //宽
double length; //长
public:
//构造函数,初始化宽和长
Rectangle(double w, double l) {
width = w;
length = l;
}
//计算矩形周长的函数
double Perimeter() {
return 2 * (width + length);
}
//计算矩形面积的函数
double Area() {
return width * length;
}
//改变矩形大小的函数
void Resize(double w, double l) {
width = w;
length = l;
}
};
int main() {
double w, l;
cout << "请输入矩形的宽:";
cin >> w;
cout << "请输入矩形的长:";
cin >> l;
//创建矩形对象
Rectangle rect(w, l);
//输出矩形的周长和面积
cout << "矩形的周长为:" << rect.Perimeter() << endl;
cout << "矩形的面积为:" << rect.Area() << endl;
//改变矩形的大小
cout << "请重新输入矩形的宽:";
cin >> w;
cout << "请重新输入矩形的长:";
cin >> l;
rect.Resize(w, l);
//输出改变后的矩形周长和面积
cout << "改变后的矩形周长为:" << rect.Perimeter() << endl;
cout << "改变后的矩形面积为:" << rect.Area() << endl;
return 0;
}
二.
2.有以下程序
#include <iostream>
using namespace std;
class Time
{public:
int hour;
int minute;
int sec;
};
int main( )
{
Time t;
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
cout<<t.hour<<″:″<<t.minute<<″:″<<t.sec<<endl;
return 0;
}
要求:
(1) 将数据成员改为私有的;
(2) 将输入和输出的功能改为由成员函数实现;
(3) 在类体内定义成员函数。
代码示例二
#include <iostream>
using namespace std;
class Time {
private:
int hour;
int minute;
int sec;
public:
void getInput() {
cin >> hour;
cin >> minute;
cin >> sec;
}
void display() {
cout << hour << ":" << minute << ":" << sec << endl;
}
};
int main() {
Time t;
t.getInput();
t.display();
return 0;
}
解析
在这个版本中,hour,minute和sec变量都被设置为私有的,这意味着它们只能在Time类内部访问。
对于输入和输出功能,我添加了两个新的成员函数。getInput()函数负责从用户获取时间,display()函数负责显示时间。这两个函数都是在类体内定义的。
在main()函数中,我创建了一个Time类的对象t,然后使用这个对象调用getInput()和display()函数,以获取和显示时间。
三.
3.创建一个employee类,该类中有姓名(name)、城市(city)、邮政编码(postCode)等数据成员。成员函数setValue设定对象数据,display函数输出对象数据。
具体要求如下:
(1)数据成员设定成private
(2)成员函数设定成public
(3)建立指针对象,利用指针对象对成员进行引用
(4)将类声明和成员函数的定义分开,放到.h和.cpp中
(5) 建立main函数并验证
代码示例三
首先,我们需要创建一个名为"Employee.h"的头文件:
#include <string>
class Employee {
private:
std::string name;
std::string city;
std::string postCode;
public:
Employee(); // 构造函数
void setValue(std::string n, std::string c, std::string p);
void display() const;
};
然后,我们创建一个名为"Employee.cpp"的文件,以定义成员函数:
#include <iostream>
#include "Employee.h"
Employee::Employee() {
name = "";
city = "";
postCode = "";
}
void Employee::setValue(std::string n, std::string c, std::string p) {
name = n;
city = c;
postCode = p;
}
void Employee::display() const {
std::cout << "姓名: " << name << std::endl;
std::cout << "城市: " << city << std::endl;
std::cout << "邮编: " << postCode << std::endl;
}
最后,我们在main函数中测试这个类:
#include <iostream>
#include "Employee.h"
int main() {
Employee* e1 = new Employee();
e1->setValue("张三", "杭州", "10001");
e1->display();
delete e1;
return 0;
}
在上述代码中,我们首先创建了一个Employee类的对象指针,然后使用此指针设置并显示Employee的值,最后删除指针。
四
创建Student类,在main函数中输入3个学生数据,包括学号(stuID),姓名(name)和成绩(score),要求只输出成绩在80~90分数段的学生数据。
具体要求如下:
(1)数据成员设定成private
(2)成员函数设定成public
(3)将类声明和成员函数的定义分开,放到.h和.cpp中
(4) 建立main函数并验证
代码示例四
main.cpp
#include <iostream>
#include<string>
#include "Student.h"
using namespace std;
int main()
{
string stuID;
string name;
float score;
Student* stu[3];
cout << "输入3个学生的数据" << endl;
for (int i = 0; i < 3; i++) {
cin >> stuID >> name >> score;
stu[i] = new Student(stuID, name, score);
}
cout << "学号 姓名 成绩\n";
for (Student *stud:stu){
score = stud->getScore();
if(score>=80&&score<90)
cout << stud->getStuID() <<" " << name << " "<< score<<endl;
}
}
/*
201 a1 60
301 a2 100
401 a3 85
*/
Student.h
#pragma once
#include <string>
using namespace std;
class Student
{
private:
string stuID;
string name;
float score;
public:
Student();
Student(string stuID_, string name_, float score_);
string getStuID();
string getName();
float getScore();
};
Student.cpp
#include "Student.h"
Student::Student(){}
Student::Student(string stuID_, string name_, float score_) {
stuID = stuID_;
name = name_;
score = score_;
}
string Student::getStuID() {
return stuID;
}
string Student::getName() {
return name;
}
float Student::getScore() {
return score;
}
停留在世界边缘,与之惜别