内存:128  时间:1

题目描述

MathBook类继承自Book类,Book类中含有私有成员price,MathBook类中增加私有成员difficulty,为MathBook设计display函数输出price和difficulty。

只需提交补充部分。

请以C++方式提交

#include <iostream>
using namespace std;
class Book
{
public:
    Book(int);
    int getprice();
protected:
    int price;
};
class MathBook:public Book
{
public:
    MathBook(int,int);
    int getdifficulty();
    void display();
private:
    int difficulty;
};
Book::Book(int a)
{
    price=a;
}
int Book::getprice()
{
    return price;
}
MathBook::MathBook(int a,int b):Book(a)
{
    difficulty=b;
}
int MathBook::getdifficulty()
{
    return difficulty;
}
void MathBook::display()
{
    /*
   补充部分
    */
}
int main()
{
    int price,difficulty;
    cin>>price>>difficulty;
    MathBook math(price,difficulty);
    math.display();
    return 0;
}

输入

price和difficulty

输出

price和difficulty(中间以空格分开)

样例输入

30 4

样例输出

30 4

提示

代码如下


#include <iostream>
using namespace std;
class Book
{
public:
    Book(int);
    int getprice();
private:
    int price;
};
class Math:public Book
{
public:
    Math(int,int);
    int getdifficulty();
private:
    int difficulty;
};
Book::Book(int a)
{
    price=a;
}
int Book::getprice()
{
    return price;
}
Math::Math(int a,int b):Book(a)
{
    difficulty=b;
}
int Math::getdifficulty()
{
    return difficulty;
}
void display(Math math)
{    cout<<math.getprice()<<" "<<math.getdifficulty()<<endl;

}
int main()
{
    int price,difficulty;
    cin>>price>>difficulty;
    Math math(price,difficulty);
    display(math);
    return 0;
}

代码来源于互联网,仅供参考!