I am working on a project that requires me to create objects from a abstract class that has 2 child classes (that need to be derived). Any examples on how to do this? I looked online and the examples were pretty vague. the main error that I am getting is when I make a temp object with & in front of it (such as Employee &genericEmp) it throws a must be initialized error.
So i'm actually using polyphormism for calculating the area of the cross and other shapes. so how do I actually make use of dynamically create an object this way?
do I create them in my Shape2DLink or in my individual child classes?
I have an abstract base class - let's call it MyInterface - and a class that most classes in my program inherit from, let's call it MyBaseclass.
Let's assume that all my objects inherit MyBaseclass, some of which also inherit MyInterface. Now I want to collect objects in a container class, MyContainerclass. The container class is only interested in objects that implement MyInterface.
Now I know that all objects that inherit MyInterface also inherit MyBaseclass, but the compiler doesn't know that. MyContainerclass wants to call methods in MyBaseclass, but it collects pointers to MyInterface classes. I can't make MyInterface inherit MyBaseclass, because I will be using classes that I don't want to change (they are part of a framework) that already inherit MyContainerclass. IOW, I can't use virtual inheritance to get a nice inheritance diamond.
To sum up, I want to create a container class that:
1. Collects objects that implement MyInterface.
2. Calls MyBaseclass methods on the collected objects.
I need to create a vector of pointers and hold the book objects in it. then i have a virtual function in my books which is a pure virtual in LibraryItems. When i try to add the books object in my code, i understand that since the scope runs out there is no object that is added. so when i run print it gives me an error.
#include<iostream> #include "books.h" #include "library.h" #include <vector> using namespace std;
int main(int argc, char * argv[]) { vector<LibraryItems* >libraryInfo;
I have this header file called Shape.h containing these function declarations. and a Shape.cpp which contains the body of the function. I am not showing it since it is not needed.
//This is from Shapes.h header file #ifndef SHAPES_H #define SHAPES_H #include <iostream>
[Code]....
I have this unfinished Main.cpp because the third line "JuanSanchez::Circle *pCar = new Circle; " is giving me a compiler error "error C2061: syntax error : identifier 'Circle' "
#include "Shapes.h" int main() { const int arrayIndex = 4; JuanSanchez::Shape *myShape[arrayIndex]; JuanSanchez::Circle *pCar = new Circle; }
class Element { public: .. virtual unsigned NumberOfNodes() = 0;
[Code] ....
Is it possible to implement this better? All the element stuff can be static, but this is not possible with the abstract class. I want to have Mesh independent of a specific element. With the code above, if I have multiple meshes I have one instance of an element, e.g., Triangle for each mesh. Although they are all exactly the same.
I want to create an abstract base class having a member function that can accept a templatized structure as its parameter, something that according to C++'s rules can't be done for a good reason.
That good reason it is because an abstract base class is intended to provide interface rules to the classes that will derive from it and should not deal with data.
But how would you go about doing something like the following which is probably a reasonable design decision?
The problem I have is with the Size constructor and the abstract class LevelObject which size is a member of.
The compiler error I get is:
C:Program Files (x86)ProgrammingProjectsUniversityprg_interactivesnakey_takeysrc..inc..incPlayer.hpp|17|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]| C:Program Files (x86)ProgrammingProjectsUniversityprg_interactivesnakey_takeysrc..inc..inc..incPlayer.hpp|17|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
[Code] .....
However I do invoke the copy constructor when I pass a variable of type size to the constructor in this line:
size_ = Size(s);
But the problem is that its complaining that the abstract class LevelObject doesn't invoke the constructor, which it shouldn't.
I have an abstract class called Mbase and from it derived two classes: Sparse and Dense. Now I have an array in which its elements can be either Sparse or Dense. So, I delcared the array to have pointers to Mbase class. For example:
PHP Code: Mbase** A; Sparse* A1 = new Sparse; Dense* A2 = new Dense; A[1] = dynamic_cast<Mbase*>(A1); A[2] = dynamic_cast<Mbase*>(A2);
Now, I have operator + defined in Sparse and Dense. but when I do
PHP Code:
A[1]+A[2]
I get that operator + is not defined for Mbase class. So, I tried to define it in the Mbase class
However, the last code does not compile complaining that it cannot declare a class of type abstract in Mbase operator +(Mbase A). I think this is because I am returning Mbase instance.
I have an abstract class named Terrain, and a class named RoadMap, which supposed to hold an N*N array of Terrains. But I'm not sure what type should the RoadMap class hold:
Code: #ifndef TERRAIN_H #define TERRAIN_H class Terrain {
[Code] ....
I can't use an array of refernces here, so I tried this:
Code: Terrain** terrain; and then I thought this was the way to go:
Code: Terrain (*terrain)[]; But now I'm not sure.
The N*N matrix size supposed to be determined according to a given input... What type should I use there?
Imagine if there is an abstract class with a method (say output or print) which would be inherited by a few other classes. Later objects are created using the inherited classes, and the user wishes to call the above method twice, for eg (i) output/print to screen and (ii) output/print to a file. What is the best way to achieve that.
I'm trying to implement a decision tree that gets doubles as input (in this sample code just random numbers) and returns a boolean value. At the nodes, I'd like to use various operators that can have different input and return types. I've implemented an abstract base class for the nodes and I'm deriving the specific nodes from it. In the code below, I've included only a few derived classes. Building the tree (randomly) is no problem. However, I'm looking for a clever way to evaluate the tree. I think that uncommenting the lines in bold print would in principle do it. However, this is not possible because "value" is not a member of the base class. The type of "value" is different in the derived classes, so I cannot simply declare it in the base class.
"Node.h" #pragma once class NodeBase{ public: NodeBase* Child_1; NodeBase* Child_2; virtual void evaluate() = 0;
A have two classes, one inheriting the other, and the parent class being abstract (I plan on adding more child classes in the future). For reasons I won't bother mentioning, I'm making use of an STL container as a way for me to access all of the child objects in the heap. I've done so by making use of a map, with key type int and value type being a pointer to the parent class:
//PARENT.H class Parent { protected: static int n; static std::map<int, Parent*> map; public: virtual void pureVirtual() = 0;
[code]....
The Problem:In line 5 of Parent.cpp, initializing the value of the element to new Child won't work, as according to the compiler, the Child class hasn't been declared yet, and including Child.h into the Parent.h only opens an even bigger can of worms.I also can't initialize it as new Parent, seeing as the parent class is an abstract one.
The Question:Is there a way I can initialize the static map properly. Making the Parent class abstract is not an option.
For example, in a header file A.h, I define an abstract class,
Code:
// A.h class A { public: virtual void foo() = 0; private: static int _x; };
How'd I initialize static member data _x?Normally, we initialize a static member data in a cpp file. However, there is not cpp file for A.h. If I intialize _x in header file, there will be linker errors like mulitple defined symbols. What is appropriate way to do that?
The abstract class can provide more functionality without affecting child classes.If we add any method to the interface ,then will it affect all the child classes ?
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code: class B { public: B():_y(1){} virtual ~B() { cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
#include <iostream> using namespace std; class superclass; class subclass1; class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
Let's say I have a Car object , and it contains inner Engine object.
Code: struct Car{ Engine mEngine; };
In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:
Code: Car::Car: mEngin(arg1,arg2,...) { other stuff here }
Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?
Code: class Toyota: Car(...), mEngine(...), mGear(..) { ... };
The other options are: 1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive 2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
I actually need to create a labyrinth and a player who needs to go thru the map collecting objects (like bags, water etc...). I planned to do the map with a bidimentional array (5x5) but I dont know how to put different objects on an index.. Ex: array[3][2] will have a Botte of water and an energy bar..
Someone told me to use structs but I dont really understand how this would work... I've tried so many times bit I cant do it...
What i'm trying to create is a simple State Manager for SFML! I created another class that inherits State.
#pragma once #include "state.h" class FirstState : public State { public: FirstState(); ~FirstState(); void handle_action(); void update(); void render(); };
So the question is this, each state that i have will inherit the State class. However, i wanted to perhaps add each state object into a vector array. But i'm not sure as to what data type it be? I have a state manager class that will contain the vector.
What i want to do is this, each game state will create an object that will inherit functions from the state.h class. I want to store them all in a vector array, but each object is clearly named different. My curiosity was wondering, since all those different states inherit the State.h class, can i simply create a State Object std::vector<State> *states; that will contain all those different state objects?
Im supposed to create an array of eight Circle objects initialized with the radii which is in the program. Also I must use bubble sort to arrange the objects is ascending order.
ERRORS: 'initializing' : cannot convert from 'double' to 'Circle' 'setRadius' : is not a member of 'Circle' see declaration of 'Circle' 'findArea' : is not a member of 'Circle' see declaration of 'Circle
#include "stdafx.h" #include <iostream> #include <iomanip> using namespace std; class Circle {
I have been working on an assignment where I have to add three objects of a class Matrix. The class should have the flexibility to add more than two oprands without changing any operand on Left hand side of the '=' operator.