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


ADVERTISEMENT

C++ :: Reflect Child Classes From Base Class

Nov 26, 2013

We want a solution in C++ that must be able to do the following:

Given a string of particular type, lets say 'A', we want to find all the types that derives from 'A'.

Instantiate new objects out of the types that are derived from 'A'.

E.g. Lets say we have a class, VehicleEntity. VehicleEntityhas child classes, PassangerCarEntity, TruckEntity, TrainEntity, BoatEntity.

We are unsure what vehicle entities there may be as the a library could be added containing more VehicleEntities. E.g. an AirplaneEntity thaterives from VehicleEntity could be added after deployment.

In the application, when a user wants to select a VehicleEntity, the user should be able to pick any of the entities deriving from VehicleEntity. This includes the PassangerCarEntity, TruckEntity, TrainEntity, BoatEntity and AirplaneEntity. The user selects an Entity, lets say AirplaneEntity, A new object of type AirplaneEntity must be instantiated.

The following is an concept example in C# of what we want to achieve in C++.

In C# the items for the dropdown list can be retrieved as follows:

Type vehicleEntityType = typeof(VehicleEntity);
List<Type> types = new List<Type>();
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())

[Code] .....

We are aware that standard C++ does not contain any Metadata on its objects, and thus it is not possible without a workaround. It does not seem possible with RTTI and boost.Mirror.

View 7 Replies View Related

C# :: Webrequest And Webresponse Abstract Classes

Mar 5, 2015

.Theoretically speaking, we cannot create object of abstract class and even return an instance of it. But according to msdn documentation, WebRequest request = WebRequest.Create("Uri here");. In other word, with this statement, the create method return an instance of the WebRequest class, which is the abstract class.

As far as abstract class is concerned, there is no way for us to return an instance of the derived class within the abstract class or superclass, within the method of abstract class in particular. Since the abstract class knows nothing about the derived class. Unless the method is implemented within the derived class. Here is the statement HttpWebRequest request = (HttpWebRequest) WebRequest.Create("Uri here");

View 14 Replies View Related

C++ :: Pure Virtual Functions - Abstract Classes

Jan 26, 2013

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?

View 4 Replies View Related

C++ :: Including Header And Inheritance - Parent And Child Classes

Jan 27, 2012

I have defined to classes : Parent and Child. I have some global variables in a header file named as "var.h". These variables are used in both Parent and child Classes. The source code of these classes are written below:

Parent.h
==============================================
#ifndef PARENT_H
#define PARENT_H
#pragma once
#include <stdio.h>
class Parent {

[Code] ....

After compiling, the compiler returns a fatal error as follows:

1>Parent.obj : error LNK2005: "int counter" (?counter@@3HA) already defined in Child.obj
1>C:Documents and SettingspishiDesktop estDebug est.exe : fatal error LNK1169: one or more multiply defined symbols found

It says the "counter" is defined multiple times....

View 4 Replies View Related

C++ :: Graph Class - How To Provide Virtual Iterators

May 29, 2013

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.

Code:
class Base {
public:
class BaseIterator {

};
virtual const BaseIterator& begin();
virtual const BaseIterator& end();

[Code] .....

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.

View 7 Replies View Related

C++ :: Derive A Class From Xml Node To Add More Functionality

Dec 10, 2012

I would like to derive a class from pugi::xml_node to add more functionality to the class. I get stuck when I try to append a child because pug::xml_node append_child return pugi::xml_node (not a pointer).

This is what I've tried, but it fails to compile.

Code:
PugiXmlNode PugiXmlNode::appendChild(std::string name)
{
return (PugiXmlNode) (append_child(name.c_str()));
}

Is there any way to do this?

View 2 Replies View Related

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++ :: 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++ :: Class Function That Uses Instance Of Its Child Class As Argument

Mar 1, 2013

I am facing a real-life problem, it can be simplified as below:

#include <iostream>
using namespace std;
class B;
class A {
public:
void f1(A a) {}
void f2(B b) {}

[Code]...

There is no problem at all with the f1(), it compiles and executes without any problem. But f2() gives compilation error. How to solve this?

The error message is: error: 'b' has incomplete type This is just to define the function f2() in a class, that uses an instance of its child class as one of its arguments.

View 11 Replies View Related

C++ :: Using Child Class As Parameter Of A Function In Its Parent Class

Aug 27, 2014

I am currently having an issue with a piece of code that I am writing in which I need to use a vector of a child class as a parameter in a function in the parent class. Below is an example of my code:

#include "child.h"
#include <vector>
class parent {
parent();
function(std::vector<child> children);
// rest of class here
}

When I do this my program doesn't compile. However if I try to forward declare, as shown in the following example, it once again refuses to compile:

#include <vector>
class child;
class parent{
parent();
function(std::vector<child> children);
// rest of class here
}

This time, it refuses to compile because it needs to know the full size of the class child in order to create the vector. How to being able to access the child is essential for my program, so what should I do?

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++ :: 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 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++ :: Using OOP To Implement Matrix Class That Provide Basic Matrix Operations

Mar 27, 2013

i want to know how i can solve this question? do i need to create a class or write the program codes.

View 12 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++ :: 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++ :: 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++ :: Size Of Object Of Class Child?

Feb 5, 2014

hiclass Parent {
};
class Child : virtual public Parent {
};

What is the size of object of Class Child in following case?

View 17 Replies View Related







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