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;
}
when I should use pure virtual functions.On the one hand, "TOY" for example should be an abstract class since theres no such thing as "TOY" , there are "toy cars", "toy fighters" etc , but on the other hand I need to force it somehow to be abstract since theres no really a function that any toy should have and implement on his own way (except PRINT maybe).
when I should REALLY use pure virtual functions? And if I want to avoid people from creating TOY objects (for example), the only way is PURE virtual functions. right?
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;
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 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.
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.
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
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 ?
I am trying to create a platformer and is stuck on a problem regarding my virtual class Entity. I wish to use it to create stuff like the Player and Enemy class(es). But how to do the parameter for my collision check function. Below is my Entity- and player class.
There might be a better way to check CC with a lot of different objects, this is my first attempt.
This is the error I am getting: "error C2664: 'bool Player::CollisionCheck(Hostile)' : cannot convert argument 1 from 'Player' to 'Hostile'"
and in Hostile I would (I guess) use bool CollisionCheck(Player p);
But if I try for example to use Player in the CC in player.h it will complain that the function doesn't have an overload for that. Hostile is just a example class name right now, it isn't implemented yet. I am trying to use Player, but if possible wish to be able to have a different class depending on what kind of entity it is. The entity will probably also be the players projectiles and so on.
How can I access the virtual base class? This is a practice exercise from c++ primer plus 6.
The problem is that the name becomes No Name instead of the name specified when creating the gunslinger, I don't know how I can call the virtual base class explicitly
Output,
#ifndef PERSON_H_ #define PERSON_H_ #include <string> #include <iostream> #include <cstdlib> using std::string; class person
I have a 'Graph' class, which has derived classes for Adjacency Matrix and Adjacency List representations.
How do I provide iterators for traversing vertices and edges, when the iterator classes would have different implementations for the different derived classes ?
The following way is the only one I can think of, but seems quite cumbersome.
Or is there a pattern for doing this that I'm not aware of ? Would composition be a better idea here compared to polymorphism ? I mean, I can think like..a Graph can 'have' several representation 'objects' within it.
All the involved classes are templates,not sure if that makes the situation different.
Below is simplified code consists of two classes, namely Parent and Child.
Child is inherited from Parent.
All member functions of class Parent are declared virtual, and they have been overridden in the class Child.
Code 1:
#include <cstdlib> #include <iostream> using namespace std; #define QUANTITY 5 class Parent {
[Code] ....
The output of the code:
Child::showID() -- ID is 1804289383 Child::showID() -- ID is 846930886 Child::showID() -- ID is 1681692777 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1957747793
Parent::operator=() invoked.
Child::showID() -- ID is 1804289383 Child::showID() -- ID is 846930886 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1957747793
Question:
Why is Parent::operator= invoked instead of Child::operator= ..?
Isn't it already declared virtual and hence would be overridden..?
I need to invoke Child::operator= instead. How to achieve this?
I need a2 to be a deep copy of a1, but if I understand it correctly, then a2 should just be a pointer copy of a1. How do I make a2 be a different instance of B?
Code: class A { public: virtual void foo(){} virtual void foo2(){} virtual void foo3(){} }; int main() { A a; int ret = sizeof(A); return 0; }
Basically object a contains a virtual table pointer which is of size 4 bytes. Since class A should have a virtual table which contains three pointers pointing to foo, foo2,foo3 separately. So the virtual table should be of size 12 bytes. I wonder where is virtual table located in memory?