MathBook类--继承

MathBook类--继承

时间: 1ms        内存:128M

描述:

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

提示:

参考答案(内存最优[1092]):

#include<stdio.h>
#include<string.h>
int main()
{
	int i,j;
	scanf("%d %d",&i,&j);
	printf("%d %d",i,j);
}

参考答案(时间最优[0]):


#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;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注