BJFUOJ-C++程序设计-实验3-继承和虚函数

完整题目见BJFUOJ-C++程序设计-实验3-继承和虚函数-CSDN博客

A TableTennisPlayer

答案:

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

class TableTennisPlayer{
private:
    string firstname;
    string lastname;
    bool hasTable;

public:
    TableTennisPlayer(const string &, const string &, bool);
    string FirstName() const;
    string LastName() const;
    bool HasTable() const;
};

TableTennisPlayer::TableTennisPlayer(const string & fn, const string & ln, bool h)
{
    firstname=fn;
    lastname=ln;
    hasTable=h;
}

string TableTennisPlayer::FirstName()const{ 
    return firstname;
}

string TableTennisPlayer::LastName()const{ 
    return lastname;
}

bool TableTennisPlayer::HasTable()const{
    return hasTable;
}


class RatedPlayer :public TableTennisPlayer{
private:
    int rating;
public:
    RatedPlayer(int,const string &, const string &, bool);
    int Rating() const;
};

RatedPlayer::RatedPlayer(int r,const string & fn, const string & ln, bool h):TableTennisPlayer(fn,ln,h)
{
    rating=r;
}

int RatedPlayer::Rating() const
{
    return rating;
}

int main(){

    string firstname, lastname;
    bool hasTable;
    int rating;
    char flag;

    while(cin>>flag){
        if(flag=='T'){
            cin>>firstname>>lastname>>hasTable;
            TableTennisPlayer tp(firstname,lastname,hasTable);
            if(tp.HasTable())
                cout<<tp.FirstName()<<" "<<tp.LastName()<<" has a table.\n";
            else
                cout<<tp.FirstName()<<" "<<tp.LastName()<<" hasn't a table.\n";
        } else if(flag=='R'){       
            cin>>firstname>>lastname>>hasTable>>rating;
            RatedPlayer rp(rating,firstname,lastname,hasTable);
            if(rp.HasTable())
                cout<<rp.FirstName()<<" "<<rp.LastName()<<" has a table. The rating is "<<rp.Rating()<<".\n";
            else
            cout<<rp.FirstName()<<" "<<rp.LastName()<<" hasn't a table. The rating is "<<rp.Rating()<<".\n";
        }
    }

return 0;

}

重要知识点:

·继承相关知识;
·派生类对象构造函数先调用父类构造函数,以及如何实现。

B Person和Student

答案:

#include<iostream>
#include<cstring>

using namespace std;

class Person{
private:
    string name;
public:
    virtual void input();
    virtual void display();
};

void Person::input()
{
    cin>>name;
}

void Person::display()
{
    cout<<name<<endl;
}

class Student:public Person{
private:
    string id;
public:
    void input();
    void display();
};

void Student::input()
{
    cin>>id;
    Person::input();
}

void Student::display()
{
    cout<<id<<" ";
    Person::display();
}

int main(){
    Person * p;

    p = new Person;

    p->input();

    p->display();

    delete p;

    p = new Student;

    p->input();

    p->display();

    delete p;

    return 0;
}

重要知识点:

·多态(主函数中体现)
·虚函数的定义与在子类中的实现(virtual关键字)
·赋值兼容规则是指在公有继承情况下,对于某些场合,一个派生类的对象可以
作为基类对象来使用。即:公有派生类的对象可以赋值给一个基类对象,并自
动转换为基类类型。 但基类对象只能“看到”其所替代的派生类对象中的基类部分。

C 图书商品

答案:

#include<iostream>

using namespace std;

class Item_base //未打折的图书商品
{
protected:
    string ISBN; //图书序列号
    double price; //单价

public:
    Item_base(const string & book_ISBN = "", double sales_price = 0.0);
    string get_ISBN() const;
    virtual double net_price(int) const; //返回购买指定数量的图书的总价
    virtual ~Item_base();
};

Item_base::Item_base(const string & book_ISBN, double sales_price)
{
    ISBN=book_ISBN;
    price=sales_price;
}

string Item_base::get_ISBN()const
{
    return ISBN;
}

double Item_base::net_price(int n) const
{
    return price*n;
}

Item_base::~Item_base(){
}

class Bulk_Item : public Item_base //根据购买数量打折
{
public:
    Bulk_Item(const string & book_ISBN = "", double sales_price = 0.0, int min_qty = 0, double discount = 0.0);
    double net_price(int) const; //返回根据购买数量打折后的总价
private:
    int min_qty; // 买够这个数量可以打相应的折扣
    double discount; //折扣
};

Bulk_Item::Bulk_Item(const string & book_ISBN, double sales_price, int min_qty, double discount):Item_base(book_ISBN,sales_price)
{
    this->min_qty=min_qty;
    this->discount=discount;
}

double Bulk_Item::net_price(int n)const
{
    return price*n*(1.0-discount);
}


int main()
{
    Item_base book("0-001-0001-1", 10.0);
    Bulk_Item bulk1("0-001-0001-1",10.0, 5, 0.1);
    Bulk_Item bulk2("0-001-0001-1", 10.0, 10, 0.2);
    int num;
    while (cin >> num)
    {
    cout << bulk1.get_ISBN() << "\t" << num << "\t";
    Item_base * p;
    if (num >= 10) p = &bulk2;
    else if (num >= 5) p = &bulk1;
    else p = &book;
    cout << p->net_price(num) << "\n";
    }
    return 0;
}

重要知识点

·虚函数:
(1)只有类的普通成员函数或析构函数才能声明为虚函数。
(2)基类中的虚函数可以在派生类中被重新定义,但重新定义时必须与基类中的函数原型完全相同(同名覆盖),且无论是否用virtual修饰,系统都将其视为虚函数(建议加上virtual)。
(3)当一个类的公有成员函数声明为虚函数,且在其公有派生类中被同名覆盖时:

 当用基类指针(或引用)指向这个派生类对象,并用该指针调用该函数时,系统自动用派生类中
的同名函数。

 即:当用基类指针(或引用)指向派生类对象,并用该指针调用该函数时,系统会在程序运行中根据所指向对象的不同,自动选择执行当前指向对象所属类的成员函数,从而实现了运行时的多
态性。
(5)静态成员函数不能声明为虚函数。因为静态成员函数不属于某一个对象,没
有多态性的特征。
(6)构造函数不能是虚函数。
(7)析构函数可以是虚函数,且往往被声明为虚函数。

D Vehicle类

答案:

#include<iostream>

using namespace std;

class Vehicle{
protected:
    string name;
    string color;
public:
    Vehicle(string ,string);
    virtual void display()=0;
};

Vehicle::Vehicle(string n,string c)
{
    name=n;
    color=c;
}

class Car:public Vehicle{
private: 
    int passenger;
public:
    Car(string,string,int);
    void display();
};

Car::Car(string n,string c,int num):Vehicle(n,c)
{
    passenger=num;
}

void Car::display()
{
    cout<<"Car name:"<<name<<" Car color:"<<color<<" Car passenger:"<<passenger<<endl;
}

class Truck:public Vehicle{
private: 
    double capacity;
public:
    Truck(string,string,double);
    void display();
};

Truck::Truck(string n,string c,double m):Vehicle(n,c)
{
    capacity=m;
}

void Truck::display()
{
    cout<<"Truck name:"<<name<<" Truck color:"<<color<<" Truck capacity:"<<capacity<<endl;
}

int main()
{
    Vehicle *p;
    char type;
    char name[110],color[110];
    int pas;
    double cap;
    while(cin>>type)
    {
        cin>>name>>color;
        if(type == 'C')
        {
            cin>>pas;
            Car car(name,color,pas);
            p = &car;
            p->display();
        }
        else if(type == 'T')
        {
        cin>>cap;
        Truck truck(name,color,cap);
        p = &truck;
        p->display();
        }
    }
return 0;
}

重要知识点:

·纯虚函数:
在定义一个表达抽象概念的基类时,有时可能会无法给出某些成员
函数的具体实现。这时,就可以将这些函数声明为纯虚函数。

virtual 类型 函数名(参数表)=0 ;

 即纯虚函数就是在基类中声明为虚函数,但未给出具体函数定义体的函数

 在各派生类中对该虚函数进行同名覆盖(即重写)。

·抽象类:
类体内声明了纯虚函数的类,称为抽象类。

 抽象类的主要作用:以此类为基类建立的一个类族具有一组公共的接口(即公
有成员函数),将它们定义为纯虚函数,使它们能够更有效地发挥多态特性。

 抽象类声明了一族派生类的共同接口,而接口的完整实现,即纯虚函数的函数

体,要由派生类自己定义。

·使用纯虚函数与抽象类的注意事项:
(1)抽象类只能用作基类来派生出新类,不能声明抽象类的对象,但可以声明指向抽象类的指针
变量或引用变量。

(2)抽象类中可以有多个纯虚函数。

(3)抽象类中也可以定义其他非纯虚函数。

(4)抽象类派生出新类之后,如果在派生类中没有重新定义基类中的纯虚函数,则必须再将该虚
函数继续声明为纯虚函数,此时,这个派生类仍然是一个抽象类;

(5)在一个复杂的类继承结构中,越上层的类抽象程度就越高,有时甚至无法给出某些成员函数
的具体实现。
(6)引入抽象类的目的主要是为了能将相关类组织在一个类继承结构中,并通过抽象类来为这些
相关类提供统一的操作接口。

E 表面积和体积

答案:

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

const double pi = acos(-1);

class Shape {
public:
Shape() {}
virtual double area() = 0;
virtual void input() = 0;
virtual double volume() = 0;
virtual ~Shape() {}
};

class Cylinder:public Shape
{
private:
    int r;
    int h;
public:
    double area();
    void input();
    double volume();
};

double Cylinder::area()
{
    return pi*r*r*2+2*pi*r*h;
}

void Cylinder::input()
{
    cin>>r>>h;
}

double Cylinder::volume()
{
    return pi*r*r*h;
}

class Cuboid:public Shape
{
private:
    int a;
    int b;
    int c;
public:
    double area();
    void input();
    double volume();
};

double Cuboid::area()
{
    return 2*a*b+2*a*c+2*b*c;
}

void Cuboid::input()
{
    cin>>a>>b>>c;
}

double Cuboid::volume()
{
    return a*b*c;
}

class Ball:public Shape
{
private:
    int r;
public:
    double area();
    void input();
    double volume();
};

double Ball::area()
{
    return 4.0*pi*r*r;
}

void Ball::input()
{
    cin>>r;
}

double Ball::volume()
{
    return (4.0/3.0)*pi*r*r*r;
}

void work(Shape *s) {
    s->input();
    cout << s->area() << " " << s->volume() << endl;
    delete s;
}

int main() {
    char c;
    while (cin >> c) {
        switch (c) {
            case 'y':
                work(new Cylinder());
                break;
            case 'c':
                work(new Cuboid());
                break;
            case 'q':
                work(new Ball());
                break;
            default:
                break;
        }
    }
    return 0;
}

重要知识点:

同上

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇