我目前正在编写一个程序,根据不同的参数搜索歌曲.在我的系统中有两种类型的歌曲:歌词和乐器.因为我需要将它们都放在1个向量中,所以我有一个歌曲类和一个LyricsSong&InstrumentalSong子类.
所以我有一个Song.h文件:
#include <stdio.h>
#include <iostream>
#include <string>
class Song
{
public:
std::string title;
virtual void print();
virtual void printSong(std::string query);
};
Run Code Online (Sandbox Code Playgroud)
还有乐器和歌词子类,它们以这种方式定义:
class LyricsSong : public Song
class InstrumentalSong : public Song
Run Code Online (Sandbox Code Playgroud)
两者都包括Song.h,在这两个类中,类只在头文件中定义.
当我尝试运行另一个使用这两个子类的文件时,包括:
#include "LyricsSong.h"
#include "InstrumentalSong.h"
Run Code Online (Sandbox Code Playgroud)
(显然更多的cpp库),我得到以下编译错误:
In file included from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/InstrumentalSong.h:16:0,
from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/songsParser.cpp:26:
/cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/Song.h:6:7: error: redefinition of 'class Song'
class Song
^
In file included from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/LyricsSong.h:15:0,
from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/songsParser.cpp:25:
/cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/Song.h:6:7: error: previous definition of 'class Song'
class Song
^
Run Code Online (Sandbox Code Playgroud)
什么时候: …
我遇到了一个有趣的错误,我很确定它与条件语句的上下文中的包含多态性有关.
该示例的亮点如下:
ClassParent *parentPointer; //Declare pointer to parent
if(condition){
ClassChild1 = mychild; //Declare child1 object
parentPointer = *mychild;//Parent pointer points to child
}
if(!condition){
ClassChild2 = mychild; //Declare child2
parentPointer = *mychild;//Parent pointer points to child2
}
cout << *parentPointer; //What will this point to???
Run Code Online (Sandbox Code Playgroud)
应该清楚,条件语句在最后一行中生成*parentPointer变量.
我的整个函数看起来像这样:(注意崩溃的地方)
void PosApp::addItem(bool isPerishable) {
Item *refitem;
if (isPerishable) {
Perishable myitem;
std::cout << "Enter the following: " << std::endl
<< "Sku: " << std::endl
<< "Name:" << std::endl
<< "Price: " << std::endl …Run Code Online (Sandbox Code Playgroud) 我有以下架构(类比很糟糕,但W/E).
在program其他逻辑类中,我有很多方法使用finger(MonkeyFinger)的特定类型.这意味着我必须明确地转换所有那些testMethods.
是否有任何设计模式/解决方案可以避免显式演员表?
编辑代码:
Monkey govi = new Monkey(...)
Program test = new Program()
test.testFinger1((MonkeyFinger) govi.GetHand.getFinger)
Run Code Online (Sandbox Code Playgroud)
...
我正在学习Rust,我在实现多态方面遇到了困难.我想用数组来存储Circle或者Test.
trait Poli {
fn area(&self) -> f64;
}
struct Circle {
x: f64,
y: f64,
radius: f64,
}
impl Circle {
fn new (xx: f64, yy: f64, r: f64) -> Circle{
Circle{ x: xx, y: yy, radius: r }
}
}
impl Poli for Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * (self.radius * self.radius)
}
}
struct Test {
x: f64,
y: f64,
radius: f64,
test: f64,
}
impl Test {
fn new …Run Code Online (Sandbox Code Playgroud) 我有一个类的层次结构如下:
class ANIMAL
{
public:
ANIMAL(...)
: ...
{
}
virtual ~ANIMAL()
{}
bool Reproduce(CELL field[40][30], int x, int y);
};
class HERBIVORE : public ANIMAL
{
public:
HERBIVORE(...)
: ANIMAL(...)
{}
};
class RABBIT : public HERBIVORE
{
public:
RABBIT()
: HERBIVORE(10, 45, 3, 25, 10, .50, 40)
{}
};
class CARNIVORE : public ANIMAL
{
public:
CARNIVORE(...)
: ANIMAL(...)
{}
};
class WOLF : public CARNIVORE
{
public:
WOLF()
: CARNIVORE(150, 200, 2, 50, 45, .40, 190, 40, …Run Code Online (Sandbox Code Playgroud) 考虑以下抽象类AbstractEngine:
class AbstractEngine {
static void init();
static std::string getName();
};
Run Code Online (Sandbox Code Playgroud)
并考虑以下2个实现者类:
class Engine1 : public AbstractEngine {
static std::string getName();
};
class Engine2 : public AbstractEngine {
static std::string getName();
};
Run Code Online (Sandbox Code Playgroud)
并且,init()函数应该getName()根据类的类型调用正确的:
void AbstractEngine::init() {
std::cout << getName() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
例如,如果我打电话Engine1::init(),我希望它打电话Engine1::getName()而不是AbstractEngine::getName()
我怎样才能AbstractEngine::init()真正调用正确的实现getName()?
我在使用自动存储持续时间声明的对象使用虚函数时遇到问题.这是一个可重现的场景:
#include <iostream>
class A {
public:
A() {}
virtual ~A() {}
virtual void printClassName() {
std::cout << "A" << std::endl;
}
};
class B : public A {
public:
B() : A() {}
~B() {}
void printClassName() {
std::cout << "B" << std::endl;
}
};
class Test {
private:
A item;
public:
Test() {}
~Test() {}
void setItem(A item) {
this->item = item;
}
A getItem() {
return this->item;
}
};
int main() {
Test t;
B item;
t.setItem(item); …Run Code Online (Sandbox Code Playgroud) 下面的类有重载方法calculate.第一种方法接受int,第二种方法接受short.
public class TestOverLoading
{
public void calculate(int i)
{
System.out.println("int method called!");
}
public void calculate(short i) //or byte
{
System.out.println("short method called!");
}
public static void main(String args[])
{
//Test1
new TestOverLoading().calculate(5); //int method called
//Test2
new TestOverLoading().calculate((short) 5); //short method called
}
}
Run Code Online (Sandbox Code Playgroud)
问题是如何int method called!打印Test1?它是如何确定5是int不short?
Animal为了在Rust中试验多态,我声明了一个自定义特征的数组,但编译器似乎对第一个元素的子类型进行了类型推断:
fn main() {
let animals = [Cat, Dog, Cat, Lion, Dog, Lion];
for single_animal in animals {
single_animal.talk();
}
}
trait Animal {
fn talk(&self);
}
struct Cat;
struct Dog;
struct Lion;
impl Animal for Cat {
fn talk(&self) {
println!("Je miaule !");
}
}
impl Animal for Dog {
fn talk(&self) {
println!("J'aboie !");
}
}
impl Animal for Lion {
fn talk(&self) {
println!("Je rugit !");
}
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨第一个元素是a Cat而不是其他元素:
error: mismatched types [--explain E0308] …Run Code Online (Sandbox Code Playgroud) 我们正在学习UML课程.老师说:
如果每个类都充当其派生类的基类,则应将其声明为abstract.
在下图中,假设我们要获得class german shepherd并class labrador从class chien (Dog woof woof).是否有义务class chien成为一个抽象的阶级?
polymorphism ×10
c++ ×5
casting ×2
inheritance ×2
rust ×2
abstract ×1
arrays ×1
c# ×1
class ×1
conditional ×1
include ×1
inclusion ×1
java ×1
object ×1
oop ×1
overloading ×1
traits ×1
uml ×1