C++ :: Abstract Base Class With Templates?

Oct 3, 2014

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?

template<typename T>
class Matrix { /* ... */ };
class iFile {
public:
virtual ~iFile() {} = 0;
virtual void Process(const Matrix<T>&) = 0;

[Code] .....

View 5 Replies


ADVERTISEMENT

C++ :: Common Base Class And Abstract Interface Class?

May 28, 2013

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.

View 11 Replies View Related

C++ :: Inheritance From Abstract Base Class For Decision Tree

Feb 27, 2014

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;

[Code] ....

View 4 Replies View Related

C++ :: Can't Instantiate Abstract Class

Dec 28, 2012

two more questions

Code:
#ifndef PERFECTSIM_PARSER
#define PERFECTSIM_PARSER
#include <string>
#include <d3dx9.h>
#include <sstream>
#include <iostream>
#include "tinyxml inyxml.h"
using namespace std;
template<class T>
class GetValue {
protected:
virtual T get(TiXmlNode* pParent);

[code].....

1) Can't instantiate abstract class of GetVector3.

2) Don't you think the coding is very redundant ?

View 3 Replies View Related

C++ :: Vector Of Pointers - Abstract Class

May 13, 2014

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;

[Code] .....

View 4 Replies View Related

C++ :: Abstract Class And Virtual Function?

Feb 17, 2013

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;
}

What Could be causing this error?

View 8 Replies View Related

C++ :: Abstract Static Class And Performance

Nov 11, 2014

I have the following code:

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.

View 1 Replies View Related

C++ :: Create Objects From Abstract Class

Oct 13, 2013

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.

View 6 Replies View Related

C++ :: Struct Constructor And Abstract Class

Feb 14, 2013

I am making a snake game just to give some context.

//LevelObject.hpp
class LevelObject {
public:
virtual void Update() = 0;
virtual void Draw(Canvas& canvas) = 0;
protected:
Vector3 location_;
[Code] ....

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.

View 2 Replies View Related

C++ :: Defining Operator In Abstract Class

Jun 22, 2012

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

PHP Code:

class Mbase{
public:
void put()=0;
double get()=0;
Mbase operator +(Mbase A);


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.

View 10 Replies View Related

C++ :: How To Hold Pointers / References To Abstract Class

Nov 15, 2014

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?

View 2 Replies View Related

C Sharp :: Abstract Class With Parametrized Constructor?

Jul 27, 2012

As i have one Abstract base class say MyBase.

It have parameterized constructor with string value it have abstract method call.And I also have One child class say MyChild.

It does not have any constructor only one have public method and i want to call that parameterized constructor of base class onto child class.

View 1 Replies View Related

Visual C++ :: Polymorphic Methods In Abstract Class

Oct 2, 2013

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.

View 6 Replies View Related

C++ :: Initializing Static Map Of Variable Type Abstract Class?

Dec 3, 2014

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.

View 3 Replies View Related

C++ :: How To Define Static Member Data In Abstract Class

Jul 11, 2012

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?

View 4 Replies View Related

C# :: Abstract Class Provide Functionality Without Affecting Child Classes?

Mar 6, 2014

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 ?

View 2 Replies View Related

C++ :: Setting Up Linked List Class With Templates?

Dec 2, 2013

I am trying to implement a linked list using a class template, however each of my classes that I want in the list all inherit from an Account class. I have attempted to template the linked list class as account however Ive come into errors I can't resolve. How I can go about this?

The error exists within the customer.cpp class where it says

14IntelliSense: a value of type "Account *" cannot be assigned to an entity of type "CurrentAccount *"f:Further C++Assignment with TemplatesAssignmentCustomer.cpp229Assignment

Customer class:

#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <iostream>
#include <string>
#include "Account.h"
#include "AccountLinkedList.h"

[Code] .....

View 2 Replies View Related

C++ :: Calling Derived Class Functions In A Function With Parameter Of Base Class

Mar 30, 2013

Say I have 3 classes:

class Player {
public:
virtual func1();

[code]....

Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:

//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);

When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).

View 6 Replies View Related

C++ :: Size Of Derived Class With Overriding Virtual Functions From Base Class?

Jan 21, 2014

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.

#include<iostream>
class A{
public:

[Code].....

View 1 Replies View Related

C++ :: Transfer Values Set In Privates Of Base Class By Object Of One Derived Class To Another

Apr 26, 2014

I have my main.cpp like this:

#include <iostream>
#include "curve1.h"
#include "curve2.h"
using namespace std;
int main() {
Curve1 curve1Obj;
Curve2 curve2Obj;

[Code]...

Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.

My base class header score.h looks like this:

#ifndef SCORE_H
#define SCORE_H
class Score {
private:
int *ipScore;
float fAverage;
int iSize;

[Code]...

You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously).
.....

The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....

How can I cause it to return the old values that are set in curve1.cpp?

Here is my curve2.cpp

#include <iostream>
#include "curve2.h"
using namespace std;
void Curve2::curve() {
cout << "getSize() returns: " << getSize() << endl; // out comes the garbage
}

Can I use a function to simply put values from old to new variables? If yes then how?

View 3 Replies View Related

C++ :: Deleting Array Of Derived Class Objects Through Base Class Pointer

Mar 21, 2015

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?

View 1 Replies View Related

C++ :: Creating Array Of Pointers To Base Class To Point To Derived Class Objects Dynamically

Jan 16, 2013

Please consider the following code :

#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.

View 2 Replies View Related

C++ :: Initializing Inner-objects Of Base Class From Driven-class Constructor

Jan 6, 2015

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 .

View 5 Replies View Related

C++ :: Derived Class Not Overwriting Base Class Function - Using Vectors

Feb 4, 2014

So I have a base class, lets call it base. In base I have a virtual function called update(), update just couts "base" then I have a class derived from base called derived;

it has a function called update(), update just couts "derived" then I create a vector called Vec it's initialised like this:

std::vector<base> Vec;

then I add an element into it like this

Derived DerElement;
Vec.push_back(DerElement);

then when I type:

for (int i=0; i<Vec.size(); i++) {
Vec.at(i).Update();
}

It outputs:

Derived DerElement2;
DerElement2.Update();

and it outputs this:

#include <iostream>
#include <vector>
class Base {
public:
virtual void Update() {

[Code] .....

and this is it's output:

Base
Derived
Press any key to continue . . .

View 1 Replies View Related

C++ :: Friendship From Derived Class Method To Base Class Members

Jul 15, 2014

I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:

class Derived;
class Base {
int i, j;
friend void Derived::f();
protected:
Base();

[Code] ......

View 3 Replies View Related

C++ :: Variable Belonging To Base Class - Tell Compiler Consider This To Be Derived Class?

Oct 12, 2013

I have an example where I have a variable belonging to a base class, but I would like to tell the compiler that it actually belongs to a derived class. How can I do this?

// base class: R0
// derived class: R1
// see function SetR1 for the problem
class R0 {
public:
int a;

[Code] .....

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved