目录
1. (简答题)
设计一个学生基本信息类Student,学生的基本信息有公有属性:学号、姓名,保护属性:性别,私有属性:联系电话。成员函数有设置联系电话SetTel和获取联系电话GetTel,设置性别SetSex和获取性别GetSex,输出学生所有基本信息OutputInfo,这些函数都是公有成员函数,OutputInfo函数在类外定义,其他函数在类内定义。
编写主程序对一个班的学生信息进行管理(班级学生总数不超过100人),需要的功能有:增加、修改、删除和查找,每个功能用一个函数来实现。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Student{
public:
string num; //学号
string name; //姓名
protected:
string sex; //性别
private:
string phoneNum; //电话号码
public:
/*性别和电话的Getter and Setter*/
const string &getSex() const {
return sex;
}
void setSex(const string &sex) {
Student::sex = sex;
}
const string &getPhoneNum() const {
return phoneNum;
}
void setPhoneNum(const string &phoneNum) {
Student::phoneNum = phoneNum;
}
/*输出学生所有信息*/
void OutputInfo();
};
/*输出学生所有信息*/
void Student::OutputInfo() {
cout<<"学号:"<<Student::num<<endl;
cout<<"姓名:"<<Student::name<<endl;
cout<<"性别:"<<Student::getSex()<<endl;
cout<<"联系电话:"<<Student::getPhoneNum()<<endl;
}
bool operator==(const Student &t1,const string t2){
return t1.num == t2;
}
vector<Student> students;
/**
* 函数声明区
*/
void searchStu(string num);
void modifyStu(string num);
void deleteStu(string num);
void addStu();
int main(){
/**
* 1.添加 2.修改 3.删除 4.查找
*/
int choice;
string num;
while (true){
cin>>choice;
switch (choice) {
case 1:
addStu();
break;
case 2:
cout<<"请输入要修改的学生学号:"<<endl;
cin>>num;
modifyStu(num);
break;
case 3:
cout<<"请输入要删除的学生学号:"<<endl;
cin>>num;
deleteStu(num);
break;
case 4:
cout<<"请输入要查找的学生学号:"<<endl;
cin>>num;
searchStu(num);
break;
}
}
}
/**
* 查找
*/
void searchStu(string num){
//容器为空直接返回
if (students.empty()) return;
//要查找的学生
vector<Student>::iterator stu = find(students.begin(),students.end(),num);
if (stu == students.end()){
cout<<"没有该学生"<<endl;
} else {
stu->OutputInfo();
}
}
/**
* 修改
*/
void modifyStu(string num){
//容器为空直接返回
if (students.empty()) return;
//要修改的学生
vector<Student>::iterator stu = find(students.begin(),students.end(),num);
if (stu == students.end()){
cout<<"没有该学生"<<endl;
} else {
cout<<"输入该学生新的信息:"<<endl;
//设置姓名
cout<<"姓名:"<<endl;
cin>>stu->name;
//设置联系电话
cout<<"联系电话:"<<endl;
string phoneNum;
cin>>phoneNum;
stu->setPhoneNum(phoneNum);
}
}
/**
* 删除
*/
void deleteStu(string num){
//容器为空直接返回
if (students.empty()) return;
//要删除的学生
vector<Student>::iterator stu = find(students.begin(),students.end(),num);
if (stu == students.end()){
cout<<"没有该学生"<<endl;
} else {
students.erase(stu);
cout<<"删除成功"<<endl;
}
}
/**
* 增加
*/
void addStu(){
Student stu;
cout<<"请输入要添加的学生信息:"<<endl;
string sex,phoneNum;
cout<<"学号:"<<endl;
cin>>stu.num;
cout<<"姓名:"<<endl;
cin>>stu.name;
cout<<"性别:"<<endl;
cin>>sex;
stu.setSex(sex);
cout<<"联系电话:"<<endl;
cin>>phoneNum;
stu.setPhoneNum(phoneNum);
students.push_back(stu);
}
2. (简答题)
设计一个字符串类MyString,公有数据成员有:char *HeadP(指向字符串的首指针),int Len(字符串的长度),构造函数中申请空间,建立字符串,并设置数据成员,构造函数中可以传进来空串字符或者一串字符,并且可以有默认值,默认值字符串为“String Example!”,析构函数释放空间。
再编写主程序,输入和输出一个字符串类对象和它的长度,然后通过已有的字符串类对象拷贝一个新的字符串类对象,采用拷贝构造函数实现,在输出新的字符串类对象及其长度。(注意:拷贝构造函数中需要用到深拷贝实现,否则程序会出错。)
#include <iostream>
#include <string>
using namespace std;
class MyString{
public:
char *HeadP;
int Len;
MyString(string s,int len);
~MyString();
MyString(const MyString& myString);
};
MyString::MyString(string s,int len) {
MyString::Len = len;
//如果len = -1,那么就设置默认值
if(len == -1){
s = "String Example!";
MyString::Len = 15;
MyString::HeadP = (char*) malloc(sizeof (char) * MyString::Len);
MyString::HeadP = s.data();
} else {
MyString::Len = len;
MyString::HeadP = (char*) malloc(sizeof (char) * MyString::Len);
MyString::HeadP = s.data();
}
}
MyString::~MyString() {
free(MyString::HeadP);
}
MyString::MyString(const MyString &myString) {
MyString::HeadP = (char*) malloc(sizeof (char) * myString.Len);
MyString::HeadP = myString.HeadP;
MyString::Len = myString.Len;
}
int main() {
int len;
string s;
cin>>len>>s;
MyString myString_1(s,len);
MyString myString_2 = myString_1;
cout<<myString_2.Len<<" "<<myString_2.HeadP<<endl;
return 0;
}
3. (简答题)
设计一个职工类Employee,属性有:编号Num、姓名Name、住址Addr、公司名称CName、出生日期EDate。其中:出生日期是一个Date类的子对象(需要事先编写Date类),属性是年、月、日。Num是常成员,CName是静态成员。Employee的行为有:设置生日SetDate和获取生日GetDate,输入InInfo和输出OutInfo等。InInfo和OutInfo是输入和输出Employee类的基本属性,OutInfo是常成员函数,InInfo是静态成员函数。类成员的访问属性自定义。
再编写主程序,对2个职工类对象信息进行数据的设置和输入、输出。
#include <iostream>
using namespace std;
/**
* 日期类
*/
class Date{
public:
int year; //年
int month; //月
int day; //日
};
/**
* 职工类
*/
class Employee{
public:
int Num; //编号
string Name; //姓名
string Addr; //地址
static string CName; //公司名称
Date EDate; //出生日期
public:
//设置生日
void SetDate(int year,int month,int day){
Employee::EDate.year = year;
Employee::EDate.month = month;
Employee::EDate.day = day;
}
//获取生日
const Date &getEDate() const {
return EDate;
}
//输入Employee类的基本属性
static Employee InInfo(){
int num;
string name;
string addr;
string cname;
Date edate;
cout<<"输入编号:"<<endl;
cin>>num;
cout<<"输入姓名:"<<endl;
cin>>name;
cout<<"输入地址:"<<endl;
cin>>addr;
cout<<"输入生日年份:"<<endl;
cin>>edate.year;
cout<<"输入生日月份:"<<endl;
cin>>edate.month;
cout<<"输入生日日期:"<<endl;
cin>>edate.day;
Employee employee(num,name,addr,edate);
return employee;
}
//输出Employee类的基本属性
const void OutInfo() const{
cout<<"编号:"<<Employee::Num<<endl;
cout<<"姓名:"<<Employee::Name<<endl;
cout<<"住址:"<<Employee::Addr<<endl;
cout<<"公司名称:"<<Employee::CName<<endl;
cout<<"出生日期:"<<Employee::EDate.year<<"年"<<Employee::EDate.month<<"月"<<Employee::EDate.day<<"日"<<endl;
}
Employee(int Num,string Name,string Addr,Date EDate){
this->Num = Num;
this->Name = Name;
this->Addr = Addr;
this->EDate = EDate;
}
Employee(){
}
};
string Employee::CName = "好多人";
int main() {
system("chcp 65001");
/**
* 职工1
*/
//数据设置
Employee x;
x.SetDate(2003,10,10); //设置生日
x.Num = 001;
x.Name = "赵玥萱";
x.Addr = "天元区泰山路88号";
//打印
x.OutInfo();
/**
* 职工2
*/
//数据输入
Employee y = Employee::InInfo();
y.OutInfo();
return 0;
}
4. (简答题)
设计一个名为Rectangle的矩形类,其私有属性为矩形的左上角和右下角两个点的坐标,成员函数能计算和输出矩形的周长,并统计程序中目前存在矩形类对象的个数,然后写一个友元函数求矩形的面积。
#include <iostream>
#include <cmath>
using namespace std;
/**
* 坐标类
*/
class Coordinate{
public:
double x; //横坐标
double y; //纵坐标
public:
Coordinate(double x,double y){
this->x = x;
this->y = y;
}
Coordinate() {}
};
/**
* 矩形类
*/
class Rectangle{
public:
static int count; //计数器
private:
Coordinate left_up; //左上角坐标
Coordinate right_down; //右下角坐标
public:
/*声明友元函数求矩形面积*/
friend double calArea(Rectangle&);
/*计算周长,统计矩形类个数*/
int calCir(){
double res; //计算结果
double length = fabs(Rectangle::right_down.x - Rectangle::left_up.x); //长
double height = fabs(Rectangle::left_up.y - Rectangle::right_down.y); //高
res = 2*(length + height);
cout<<"矩形的周长是:"<<res<<endl;
cout<<"目前存在的矩形类对象个数为:"<<count<<endl;
return count;
}
/*构造器*/
Rectangle(Coordinate a,Coordinate b){
left_up = a;
right_down = b;
count++; //调用构造函数后计数器+1
}
};
/*计算矩形面积*/
double calArea(Rectangle& rectangle){
double res; //计算结果
double length = fabs(rectangle.right_down.x - rectangle.left_up.x); //长
double height = fabs(rectangle.left_up.y - rectangle.right_down.y); //高
res = length * height;
return res;
}
int Rectangle::count = 0;
int main() {
system("chcp 65001");
/**
* 测试,计算面积
*/
Coordinate left_1 = Coordinate(0, 6);
Coordinate right_1 = Coordinate(4, 0);
Rectangle rectangle_1 = Rectangle(left_1, right_1);
double res = calArea(rectangle_1);
cout<<"矩形的面积是:"<<res<<endl;
/**
* 测试,算周长和个数
*/
Coordinate left_2 = Coordinate(1,8);
Coordinate right_2 = Coordinate(5,2);
Rectangle rectangle_2 = Rectangle(left_2,right_2);
rectangle_2.calCir();
return 0;
}