学习记录--C++多态性简答+编程题

#include<iostream>
#include<string>
//双目运算符:运算符作用域两个操作数
//定义一个复数类,重载“+”,作为复数类的成员函数,使这个运算符能直接完成两个复数的加法运算,以及一个复数与一个实数的加法运算
using namespace std;
class Complex
{
public:
    Complex(int r = 0, int i = 0)
    {
        m_r = r;
        m_i = i;
    }
    Complex operator+(Complex  complex);
    void Print();
    ~Complex(){}
private:
    int m_r;
    int m_i;
};

Complex Complex::operator+(Complex complex)
{
    Complex t;
    t.m_r = m_r + complex.m_r;
    t.m_i = m_i + complex.m_i;
    return t;
}

void Complex::Print()
{
    if (m_i < 0)
    {
        cout << m_r << m_i << "i" << endl;
    }
    else
        cout << m_r << "+" << m_i << "i" << endl;
}
int main()
{
    Complex c1(10, 13), c2(9, 2), c3;
    c1.Print();
    c2.Print();
    c3 = c1 + c2;
    c3.Print();
    system("pause");
    return 0;
}

编程题

封装一个CStudent类,用来描述学生的属性和行为。具体要求如下。

1.学生有姓名,籍贯,学号,年龄,成绩五个成员数据,编写构造函数,拷贝构造函数,同时编写Display(),成员函数显示学生的信息。
2.编写“+”运算符重载函数,使CStudent类的两个对象相加返回两个对象总成绩相加的和。
3.编写主函数,定义两个CStudent类对象,分别调用成员函数Display(),显示两个对象的学生信息,同时显示两个对象相加的结果

#include<iostream>
#include<string>
using namespace std;
class Cstudent
{
public:
    Cstudent(string name="", string jiguan="", int id=0, int age=0, int score=0)
    {
        m_name = name;
        m_jiguan = jiguan;
        m_ID = id;
        m_Age = age;
        m_Score = score;
    }
    //拷贝构造函数
    Cstudent(const Cstudent& p)
    {
        m_name = p.m_name;
        m_jiguan = p.m_jiguan;
        m_ID = p.m_ID;
        m_Age = p.m_Age;
        m_Score = p.m_Score;
    }
    int getScore()
    {
        return m_Score;
    }
    void Display()
    {
        cout << "姓名:" << m_name << "\t" << "籍贯:" << m_jiguan << "\t" << "学号:" << m_ID << "\t" << "年龄:" << m_Age << "\t" << "成绩:" << m_Score << endl;
    }
    Cstudent operator+(Cstudent student);
private:
    string m_name;
    string m_jiguan;
    int m_ID;
    int m_Age;
    int m_Score;
};

Cstudent Cstudent::operator+(Cstudent  student)
{
    Cstudent s1;
    s1.m_Score = m_Score + student.m_Score;
    return s1;
}

int main()
{
    Cstudent c1("JIN", "果川",1, 18, 98);
    Cstudent c2("SUGA", "大邱", 2, 17, 100);
    Cstudent c3;
    c1.Display();
    c2.Display();
    c3 = c1 + c2;
    cout << "成绩总和为:" <<c3.getScore()<< endl;
    system("pause");
    return 0;
}

2.设计一个三角形类Triangle,包含三角形三条边长的私有数据成员,另有一个重载运算符”+”,以实现求两个三角形对象的面积之和

 

#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
class Triangle
{
public:
    Triangle(double s1 = 0, double s2 = 0, double s3 = 0, double area=0)
    {
        m_s1 = s1;
        m_s2 = s2;
        m_s3 = s3;
        m_area = area;
    }
    double getS1()
    {
        return m_s1;
    }
    double getS2()
    {
        return m_s2;
    }
    double getS3()
    {
        return m_s3;
    }
    double getArea()
    {
        return m_area;
    }
    Triangle operator+(Triangle triangle);
private:
    double m_s1;
    double m_s2;
    double m_s3;
    double m_area;
};
Triangle Triangle::operator+(Triangle triangle)
{
    Triangle t1;
    double p = (m_s1 + m_s2 + m_s3)/2;
    double p1 = (triangle.m_s1 + triangle.m_s2 + triangle.m_s3)/2;
    double d= p*(p - m_s1) * (p - m_s2) * (p - m_s3);
    double d1 = p1 * (p1 - triangle.m_s1) * (p1 - triangle.m_s2) * (p1 - triangle.m_s3);
    double area = sqrt(d);
    double area1 = sqrt(d1);
    t1.m_area = area + area1;
    return t1;
}
int main()
{
    Triangle c1(3,4,5), c2(6,8,10),c3;
    c3 = c1 + c2;
    cout << "两个三角形面积之和为:" << c3.getArea() << endl;
    system("pause");
    return 0;
}

 

上一篇:POJ 1163 The Triangle


下一篇:Triangle Fun UVA - 11437(一个数学定理 + 三角形求面积)