C++程序:
#include "iostream"
#include "string"
using namespace std;
class Book
{
protected:
string name;
double price;
int salec;
public:
Book():name(NULL), price(0), salec(0)
{
cout<<"调用Book类的无参构造方法"<}
Book(string name, double price, int salec):name(name), price(price), salec(salec)
{
cout<<"调用Book类的有参构造方法"<}
~Book()
{
cout<<"调用Book类的析构方法"<}
void setprice(double price)
{
this->price = price;
}
void print()
{
cout<<"书名:"<cout<<"价格:"< cout<<"月销售量:"< }
};
class Cartoon : public Book
{
protected:
static int countA;
public:
Cartoon(string name, double price, int salec):Book(name, price, salec)
{
cout<<"调用Cartoon类的有参构造方法"<countA++;
}
void print()
{
Book::print();
cout<<"总数目:"<}
};
int Cartoon::countA = 0;
int main()
{
Cartoon b1 = Cartoon("Book1", 11, 50);
b1.setprice(20);
b1.print();
cout<Cartoon b2 = Cartoon("Book2", 12, 100);
b2.print();
cout<Cartoon b3 = Cartoon("Book3", 23, 100);
b3.print();
return 0;
}
运行测试: