山东理工大学,C++

5-1 继承与派生

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

class Point
{
public:
    Point()
    {
        x = y = 0;
    }
    void getpoint()
    {
        cin>>x>>y;
    }
    float getX(float);
    float getY(float);
    void Move(float, float);
    void show();
private:
    float x, y;
};

void Point::Move(float a, float b)
{
    x = x+a;
    y = y+b;
}

float Point::getX(float a)
{
    return x+a;
}

float Point::getY(float b)
{
    return y+b;
}

void Point::show()
{
    cout<<x<<" "<<y;
}

class Rectangle:public Point
{
public:
    Rectangle()
    {
        w = h = 0;
    }
    void setdata()
    {
        getpoint();
        cin>>w>>h;
        if ( w <= 0 )
            w = 0;
        if ( h <= 0 )
            h = 0;
    }
    void display();
    float getW();
    float getH();
private:
    float w, h;
};

float Rectangle::getW()
{
    return w;
}

float Rectangle::getH()
{
    return h;
}

void Rectangle::display()
{
    show();
    cout<<" "<<w<<" "<<h<<endl;
}

int main()
{
    float m,n;
    Rectangle re;
    re.setdata();
    cin>>m>>n;
    re.Move(m,n);
    re.display();
    return 0;
}

5-2 派生类的构造函数


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

class Person
{
protected:
    char name[105];
    char sex[105];
    int age;
public:
    Person()
    {
        cin>>name>>sex>>age;
    }
};

class Employee:public Person
{
private:
    int basicsalary;
    int leavedays;
public:
    Employee()
    {
        cin>>basicsalary>>leavedays;
    }
    void show()
    {
        cout<<"name:"<<name<<endl;
        cout<<"age:"<<age<<endl;
        cout<<"sex:"<<sex<<endl;
        cout<<"basicSalary:"<<basicsalary<<endl;
        cout<<"leavedays:"<<leavedays<<endl;
    }
};

int main()
{
    Employee e;
    e.show();
    return 0;
}

5-3 多级派生类的构造函数


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

class Person
{
protected:
    char name[105];
    char sex[105];
    int age;
public:
    Person()
    {
        cin>>name>>sex>>age;
    }
};

class Employee:public Person
{
private:
    int basicsalary;
    int leavedays;
public:
    Employee()
    {
        cin>>basicsalary>>leavedays;
    }
    void show()
    {
        cout<<"name:"<<name<<endl;
        cout<<"age:"<<age<<endl;
        cout<<"sex:"<<sex<<endl;
        cout<<"basicSalary:"<<basicsalary<<endl;
        cout<<"leavedays:"<<leavedays<<endl;
    }
};

class Manager:public Employee
{
private:
    float performance;
public:
    Manager()
    {
        cin>>performance;
    }
    void show1()
    {
        show();
        cout<<"performance:"<<performance<<endl;
    }
};

int main()
{
    Manager e;
    e.show1();
    return 0;
}

6-1 多态性与虚函数


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

class Pet
{
public:
    virtual void Speak()
    {
        cout<<"How does a pet speak ?"<<endl;
    }
};

class Dog:public Pet
{
public:
    virtual void Speak()
    {
        cout<<"wang!wang!"<<endl;
    }
};

class Cat:public Pet
{
public:
    virtual void Speak()
    {
        cout<<"miao!miao!"<<endl;
    }
};

int main()
{
    Pet h;
    h.Speak();
    Cat h2;
    h2.Speak();
    Dog h1;
    h1.Speak();
    return 0;
}

6-2 多态性与虚函数


#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

class Pet
{
protected:
    char name[50];
public:
    void Scanf(char *s)
    {
        strcpy( name, s );
    }
    virtual void Speak()
    {
        cout<<"How does a pet speak ?"<<endl;
    }
};

class Dog:public Pet
{
public:
    virtual void Speak()
    {
        cout<<"I am a dog,My name is "<<name<<" My sound is wang!wang!"<<endl;
    }
};

class Cat:public Pet
{
public:
    virtual void Speak()
    {
        cout<<"I am a cat,My name is "<<name<<" My sound is miao!miao!"<<endl;
    }
};

int main()
{
    Pet h;
    h.Speak();
    Cat h2;
    h2.Scanf("Tom");
    h2.Speak();
    Dog h1;
    h1.Scanf("Snoppy");
    h1.Speak();
}

代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!


### 山东理工大学PTA平台结构体使用说明 在山东理工大学PTA平台上,编程练习通常涉及定义特定的数据结构来完成任务。对于面向对象编程中的类和对象操作,可以创建自定义数据类型以满足具体需求。 #### 定义结构体或类 为了实现类似的功能,在C++中可以通过`struct`关键字定义一个新的复合型变量;而在Java或其他支持OOP特性的语言里,则更倾向于利用`class`来进行封装[^1]: ```cpp // C++ struct example #include <iostream> using namespace std; struct Circle { double radius; // Constructor with initialization list Circle(double r):radius(r){} string toString() const{ char buffer[50]; sprintf(buffer, "Circle<%.2f>", this->radius); return string(buffer); } double area() const{ return M_PI * pow(this->radius, 2); } }; ``` ```java // Java class example public class Circle { private final double radius; public Circle(double radius){ this.radius = radius; } @Override public String toString(){ return String.format("Circle<%.2f>", this.radius); } public double getArea(){ return Math.PI * Math.pow(this.radius, 2); } } ``` 这些代码片段展示了如何在一个简单的圆形类/结构体内设置成员属性、构造函数以及计算面积的方法。注意这里实现了`toString()`方法用于返回对象的字符串表示形式,并且遵循了题目要求保留两位小数输出的要求。 当涉及到具体的输入输出时,可以根据给定的例子调整格式化方式,确保符合指定的标准。例如,在打印圆的信息时,应该分别单独处理每一项内容并按照规定顺序依次显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值