嵌入式Linux C++练习1——类和对象1

1. 定义一个Dog类,包含了 age , weight等属性,以及对这些属性操作的方法。实现并测试这个类。

#include <iostream>
using namespace std;

class Dog
{
    
    
public:
    void setdata()
    {
    
    
        cin >> name >> age >> sex >> weight;
    }
    void GetName()
    {
    
    
        cout << "它的名字叫" << name << "。" << endl;
    }
    void GetAge()
    {
    
    
        cout << "它今年" << age << "岁。" << endl;
    }
    void GetSex()
    {
    
    
        if (sex == 'm')
            cout << "是公的。" << endl;
        else
            cout << "是母的。" << endl;
    }
    void GetWeight()
    {
    
    
        cout << "体重" << weight << "千克。" << endl;
    }

private:
    char name[20];
    int age;
    char sex;
    double weight;
};

int main()
{
    
    
    Dog d;

    cout << "依次输入狗的姓名,年龄,性别,体重:" << endl;

    d.setdata();
    cout << "这是我的狗:" << endl;
    d.GetName();
    d.GetAge();
    d.GetSex();
    d.GetWeight();

    return 0;
}

2. 设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标计算矩形的面积

#include <iostream>
using namespace std;

class Rectangle
{
    
    
public:
    void initial(float a1, float b1, float a2, float b2)
    {
    
    
        x1 = a1;
        y1 = b1;
        x2 = a2;
        y2 = b2;

        cout << "矩形左下角坐标为(" << x1 << ", " << y1
             << "), 右上角坐标为(" << x2 << ", " << y2 << ")" << endl;
    }

    void Area()
    {
    
    
        double s = ((x1 - x2) * (y1 - y2));

        if (s < 0)
        {
    
    
            s = -s;
        }

        printf("面积为%g\n", s);
    }

private:
    float x1, y1, x2, y2;
};

int main()
{
    
    
    Rectangle r;
    float a, b, c, d;

    cout << "输入矩形的左下角和右上角坐标:" << endl;
    cin >> a >> b >> c >> d;

    r.initial(a, b, c, d);
    r.Area();

    return 0;
}

3. 设计一个用于人事管理的“人员”类。

由于考虑到通用性,这里只抽象出所有类型人员都具有的属性:编号,性别,出生日期,身份证号等。其中“出生日期”声明为一个“日期”类的内嵌子对象。用成员函数实现对人员信息的录入和显示。
要求包括:构造函数和析构函数,复制构造函数,内联成员函数,带默认形参值的成员函数,类的组合。

#include <iostream>
#include <string.h>
using namespace std;

class Date //日期类
{
    
    
private:
    int year;
    int month;
    int day;

public:
    Date() {
    
    } //默认构造

    Date(int y, int m, int d) //带参构造
    {
    
    
        year = y;
        month = m;
        day = d;
    }

    void set(int y, int m, int d) //设置数据函数
    {
    
    
        year = y;
        month = m;
        day = d;
    }

    void show() //显示函数
    {
    
    
        cout << year << "年" << month << "月" << day << "日";
    }
};

class Person //人员类
{
    
    
private:
    int num;
    char sex;
    Date birthday;
    char ID[18];

public:
    Person() {
    
    } //默认构造

    Person(int n, int y, int m, int d, char id[18], char s = 'm') : birthday(y, m, d)
    {
    
    
        num = n;
        sex = s;
        strcpy(ID, id);
    } //有默认值的带参构造

    Person(Person &p) //拷贝构造
    {
    
    
        num = p.num;
        sex = p.sex;
        birthday = p.birthday;

        strcpy(ID, p.ID);
    }

    void input(); //输入函数

    void output(); //输出函数

    ~Person() //析构函数
    {
    
    
        cout << " " << num << "号人员已经录入!" << endl;
    }
};

void Person::input() //输入函数
{
    
    
    int y;
    int m;
    int d;

    cout << "录入数据:" << endl;
    cout << "编号:";
    cin >> num;
    cout << "性别(m/f):";
    cin >> sex;
    cout << "生日:";
    cin >> y >> m >> d;
    birthday.set(y, m, d);
    cout << "身份证号:";
    cin >> ID;
    ID[18] = '\0';
    cout << endl;
}

void Person::output() //输出函数
{
    
    
    cout << "录入结果:" << endl
         << "编号:" << num << endl;

    if (sex == 'm')
    {
    
    
        cout << "性别:男" << endl;
    }
    else
    {
    
    
        cout << "性别:女" << endl;
    }
    cout << "生日:";

    birthday.show();

    cout << endl;
    cout << "身份证号:" << ID << endl;
}

int main()
{
    
    
    Person p;

    p.input();
    p.output();

    return 0;
}

4. 定义并实现一个矩形类,有长,宽两个属性,由成员函数计算矩形的面积。

#include <iostream>
using namespace std;

class Rectangle
{
    
    
public:
    void initial(float a, float b)
    {
    
    
        x = a;
        y = b;

        if (a <= 0 || b <= 0)
        {
    
    
            cout << "矩形的参数不符合要求!" << endl;
        }

        cout << "矩形长为" << x << ", 宽为" << y << endl;
    }

    void Area()
    {
    
    
        double s = (x * y);

        printf("面积为%g\n", s);
    }

private:
    float x, y;
};
int main()
{
    
    
    Rectangle r;
    float a;
    float b;

    cout << "输入矩形的长和宽:" << endl;
    cin >> a >> b;

    r.initial(a, b);
    r.Area();

    return 0;
}

5. 定义一个DataType类,能处理包含字符型,整型,浮点型3种类型的数据,给出其构造函数。

#include <iostream>
using namespace std;

class DataType
{
    
    
public:
    DataType(char c)
    {
    
    
        data.c = c;
        type = Char;
    }
    DataType(int i)
    {
    
    
        data.i = i;
        type = Int;
    }
    DataType(float f)
    {
    
    
        data.f = f;
        type = Float;
    }
    void show();

private:
    enum TYPE
    {
    
    
        Char,
        Int,
        Float
    };
    union DATA
    {
    
    
        int i;
        char c;
        float f;
    };
    TYPE type;
    DATA data;
};

void DataType::show()
{
    
    
    switch (type)
    {
    
    
    case Char:
    {
    
    
        cout << "字符型数据:" << data.c << endl;
        break;
    }
    case Int:
    {
    
    
        cout << "整形数据:" << data.i << endl;
        break;
    }
    case Float:
    {
    
    
        cout << "浮点型数据:" << data.f << endl;
        break;
    }
    default:
        break;
    }
}

int main()
{
    
    
    DataType d1('r');
    d1.show();
    DataType d2(24);
    d2.show();
    DataType d3(26.4f);
    d3.show();
    
    return 0;
}

6. 定义一个Complex类(复数),能处理复数运算,包括加,减,乘,除,和表示复数的功能。

如:
Complex c1(3, 5); //用复数3+5i初始化c1
c1.add(c2); //将c1和c2相加,结果保存在c1中
c1.show();     //将c1输出(这时应该是7.5+5i)

//定义一个Complex类(复数),能处理复数运算,包括加,减,乘,除,和表示复数的功能。
#include <iostream>
using namespace std;

class Complex
{
    
    
private:
    float real;
    float img;
    float real1;
    float img1;

public:
    Complex()
    {
    
    
        real = 0;
        img = 0;
    }

    Complex(float a, float b)
    {
    
    
        real = a;
        img = b;
    }

    Complex Add(Complex &a) //加法
    {
    
    
        real += a.real;
        img += a.img;

        return Complex(real, img);
    }

    Complex Sub(Complex &a) //减法
    {
    
    
        real -= a.real;
        img -= a.img;

        return Complex(real, img);
    }

    Complex Mul(Complex &a) //乘法
    {
    
    
        real1 = real * a.real - img * a.img;
        img1 = img * a.real + real * a.img;
        real = real1;
        img = img1;

        return Complex(real, img);
    }

    Complex Div(Complex &a) //除法
    {
    
    
        real1 = (real * a.real + img * a.img) / (a.real * a.real + a.img * a.img);
        img1 = (img * a.real - real * a.img) / (a.real * a.real + a.img * a.img);
        real = real1;
        img = img1;

        return Complex(real, img);
    }

    void Show()
    {
    
    
        cout << "结果为" << real << "+" << img << "i" << endl;
    }
};

int main()
{
    
    
    Complex a1 = Complex();
    Complex b = Complex(3, 4);
    Complex c = Complex(2, 3);
    Complex a2 = Complex();
    Complex a3 = Complex();
    a1.Show();

    a1.Add(b);
    a1.Show();

    a1.Sub(c);
    a1.Show();

    a2.Add(a1);
    a2.Mul(c);
    a2.Show();

    a3.Add(a1);
    a3.Div(c);
    a3.Show();

    return 0;
}

7. 定义一个Point类,再定义一个Line类,一个Line对象由两个Point对象组成,即线段的两个端点可以决定一条线段(Line类中有两个Point类的对象成员)。提供计算线段长度的功能。

#include <iostream>
#include <cmath>
using namespace std;

static int temp = 0;

class Point
{
    
    
public:
    void initP(float a, float b)
    {
    
    
        x = a;
        y = b;
    }

    void showP()
    {
    
    
        temp++;
        cout << "点" << temp << "的坐标为(" << x << "," << y << ")" << endl;
    }

    friend class Line;

private:
    float x;
    float y;
};

class Line
{
    
    
public:
    void calL(Point &p1, Point &p2)
    {
    
    
        a = p1.x - p2.x;
        b = p1.y - p2.y;
        line = sqrt(a * a + b * b);
    }

    void showL()
    {
    
    
        cout << "线段长为" << line << endl;
    }

private:
    float a;
    float b;
    float line;
    Point m_p1;
    Point m_p2;
};

int main()
{
    
    
    Line l;
    Point p1;
    Point p2;
    float a1;
    float a2;
    float b1;
    float b2;

    cout << "输入点1的坐标" << endl;
    cin >> a1 >> b1;
    p1.initP(a1, b1);
    p1.showP();
    cout << "输入点2的坐标" << endl;
    cin >> a2 >> b2;
    p2.initP(a2, b2);
    p2.showP();

    l.calL(p1, p2);
    l.showL();

    return 0;
}

8. 定义一个Circle类,有数据成员radius(半径),成员函数 getArea(),计算圆的面积,构造一个Circle的对象进行测试。

#include <iostream>
using namespace std;

const float pi = 3.14;

class Circle
{
    
    
public:
    void init(float r);
    void gerArea();

private:
    float radius;
};

void Circle::init(float r)
{
    
    
    radius = r;

    if (radius <= 0)
    {
    
    
        cout << "半径错误!" << endl;
        exit(1);
    }

    cout << "圆的半径为 " << radius << endl;
}

void Circle::gerArea()
{
    
    
    float s = radius * radius * pi;

    printf("面积为%g\n", s);
}

int main()
{
    
    
    Circle c;
    float r;

    cout << "输入圆的半径" << endl;
    cin >> r;

    c.init(r);
    c.gerArea();

    return 0;
}

9. 定义一个Tree类,有成员 age(树龄),成员函数 grow(int year) 对age加上year,getAge()显示tree对象的age值。

#include <iostream>
using namespace std;

class Tree
{
    
    
private:
    int m_age;

public:
    void init(int age);
    void grow(int year);
    void getAge();
};

void Tree::init(int age)
{
    
    
    m_age = age;
}

void Tree::grow(int year)
{
    
    
    m_age += year;
}

void Tree::getAge()
{
    
    
    cout << "树龄" << m_age << "岁。" << endl;
}

int main()
{
    
    
    Tree tree;
    int age;
    int year;

    cout << "输入树的年龄:" << endl;
    cin >> age;

    tree.init(age);

    cout << "输入过了多少年:" << endl;
    cin >> year;

    tree.grow(year);
    tree.getAge();

    return 0;
}

10. 编写一个名为CPU的类,描述一个CPU的以下信息:时钟频率,最大不会超过3000Mhz;字长,可以是32位或64位;核心数,可以是单核,双核或四核;是否支持超线程。各项信息要求使用位域来表示。通过输出sizeof(CPU)来观察该类所占的字节数。

//编写一个名为CPU的类,描述一个CPU的以下信息:时钟频率,最大不会超过3000MHZ;
//字长,可以是32位或是64位;核数,可以是单核、双核或四核;是否支持超线程。
//各项信息要求使用位域来表示。通过输出sizeof(CPU)来观察该类所占的字节数。
#include <iostream>
using namespace std;

class CPU
{
    
    
public:
    CPU(int Hz, int byte, int pit, bool chaoxain); //构造函数
    void show();                                   //展示CPU信息

private:
    int m_Hz;
    int m_byte;
    int m_pit;
    bool m_chaoxian;
};

void CPU::show()
{
    
    
    cout << "时钟频率:" << m_Hz << endl;
    cout << "字长:" << m_byte << endl;
    cout << "核数:" << m_pit << endl;

    if (m_chaoxian == 1)
    {
    
    
        cout << "是否支持超线路:否" << endl;
    }
    else
    {
    
    
        cout << "是否支持超线路:是" << endl;
    }
}

CPU::CPU(int Hz, int byte, int pit, bool chaoxain) //定义构造函数
{
    
    
    m_Hz = Hz;
    m_byte = byte;
    m_pit = pit;
    m_chaoxian = chaoxain;
}

int main()
{
    
    
    CPU c1(2500, 64, 4, true);

    c1.show();

    cout << "类所占的字节数:" << sizeof(CPU) << endl;
}

所有代码都已通过编译测试,有不足的地方欢迎指正。

猜你喜欢

转载自blog.csdn.net/qq_45792897/article/details/119300062