山东理工大学,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();
}

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值