小作业2-类和对象基础编程题

目录

1. (简答题)

2. (简答题)

3. (简答题)

4. (简答题)​​​​​​​


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;
}

### 回答1: 这是一个关于设计字符串类mystring的问题,其成员包括:char *headp(指向字符串的首指针),int len(字符串的长度),构造函数中申请空间,建立字符串,同时设置成员,构造函数中可以传进来空串。 ### 回答2: 字符串是程序中非常重要的一种数据类型,常常用于存储文本内容。为了更方便地处理字符串,可以自定义一个字符串类mystring。 首先,我们需要在mystring类中定义两个公有数据成员:char *headp和int len。其中,headp是一个指向字符串首地址的指针,len表示字符串的长度。这两个数据成员将会被用于操作字符串。 在构造函数中,我们需要对headp和len进行初始化。由于字符串长度不一,我们不知道需要申请多少空间,因此可以使用动态内存分配来实现。 首先,需要使用C++中的new关键字来分配字符串所需的内存空间。在申请完内存空间后,我们需要判断传进来的参数是否为空串。如果是,则简单地将字符串长度len设置为0;如果不是,则需要先计算字符串长度,然后将字符串内容复制到我们在前面申请的内存空间中。 可以通过以下代码来实现构造函数: ``` class mystring { public: char *headp; // 指向字符串首地址的指针 int len; // 字符串长度 mystring(const char *p = "") { // 构造函数 len = strlen(p); // 计算字符串长度 headp = new char[len + 1]; // 动态分配内存空间 if (len > 0) strcpy(headp, p); // 将字符串复制到内存空间中 } // 其他函数 }; ``` 需要注意的是,在mystring类中还需要定义析构函数和拷贝构造函数。析构函数用于在对象销毁时释放内存空间,而拷贝构造函数用于在对象拷贝时正确地处理内存空间。可以通过以下代码来实现这两个函数: ``` class mystring { public: char *headp; // 指向字符串首地址的指针 int len; // 字符串长度 mystring(const char *p = "") { // 构造函数 len = strlen(p); // 计算字符串长度 headp = new char[len + 1]; // 动态分配内存空间 if (len > 0) strcpy(headp, p); // 将字符串复制到内存空间中 } // 析构函数 ~mystring() { delete[] headp; // 释放内存空间 } // 拷贝构造函数 mystring(const mystring &str) { len = str.len; headp = new char[len + 1]; strcpy(headp, str.headp); } // 其他函数 }; ``` 除了构造函数、析构函数和拷贝构造函数外,我们还可以在mystring类中定义许多其他有用的函数,如字符串拼接、子串查找、字符串赋值等等。通过自定义mystring类,我们能够更加灵活高效地处理字符串,提高程序的可读性和可维护性。 ### 回答3: 设计一个字符串类mystring,其主要任务是管理和存储字符串,类中的公有数据成员有:char *headp和int len。char *headp是一个指向字符串首指针的指针,int len存储了字符串的长度。构造函数中需要申请空间,建立字符串,并设置数据成员。构造函数可以传入空串。 类的定义如下: ```c++ class mystring { private: char* headp; int len; public: mystring(); mystring(const char* str); mystring(const mystring& str); ~mystring(); mystring& operator=(const mystring& str); mystring& operator+(const mystring& str); int size(); char* c_str(); }; ``` 构造函数有三个,分别是默认构造函数、传入C字符串的构造函数和复制构造函数。析构函数用于释放内存。重载了赋值运算符和加法运算符,分别用于赋值和拼接字符串。size()函数返回字符串长度,c_str()函数返回C格式的字符串,用于直接输出字符串内容。 示例代码: ```c++ #include <iostream> #include <cstring> using namespace std; class mystring { private: char* headp; int len; public: mystring() : headp(nullptr), len(0) {} mystring(const char* str) { len = strlen(str); if (len == 0) headp = nullptr; else { headp = new char[len + 1]; strcpy(headp, str); } } mystring(const mystring& str) { len = str.len; if (len == 0) headp = nullptr; else { headp = new char[len + 1]; strcpy(headp, str.headp); } } ~mystring() { if (len > 0) delete[] headp; } mystring& operator=(const mystring& str) { if (headp == str.headp) return *this; if (len > 0) delete[] headp; len = str.len; if (len == 0) headp = nullptr; else { headp = new char[len + 1]; strcpy(headp, str.headp); } return *this; } mystring& operator+(const mystring& str) { len += str.len; char* temp = new char[len + 1]; if (headp != nullptr) strcpy(temp, headp); strcat(temp, str.headp); if (headp != nullptr) delete[] headp; headp = temp; return *this; } int size() { return len; } char* c_str() { return headp; } }; int main() { mystring a("hello "), b("world"); mystring c(a); cout << c.c_str() << endl; c = a + b; cout << c.c_str() << endl; cout << c.size() << endl; return 0; } ``` 以上便是设计一个字符串类mystring的实现,类中重载了不少运算符,使得操作字符串变得简单易用。注意内存管理的问题,在合适的地方申请释放内存,否则会造成内存泄漏和段错误等问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值