#include <iostream>
using namespace std;
class base
{
public:
virtual void taunt() = 0;
virtual void hide() = 0;
};
template <int nx, int ny>
class derivedA : public base
{
void hide();
public:
void taunt() { cout << "derivedA" << endl; }
char c[nx][ny];
};
template <int nx, int ny, int nz>
class derivedB : public base
{
void taunt();
public:
void hide() { cout << "hide B" << end; }
char c[nx][ny][nz];
};
int main()
{
derived * …Run Code Online (Sandbox Code Playgroud) 我有两个类,如下所示(我试图尽可能抽象示例):
#include <iostream>
using namespace std;
class foo1
{
public:
foo1() {};
virtual ~foo1() {};
void Method1() { Method2(); }
virtual void Method2() { cout<<"parent";}
};
class foo2 : public foo1
{
public:
virtual void Method2() { cout<<"child";}
};
int main()
{
foo2 a = foo2();
a.Method1();
}
Run Code Online (Sandbox Code Playgroud)
我收到了"父母"的消息.所以Method1()的foo2执行foo1::Method2().
我需要使用什么来foo2::Method1调用它们foo2::Method2?
我试图operator<<在几个子类中重载.我有一个名为Question的超类,它有一个枚举值类型和一个字符串问题.该类的子类是TextQuestion,ChoiceQuestion,BoolQuestion和ScaleQuestion.TextQuestion没有其他数据字段.ChoiceQuestion有一个字符串向量,用于存储多项选择的可能性.BoolQuestion没有其他数据字段.ScaleQuestion有两个int值,low_和high_,用于比例.
class Question {
public:
enum Type{TEXT, CHOICE, BOOL, SCALE};
Question():
type_(), question_() {}
Question(Type type, std::string question):
type_(type), question_(question) {}
friend std::ostream& operator<<(std::ostream& out, const Question& q);
virtual void print(std::ostream& out) const;
virtual ~Question();
private:
Type type_;
std::string question_;
};
class TextQuestion: public Question {
public:
TextQuestion():
Question() {}
TextQuestion(Type type, std::string question):
Question(type, question) {}
void print(std::ostream& out) …Run Code Online (Sandbox Code Playgroud) 每个C++程序员都知道,虚拟析构函数用于确保继承层次结构中对象的正确销毁顺序.
在哪里使用"虚拟析构器"/可以在实时场景中使用?
首先要说我是西班牙语,我的英语非常糟糕,我使用了一名翻译,因为我看起来像个文盲而道歉:)
我已经多年没有玩c ++而且我已经忘记了很多,以至于我可能怀疑或者可能的失败是语言中的基本内容.
我编写了一个小代码来测试c ++中带有虚拟析构函数的delete运算符的开销,我发现只有在第一次调用delete时它才能正常工作.
代码就在这里(这是一个允许你在线编写代码并执行代码的页面,我在visual studio和gcc中进行了测试,它完全相同):https://onlinegdb.com/SkiI1dNDQ
有人能告诉我为什么会这样吗?只有第一次运作良好,第二次运行不顺利
我也把代码放在这里,结果给出了:
#include <stdio.h>
class Base
{
public:
virtual ~Base() { printf("~Base\r\n"); }
void operator delete(void *m) { printf("delete Base\r\n"); }
};
class Derived : public Base
{
public:
Derived() { x = 1; }
~Derived() { printf("~Derived\r\n"); }
void operator delete(void *m) { printf("delete Derived\r\n"); }
int x;
};
int main()
{
Derived *derived = new Derived();
printf("1) delete derived (%i)\r\n", ((Derived*)derived)->x);
delete derived;
printf("2) delete derived (%i)\r\n", ((Derived*)derived)->x); …Run Code Online (Sandbox Code Playgroud) 当前正在研究由可调整大小的数组控制的堆栈的实现。尝试实例化的新对象会ResizableArrayStack产生错误。
不允许抽象类类型为“ csc232 :: ResizableArrayStack”的E0322对象:ResizableArrayStack.cpp 107
函数“ csc232 :: ResizableArrayStack :: isEmpty [with ItemType = int]”是一个纯虚函数
函数“ csc232 :: ResizableArrayStack :: push [with ItemType = int]”是一个纯虚函数
函数“ csc232 :: ResizableArrayStack :: pop [with ItemType = int]”是一个纯虚函数
函数“ csc232 :: ResizableArrayStack :: peek [with ItemType = int]”是一个纯虚函数
StackInterface.h
#include "pch.h"
#pragma once
#ifndef CSC232_HW05_RESIZABLE_ARRAY_STACK_STACK_INTERFACE_H
#define CSC232_HW05_RESIZABLE_ARRAY_STACK_STACK_INTERFACE_H
namespace csc232 {
template<typename ItemType>
class StackInterface {
public:
/**
* Sees whether the stack is empty.
* @return True if the stack …Run Code Online (Sandbox Code Playgroud) 我知道:
Shape *p;
p = new Rectangle(10,5);
Run Code Online (Sandbox Code Playgroud)
会工作,但我不明白为什么我不能像下面这样创建:
Shape *p;
Rectangle rec;
p = &rec;
p->shapeName();
Run Code Online (Sandbox Code Playgroud)
我得到的错误是这样的:
没有用于调用 'Rectangle::Rectangle()' 的匹配函数
这是我的最小示例:
#include <iostream>
using namespace std;
class Shape {
protected:
double area, perimeter;
public:
virtual void shapeName() = 0;
virtual void shapeArea() = 0;
virtual void shapePerimeter() = 0;
double getArea()
{
return area;
}
double getPerimeter()
{
return perimeter;
}
};
class Rectangle : public Shape {
double height, width;
public:
Rectangle(double height, double width)
: height(height)
, …Run Code Online (Sandbox Code Playgroud) 我已经在头文件(Environment.h)中声明了以下类,并且我想使超类 FieldAccessor 抽象:
#include <jni.h>
class FieldAccessor {
public:
FieldAccessor(
JNIEnv* env
) {
this->jNIEnv = env;
}
virtual jobject getValue(jobject, jobject) = 0;
protected:
JNIEnv* jNIEnv;
};
template<typename Type>
class PrimitiveFieldAccessor : public FieldAccessor {
public :
PrimitiveFieldAccessor (
JNIEnv* env, const char name[], const char ctorSig[],
Type (JNIEnv::*getFieldValueFunction) (jobject, jfieldID)
);
jobject getValue(jobject, jobject);
private:
jclass type;
jmethodID constructorId;
Type (JNIEnv::*getFieldValueFunction) (jobject, jfieldID);
};
Run Code Online (Sandbox Code Playgroud)
但我得到以下编译错误:
#include <jni.h>
class FieldAccessor {
public:
FieldAccessor(
JNIEnv* env
) {
this->jNIEnv = …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我无法理解调用派生类的虚方法的方式.此外,任何人都可以建议一个源,其中虚拟功能的概念用非常基本的方法图解说明.
class Base1
{
virtual void fun1() { cout << "Base1::fun1()" << endl; }
virtual void func1() { cout << "Base1::func1()" << endl; }
};
class Base2
{
virtual void fun1() { cout << "Base2::fun1()" << endl; }
virtual void func1() { cout << "Base2::func1()" << endl; }
};
class Base3
{
virtual void fun1() { cout << "Base3::fun1()" << endl; }
virtual void func1() { cout << "Base3::func1()" << endl; }
};
class Derive : public Base1, public Base2, …Run Code Online (Sandbox Code Playgroud) 我将如何在C++中执行以下操作(以下代码是C#):
class Base
{
public virtual void Foo()
{
// do stuff...
}
}
class C : Base
{
public override void Foo()
{
base.Foo(); // <=== how do you do this line?
}
}
Run Code Online (Sandbox Code Playgroud) 我刚读过(第k次)
这是关于模拟虚拟静态成员的问题.我的问题是 - 是什么让C++标准委托(或之前的Bjarne Stroustrup)没有将此功能添加到C?他们知道会破坏什么吗?或妨碍任何事情的表现(即使不使用)?
为了更好地说明我对功能定义本身的看法,这里有一些代码:
// This does not compile!
class Base {
// A pure virtual member - perhaps need to indicate that somehow
virtual static const string ToWhom;
void sayHello() {
cout << "Hello, " << ToWhom << "!" << endl;
}
};
class World : public Base {
static virtual const string ToWhom = "the entire world"s; // C++14 literal
};
class Everybody : public Base {
static virtual const string ToWhom = "everybody around"s; …Run Code Online (Sandbox Code Playgroud) 我在google和这里搜索过但我无法理解为什么类中的纯函数必须是虚函数.我理解也许将"正常功能"称为纯粹是不是很有用,但我认为这不是废话.我的意思是,"纯粹"这个词只是为了宣布一个抽象类.好吧,我不能将多态性与纯正常函数一起使用,但无论如何都要达到主要原因(将类声明为抽象).我错了吗?
我只是想知道所有虚函数是否必须是const?
我遇到了一些问题,因为当我打算将它们打印出来时,该区域总是为我的方块返回0.如果有人能够启发我,我将不胜感激.
shapetwod.h
class ShapeTwoD
{
protected:
string name, warpSpace;
bool containsWarpSpace;
public:
//constructor
ShapeTwoD();
ShapeTwoD(string, bool);
//accessors/set function
void setName(string);
//mutator/get function
string getName();
//methods
virtual double computeArea();
virtual void view();
};
Run Code Online (Sandbox Code Playgroud)
shapetwod.cpp
ShapeTwoD::ShapeTwoD()
{
string name = "";
}
ShapeTwoD::ShapeTwoD(string ShapeName)
{
name = ShapeName;
}
void ShapeTwoD::setName(string shapeName)
{
name=shapeName;
}
string ShapeTwoD::getName()
{
return name;
}
double ShapeTwoD::computeArea()
{
return 0;
}
void ShapeTwoD::view()
{
cout << "Area is: " << endl;
}
Run Code Online (Sandbox Code Playgroud)
square.h
class Square:public …Run Code Online (Sandbox Code Playgroud)