C++ :: Creating Own Vector Class?

Nov 15, 2014

I was trying to implement own vector class and wrote some code. Below is the code.

Code: #include<iostream>
#include<vector>
using namespace std;
template <typename T> class MyVector
{
T *mem;
int m_size,final_size;
public:
MyVector() : final_size(1),m_size(4)
{

[code].....

I have question on this function.

Code: myVecPush_back(T t){}

Here I was able to put elements upto any number of time, but I have allocated memory to only 4 elements in T *mem.

My question, why the program is not crashing when I tried to put elements more that 4?

Is since the variable is of type Template? Any specific reason for this?

View 2 Replies


ADVERTISEMENT

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++ :: Create Class Vector As Template And Define Operations On Vector?

May 13, 2013

I need to create a class vector as a template and define operations on vectors.

And this is what I made.

#include<iostream>
using namespace std;
template<class T>

[Code].....

View 2 Replies View Related

C/C++ :: Creating A Temporary Vector Of Pointers?

Sep 10, 2014

Is it possible to create a temporary

std::list of pointers

I would like to pass a temporary

std::list

to the constructor of a class to initialize its own one.

For example, using a

std::vector
:
#include <iostream>
#include <vector>
void func(const std::vector<int*>& myVec) {
for(int i=0; i<myVec.size(); ++i){

[code]....

Can we do this? What are other possible problems in addition the ones I have just mentioned above?

View 14 Replies View Related

C++ :: Creating Vector Array And Reading A File

Apr 23, 2013

Creating a Vector Array + Reading a file ....

View 1 Replies View Related

C++ :: Creating (Composite) Vector Array Field?

Jan 3, 2013

In a numerically intensive code, I have a Cartesian vector class Vector3d which has the normal operator overloading and basic functions such as dot product, magnitude, etc. For simplicity, assume that it is not a templated class and its components are of type double.

I frequently need large 1-d arrays (e.g. stl vectors) of Vector3d. Two use-cases must be satisfied:

1) The trivial case in which the data are stored as stl vectors of Vector3d;

2) The more difficult case where the individual components are stored as stl vectors of double, and are not guaranteed to be contiguous in memory (so one cannot rely on "stride").

Assuming the array lengths are all identical, I'd like to be able to access both types in a single loop. The straightforward way for case 2) is to construct a temporary Vector3d from the three components (indexed with the loop index). However, I would prefer not to incur the overhead of the constructor.

Is it possible using template metaprogramming. Ideally I'd like a CompositeVector3d that inherits from Vector3d and is constructed with the component vectors, but can be dereferenced using the loop index in the same way as one would do with case 1.

I am not looking for the typical template metaprogramming utility of being able to operate on the entire array without explicit loops. I just want the syntactic "sugar" whereby CompositeVector3d and Vector3d act the same, plus the avoidance of the cost of the constructor. I am not averse to using an additional templated class (perhaps a Field or a View class) to access the basic storage in both case.

Is it possible to do this without using a full template metaprogramming utility?

View 1 Replies View Related

C++ :: Triangular Distribution For Creating Momentum Four Vector

Nov 12, 2014

In my problem I am to create a base class to represent a four vector (a concept in physics involving a four dimensional vector) then create a derived class specifically to represent the four momentum of a particle which inherits from the base class. I have been supplied a small piece of code to use to generate a 'random' x y and z component of the momentum magnitude. The code is as follows

#include <cstdlib>
double triangular(double momentum){
double x, y;
do{
x = momentum*rand()/RAND_MAX;
y = x/momentum;
} while (1.0*rand()/RAND_MAX > y);
return x;
}

It is said in my problem that this code is supposed to generate the magnitude, and then randomly split into x, y and z components. This code returns a single value and so I cannot see how it is doing what it says in the problem.

View 2 Replies View Related

C++ :: Creating One Class Object In Constructor Of Another Class

Sep 20, 2013

Suppose I have two classes A and B so how to access object of class A in constructor of B's class as a parameter.

View 6 Replies View Related

C++ :: Recursive Function - Creating Vector Of Every Unique Combination Of Commands

Mar 24, 2013

I'm trying to write a recursive function that takes in a vector of strings that contains

"1 forward", "2 forward", "rotate left", "2 backwards" ... etc

how can I write recursive function that creates a vector of every unique combination of commands?

View 8 Replies View Related

C/C++ :: Error In Vector Pushback - Creating Random Unwanted Numbers

Apr 12, 2014

I'm currently writing a chunk of code that will take inputs from the user and push them into a vector until 0 is entered, at which point it will break the loop and continue on with the rest of the program. This is nothing I haven't done before, but I have never encountered this error.

The code chunk looks like this:

typedef vector <int> ivec;
int main() {
ivec nums;
int input;
while (true) {
cout << "Enter a positive integer, or 0 to quit" << endl;

[Code] ....

My standard testing input has been 3 5 6 3 8 (then 0 to quit), so one would expect my sequence to be 3 5 6 3 8...but instead after the 8 I get a random number value that is usually quite large and I cannot figure out where it comes from (ex. 3 5 6 3 8 201338847).

View 9 Replies View Related

C++ :: Passing Vector Of Class To Function Of Another Class?

Dec 14, 2014

im passing a vector of a class to a function of another class. But i cant access the data on the classes inside the vector.

Something like that:

class CDummy{
...
public:
string m_name;

[Code].....

Im creating the vector on main() and using push_back with a pointer to an initialized CDummy instance

View 5 Replies View Related

C++ :: Creating New Pointer For A Class

Aug 2, 2013

I'm currently reading the C++ Guide for Dummies

Anyway right now I'm working with pointers and classes, and when I create a new pointer for my class it looks like this...

Pen *Pointerpen = new Pen;

But in the book they threw in this...

Pen *Pointerpen = new Pen();

Can you actually designate memory space for a function? Or was this a typo on their part? It's never come up before and they didn't explain it.

View 5 Replies View Related

C++ :: Creating A Function In A Class?

Feb 16, 2014

I am currently working on a "bag" class which is sort of a common sense answer to creating a random class with difference functions. I am attempting to create a "union" function which takes two bags ie: bag1 and bag2, adds all the items in both bags and creates a new bag ie: "bag3". For some reason I keep coming up with problems instead of solutions. Maybe it's the fact I just got done with 2 days of calculus. I don't know. My code is below. Both a main(source) and header file.

Header

#ifndef BAG_H
#define BAG_H
const int BAG_CAPACITY = 20;
template <typename T>
class Bag {
private:
int count; // Number of items in the Bag

[code]....

View 1 Replies View Related

C/C++ :: Creating New Instance Of A Class?

Mar 2, 2014

I have a background in c# and am very frustrated with c++. If I created a class in c# like so:

public class Memory{
int x = 0;
int y = 0;
int height = 0;
int width = 0;
string firstname = "Bob";
string lastame = "Chester";
}

and then created a new instance of this class from a separate class by doing:

public class Main{
Memory mem = new Memory();
}

I have raked the internet for a way to create a new instance of a class in c++ while keeping its default values and have come up empty handed.

View 13 Replies View Related

C++ :: Creating A Property (Get / Set) Class?

May 16, 2013

I've just recently moved from Visual Basic 6 over to C++ and decided that I would try and write a class to mimic the Get/Set behavior seen in a variety of other programming languages. So instead of just using an overloaded function, i.e:

Code:
class NoProp {
LPCWSTR m_Title;
public:
void Title(LPCWSTR NewValue) {m_Title = NewValue;};
LPCWSTR Title() {return m_Title;};

[code].....

So far this code only works with the LPCWSTR type (const wchar_t*). Unfortunately I got to this stage using only that type, and now when I try another type, such as int, it fails. When using the int type I believe it creates an error because the constructor of InitProp expects a type of pointer to be parsed. When I try using it with int* type (the private variable then becoming int **m_Variable) it compiles, thought I cannot access the property like a normal int.

My best guess from here is that I probably have to overload the InitProp constructor in a way that it can setup the Property classes for base-types and pointer-types, and then also modify the Property class to handle both.

View 6 Replies View Related

C++ :: Creating A Class Variable With A String?

May 12, 2013

I have defined a class in a header file; just the class, no templates involved. I have a program where I'm reading in data in string format. Each string consists of a word, a delimiter, and a variable name. Example:

cajun/mustard

I want to take that string and make it the variable name of that class type. It would be implemented along the lines of:

Code:
string str;
//read/process string here, get:
str = "mustard";
createName(str);
//pass string to creator function When the function is called, I should get the variable:
Class mustard;

Thing is, I'm not supposed to know beforehand what the variable names are, only that I create them as they are read in. It could be mustard, it could be Maynard_James_Keenan, it could even be bazinga.

My problem is, what do I do for createName()? I've looked into the concepts of pairing, Factory implementation, and maps, but I don't think they answer my question.

(P.S. if I run into the same variable name being read in twice, what steps can I take to make sure that a duplicate variable isn't created? Do I need to add in code, or does the compiler know to watch for multiple variables of the same name?)

View 6 Replies View Related

C++ :: Creating Methods For Class List?

Jul 3, 2013

Creating the methods for class List

main.cpp Code: #include "List.h"
int main( )
{
List la; // create list la
la.push_front( "mom" );
la.push_back( "please" );
la.push_back( "send" );
la.push_back( "money" );
la.push_front( "hi" );
cout << "
la contains:
" << la << '

[code]...

View 12 Replies View Related

C++ :: Creating 2D Array Within Class Using Constant?

Oct 17, 2014

I've been given specific instructions to create an array inside a Class Matrix using a constant n. This is my class but I am getting errors. I thought that maybe I had to initialize the const and the array using the constructor function Matrix() instead of directly in the class, but I didn't have any luck with that either.

class Matrix
{
public:
Matrix();
private:
const int n=3;
int e[n][n];
};

View 4 Replies View Related

C++ :: Creating A Heap Template Class

Sep 16, 2014

I recently posted a question related to creating a heap template class. The ultimate goal is to create a series of classes that serve a purpose that was overlooked in the Qt library that I need for my current project.

The current "end goal" is a PriorityQueue template that uses a comparer class which is inherited from a "template interface". Basically a pure virtual class template. Perhaps that is the mistake to begin with but hopefully not. (Is this a valid approach?)The problem I am getting is that when I compile the code, it says my derived comparer class is abstract.

I will include all related classes here. I doubt it is relevant but the templates and the classes based off them are in different namespaces.Here is the comparer "template interface":

// in global namespace
template<class T>
class IIMQOBJECTS_EXPORT IQComparer {
virtual int compare(T& a, T& b) = 0;
virtual bool equals(T& a, T& b) = 0;
virtual bool isGreaterThan(T& a, T& b) = 0;
virtual bool isLessThan(T& a, T& b) = 0;
};

Here is the class that is supposed to be non-abstract but isn't recognized as such:

// in the application namespace AND IN SAME project that has the NetEventInfo class
// all functions ARE defined/implemented in a cpp file
class APPCORE_EXPORT NetEventInfoComparer : ::IQComparer<NetEventInfo*> {
public:
NetEventInfoComparer();
~NetEventInfoComparer();

[code].....

View 6 Replies View Related

C# :: Creating Instance Of A Class Given Its Assembly Object

Jan 15, 2015

Let's say I reference a dll in my project that has an abstract class in it:

public abstract class Module {
public string type;
public abstract string doSomething();
}

And then I have an Assembly object, loaded from a dll that is not referenced in project, but rather loaded during run-time, containing:

public class Tester : Module
{
public static string type = "Test";
public override void doSomething() {
//Stuff being done
return "hello";
}
}

How can I get the value of "type" in the class that is loaded during runtime? How do I use the "doSomething" method of it and get the returned object?

View 8 Replies View Related

C# :: Creating A Class And Methods For Dice Game?

Mar 21, 2015

The only difficulty im having is creating a class and methods & being able to access them from my win form app. Can i get a few tips on the do's and donts of creating classes / methods and accessing them from form app.

This is what i have put together so far.

public partial class Form1 : Form {
private Image[] dicePics;
private int[] diceNum;
private Random randomize;
public Form1() {
InitializeComponent();

[code]....

View 3 Replies View Related

Visual C++ :: Creating Instance Of MFC Derived Class

May 23, 2013

I need to create an object of a mfc derived CFormView class that's not in the doc/template (a second view class). But it was generated with a protected ctor. Here's the code explanation with comments.

I'm thinking all the normal classes of the Doc/View template are created starting with this code, but within the template code base.

Code:

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,
RUNTIME_CLASS(CViewSwitchDoc), //<-expands to-> ((CRuntimeClass*)(&CViewSwitchDoc::classCViewSwitchDoc)),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CViewSwitchView));

But I have generated "another view" using the "Add Class" Wizard, it's a derived class of mfc CFormView which I named ViewForm. However I'm having a problem creating an instance of it because of the generated protected ctor and pulls a compile error of not being able to access ctor. Below are the header and implementation files of this said ViewForm class. How to create an object of this view ? Did I go about it all the wrong way since it's not in the doc/template group ?

// ViewForm.h file
#pragma once
// ViewForm form view
class ViewForm : public CFormView {
DECLARE_DYNCREATE(ViewForm)

[Code] ....

View 5 Replies View Related

C++ :: Creating A Class Called Time - Operator Overloading

Feb 18, 2015

I am creating a class called time and we've had to do operator overloading for <, > , <=, >=, ==, !=, ++, --, >>, <<, * , +, and -.

Well I have done and error checked them all. The only one I cannot seem to get right is the minus and its because of the error checking. I am having issues with times like this

t1 = 0:0:2:3
t2 = 0:0:1:4

t1 - t2 should equal 0:0:0:59 but it returns 0:0:1:-1.
(days:hours:minutes:seconds)

I need it to check for all cases and I just do not know how. Here is the code I have so far:

time operator- (const time& x, const time& y){
time subtract;
subtract.days = x.days - y.days;
subtract.hrs = x.hrs - y.hrs;
subtract.mins = x.mins - y.mins;
subtract.secs = x.secs - y.secs;

[Code] .....

View 1 Replies View Related

C++ :: Creating Infix To Postfix Program Through Inherited Class

Apr 25, 2014

I have the following problem on my C++ Program and I am not sure why it is not working. I am creating a infix to postfix program through an inherited class which I am not sure it is working.

#include <iostream>
#include <stack>
using namespace std;

int in_stack_Priority(char a){
if(a == '*' || a == '/')
return 2;

[Code] .....

View 3 Replies View Related

C++ :: Creating A Class To Make WinHttp Requests Easier

Apr 11, 2014

I'm using Visual C++ 2010 Express on Windows 8. What I want to do at the moment, is to create a class to make WinHttp requests easier. This is the code that I'm basically using to build the class: [URL] ....

This is what I got so far. (Completely untested). Example of how I hope that it will work:

HttpRequest MyRequest;
MyRequest.Initialize("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0","","");
htmlCode = MyRequest.SendRequest("www.cplusplus.com","GET","");
cout << htmlCode << endl;

The code:

#include "stdafx.h"
#include <windows.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
class HttpRequest {

[Code] ......

View 6 Replies View Related

C/C++ :: Play Scales At Certain Frequencies - Creating Array Within Class

Jun 24, 2014

I'm trying to create a program that will play scales at certain frequencies, but my arrays are not initializing correctly. I've read up to double check what i'm doing but it doesn't seem to be working. My only guess is my use of a global variable.

const int NotesInScale = 8;
class Scales {
private:
//all Major Scales
int CMajor[NotesInScale];
int GMajor[NotesInScale];

[Code] ....

i'm simply trying to put the frequencies in the scales (the numbers in the array) but I keep getting an error. I feel its a simple fix but i'm not seeing it.

View 8 Replies View Related







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