用C++语言直接写一个代码不要复述文字,要求 在主函数中,可以创建一个指向 Book 类的指针数组,然后动态地创建 Book 类的对象和 EBook 类的对象,将这些对象存储到指针数组中。然后,可以通...

下面是使用 C 语言实现的代码,请注意,因为这段代码未经过编译和测试,可能存在语法错误:

``` #include <stdio.h> #include <stdlib.h>

// 定义 Book 类 class Book { public: Book(char* title, char* author, int pages) { this->title = title; this->author = author; this->pages = pages; }

virtual void print() { printf("Book: %s, Author: %s, Pages: %d\n", title, author, pages); }

private: char* title; char* author; int pages; };

// 定义 EBook 类 class EBook { public: EBook(char* title, char* author, int pages, char* format) { this->title = title; this->author = author; this->pages = pages; this->format = format; }

virtual void print() { printf("EBook: %s, Author: %s, Pages: %d, Format: %s\n", title, author, pages, format); }

private: char* title; char* author; int pages; char* format; };

// 定义菜单函数 static void menu() { printf("1. Add book\n"); printf("2. Remove book\n"); printf("3. Print books\n"); printf("4. Quit\n"); }

int main() { // 创建指向 Book 类的指针数组 Book** books = (Book**) malloc(sizeof(Book*) * 100);

while (1) { menu(); // 输出菜单

int choice;
scanf("%d", &amp;choice);  // 读入用户的选择

if (choice == 1) {
  // 添加图书
  int type;
  printf("Enter 1 for book, 2 for eBook: ");
  scanf("%d", &amp;type);

  if (type == 1) {
    // 动态地创建 Book 类的对象
    char title[100], author[100];
    int pages;
    printf("Enter title, author, pages: ");
    scanf("%s%s%d", title, author, &amp;pages);
    Book* book = new Book(title, author, pages);

    // 将对象存储到指针数组中
    static int count = 0;
    books[count++] = book;
  } else
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值