[英]C++ abstract base class
I am new to C++ and come from Java, I wanted to ask how exactly do I make the base class abstract and to make the classes that inherit it to have the same data types and constructor plus their own ones?我是 C++ 的新手,来自 Java,我想问一下我究竟如何使基础 class 抽象并使继承它的类和构造函数具有相同的数据类型? I looked at various different methods but I cannot quite seem to be able to find it and also see stuff about destructors so that the class cannot be initialized.我查看了各种不同的方法,但似乎无法找到它,也无法看到有关析构函数的内容,因此无法初始化 class。 Can you give me some tips on how to improve it?你能给我一些关于如何改进它的提示吗? How do I make the Book class, so that it inherits the constructor of the Item and its methods?如何制作 Book class,以便它继承 Item 的构造函数及其方法?
Item class:项目 class:
#ifndef _ITEM_H_
#define _ITEM_H_
class Item
{
private:
static int item_id = 0;
string name;
string description;
double price;
int stock;
int sales;
pulic:
virtual ~Item (string name, string description, double price, int stock)
{
this->name = name;
this->description = description;
this->price = price;
this->stock = stock;
this->sales = 0;
this->item_id += 1;
}
virtual ~Item (string name, string description, double price)
{
this->name = name;
this->description = description;
this->price = price;
this->stock = 0;
this->sales = 0;
this->item_id += 1;
}
double getPrice()
{
return this->price;
}
string getName()
{
return this->name;
}
string getDescription()
{
return this->description;
}
int getSales()
{
return this->sales;
}
int getStock()
{
return this->stock;
}
int getItemID()
{
return this->item_id;
}
};
#endif
Book class that inherits Item:继承项目的书 class:
#include "item.h"
class Book : public Item
{
private:
string author;
string genre;
public:
Book(string author, string genre)
{
this->author = author;
this->genre = genre;
}
}
I am new to C++ and come from Java, I wanted to ask how exactly do I make the base class abstract and to make the classes that inherit it to have the same data types and constructor plus their own ones?我是 C++ 的新手,来自 Java,我想问一下我究竟如何使基础 class 抽象并使继承它的类和构造函数具有相同的数据类型? I looked at various different methods but I cannot quite seem to be able to find it and also see stuff about destructors so that the class cannot be initialized.我查看了各种不同的方法,但似乎无法找到它,也无法看到有关析构函数的内容,因此无法初始化 class。 Can you give me some tips on how to improve it?你能给我一些关于如何改进它的提示吗? How do I make the Book class, so that it inherits the constructor of the Item and its methods?如何制作 Book class,以便它继承 Item 的构造函数及其方法?
Item class:项目 class:
#ifndef _ITEM_H_
#define _ITEM_H_
class Item
{
private:
static int item_id = 0;
string name;
string description;
double price;
int stock;
int sales;
pulic:
virtual ~Item (string name, string description, double price, int stock)
{
this->name = name;
this->description = description;
this->price = price;
this->stock = stock;
this->sales = 0;
this->item_id += 1;
}
virtual ~Item (string name, string description, double price)
{
this->name = name;
this->description = description;
this->price = price;
this->stock = 0;
this->sales = 0;
this->item_id += 1;
}
double getPrice()
{
return this->price;
}
string getName()
{
return this->name;
}
string getDescription()
{
return this->description;
}
int getSales()
{
return this->sales;
}
int getStock()
{
return this->stock;
}
int getItemID()
{
return this->item_id;
}
};
#endif
Book class that inherits Item:继承项目的书 class:
#include "item.h"
class Book : public Item
{
private:
string author;
string genre;
public:
Book(string author, string genre)
{
this->author = author;
this->genre = genre;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.