C++ :: Constructors / Classes For Expression Tree Program

May 7, 2012

Previously I made a infix postfix calculator with integers only using structures, which had only three files

1.main.cpp,
2.arithmetic_expression.cpp
3.arithmetic expression.h,

I manged eventually to bring it together. now there are 5 files which are using classes, constructors and destructors and there can be letters in the expression that can have assigned values. Newnode, struct node and all that fine and dandy stuff made sense, this doesn't. What needs to be done in those empty functions in arethmetic_expression.cpp? It was going well until I reached createxpressiontree{ in th arithemetic_expression.cpp and those constructors in there. At that point I missed my structures and was totally confused by the constructors. I know constructors are supposed to create objects, but then I dont know what i am creating the object for and what that object is supposed to do.

1.main.cpp
2.arithmetic_expression.cpp
3.tree.cpp
4.arithmetic_expression.h
5.tree.h

Main.cpp

Code:

#include <iostream>
#include <string>
#include <vector>
#include "arithmetic_expression.h"
#include <map>
int main() {
// arithmetic_expression expression1;
// Testing the RPN input

[Code] .....

View 1 Replies


ADVERTISEMENT

C++ :: Constructors In Derived Classes

Apr 7, 2014

As long as no base class constructor takes any arguments, the derived class need not have any constructor, if one or more arguments are used then it is mandatory for the derived class to have a constructor and pass the arguments to base class constructors. While applying inheritance, we usually create objects using derived class. Then it makes sense for the derived class to pass arguments to the base class constructor. When both the base and derived class contain constructors ,the base class constructor is execute first.

In case of multiple inheritance, the base classes are constructed ,in the order in which they appear in the declaration of the derived class. Similarly in a multiple inheritance the constructors will be executed in order of inheritance. Since the derived class takes the responsibility to supply initial values to the base class,we supply the initial values that are required by all the classes together where the derived class object is declared.

The constructor of the derived class receives the entire list of values of arguments and pass them on to the base constructors int the order in which they are declared in the derived class

View 1 Replies View Related

C++ :: Streaming Input Through Two Constructors (two Classes - Using ADTs)

Jan 7, 2013

I have a main .cpp file which contains int main(int argc, char** argv) and 2 included personal headers which correspond to their linked implementation files.

What I am trying to do is use ifstream to pass integer values from a text file into my program, have the program execute, and then output those modified integers back into the same text file. I have tested the "program" portion of my project using declared values. I know that my program does what I have intended for it to do, but I can't seem to properly inject any outside data using files.

Here's what I have (not including all of the unrelated objective code):

Main.cpp

int main(int argc, char** argv) {
file File;
std::string gameFile = File.findFile(); // prompts user for existing file
if(gameFile.size() > 0) { // load saved game
std::ifstream in;
in.open(gameFile.c_str());

[Code] .....

In game.cpp, I am unsure of Player = new player; and Player = new player(data);. I have never tried doing this. This is simply my best guess of how to make this mess work properly. Hopefully I am close?

Game.h:

#ifndef _GAME_H_
#define _GAME_H_
#include "player.h"
class game {
std::string loadedGame;

[Code] .....

In game.h, I haven't yet tried anything like player* Player;. As stated in my comments above for game.cpp, I have no clue how to go about this.

player.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include "player.h"
player::player() {
srand(time(0));

[Code] .....

In player.cpp, I really have no clue where to start with the issue. I'm not even exactly sure if any values are being passed to these variables. Though, I honestly haven't taken much time to problem solve. I don't want to waste a lot of time just to find out that my attempt is incorrect and/or unconventional.

player.h

#ifndef _PLAYER_H_
#define _PLAYER_H_
class player {
public:
player(); // default

[Code] ....

So really, my concerns are: Is my attempt mostly correct? If not, why?

View 1 Replies View Related

C++ :: Adding Mathematical Expression In Binary Tree

Jul 23, 2013

I want to add mathematical expression in a binary tree but I have some problems with the algorithm. I found this one:

-If the current token is a '(', add a new node as the left child of the current node, and descend to the left child.
-If the current token is in the list ['+','-','/','*'], set the root value of the current node to the operator represented by the current token. Add a new node as the right child of the current node and descend to the right child.
-If the current token is a number, set the root value of the current node to the number and return to the parent.
-If the current token is a ')', go to the parent of the current node.

Here is the code that I made so far:

template<class T>
void Tree<T>::Expr(Node<T> *node, char expr[], int &i) {
i++;
T x = expr[i];
if(x == '(') {
node = node->Left;

[Code] ....

I know that it is a big mess and it doesn't follow the algorithm but this is the problem. For example if the token is '(' I go to the left child of the current node. Then lets say that the next token in the expression is a number. I add this number to the current node and I must go back. But how can I go back to the parent? I will go back to line 13 and the program will end. What should be the structure that I must use?

View 1 Replies View Related

C++ :: Evaluate Mathematical Expression That Is Already Formed In Binary Search Tree

Apr 23, 2013

The point of my code is to evaluate a mathematical expression that's already formed in a Binary Search Tree. : ((6+5)-3)*(2/1)=?

I've read other forums and they usually use cout, cin and << >> but I'm still in the learning process so this is basically primitive coding.

Anyway, The recursion is already correct (I think). My problem is how to return char* or should I say an element which has 2 or more digits.

typedef struct cell{
char elem[max];
struct cell *left;
struct cell *right;
}celltype, *node;
node A;
char* evaluate(node *A) //passed as evaluation(&A);

[Code] ....

And the reason that I'm using char is because the operators are also elements in different nodes.

The code above is actually the result when I remembered that you can't return 2-digit char.

The code below is before I remembered that. It works when all results are 1 digit numbers.

typedef struct cell{
char elem;
struct cell *left;
struct cell *right;
}celltype, *node;
node A;

[Code] ....

View 1 Replies View Related

C :: Expression Extraction In Program Runtime From User

Apr 6, 2013

I want to extract expressions like <23.34 463.2 23.2 + *> in program runtime from the user. (I am trying to evaluate postfix expression)....

View 3 Replies View Related

C :: Program To Evaluate Postfix Expression Using Array Implementation Of Stack

Apr 26, 2014

Write a program that evaluates postfix expression using array implementation of stack.

The expression [the input] is evaluated from left to right using a stack. When the element read from the expression is an operand, push it into the stack.When the element read from the expression is an operator: Pop two operands from the stack.Evaluate the two operandsPush the result of the evaluation into the stack.

The final result lies on the top of the stack at the end of the calculation. Make sure to display the result before terminating the program.Write a program that evaluates postfix expression using array implementation of stack.

Code:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define M 20

typedef struct{

[Code] ....

View 1 Replies View Related

C :: Exponent Program - Expression Evaluates To A Function Which Is Missing Argument List

Dec 15, 2013

I am new to C programming and I am trying to compile and run an exponent program my instructor posted for us but it is giving me an error saying:

Warning c4550: expression evaluates to a function which is missing an argument list.

Why this is happening (she doesn't seem to find anything wrong with the code). From what I could gather there is some issue with the math but idk. It is supposed to prompt for the number and the exponent to raise it to, then calculate and output the result.

Code:
#include <stdio.h>
int main() {
int base, exp;
long long int value=1;

[Code] .....

View 9 Replies View Related

C++ :: How To Know If Classes In Program Are Being Used

Aug 13, 2014

In this program the intention is to create a menu driven program for a pizza restaurant. I have to use a class called Pizza and have to include at least three public functions; one is called SetSize, another one is called Display, and the last one is called ComputePrice. A small pizza is worth $10, a medium is $14, and a large is $17. Each topping is worth 2 dollars. I know that the program runs correctly, but I have doubts over the classes and function actually being utilized correctly or at all.

Program:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Pizza {
private:

int size;
int style;

[Code] .....

View 8 Replies View Related

C++ :: Design And Implement A Program That Consists Of Two Classes?

May 15, 2013

I am trying to design and implement a program that consists of two classes. The Base class A is purely for supporting class B. I do not want to be able to create an object of class A. When object B is created it inherits from class A.

What is the best way to achieve this?

View 8 Replies View Related

C# :: Block Program From Seeing Process Tree

Dec 7, 2011

I have a program that I can only run one instance of. When I try to start it a second time, it doesn't do anything. I would like to create a script that loads the program in a way that I can run multiple instances. Since I assume the program checks the process tree on startup to see if an other instance is already running, I figure disabling access to the process tree could do the trick.

View 6 Replies View Related

C++ :: Plant Tree Program Memory Leak

Mar 2, 2012

Write a C++ program that had a base class PLANT with a sub class of that of TREE. The Program works exactly to her specifications except there is a memory leak.

--Header Files --
Plant.h
Tree.h
--Resource Files--
plant.txt - For Input
tree.txt - For Input
plantReport.txt - Output of Program. No cout is really allowed by her.
--Source Files--
Plant.cpp - Code for plant.h class
Tree.cpp - Code for tree.h class
MainDriver.cpp - Contains int main()

View 14 Replies View Related

C/C++ :: Find Maximum Element From A Tree (not A Binary Tree)

Oct 31, 2014

I want to find maximum element from a tree (not a binary tree ) ???? ...using c language...

View 1 Replies View Related

C/C++ :: Binary Search Tree Program - Segmentation Fault Error

Mar 21, 2014

I'm writing a binary search tree program and I got it to compile but as soon as I input something it returns a "segmentation fault error" . I suspect the issue with the code is withing my `add` function.

template<typename T>
void BinarySearchTree<T>::add(T value) {
if (m_root == nullptr) {
Node<T>* node = new Node<T>;
node->setValue(value);
m_root = node;

[Code] ....

View 6 Replies View Related

C++ :: Tag Dispatching Constructors

Dec 24, 2014

#include <iostream>
#include <string>

struct A {
struct Tag{};
std::string name;
int value;
A (const std::string& n, int v) : name(n), value(v) {}
A (const std::string& n, int v, Tag) : name(n), value(v) {std::cout << "Constructor with Tag called.

[Code] ....

How to avoid having to type out a second near-identical constructor for any class like D and E which have specialized constructors different from A? You can imagine the nuissance this causes if there are many classes like D and E (and with many parameters) that need the Tag parameter. The nuisance will be there to when making changes to the constructors. Delegated constructors I don't think will work because of passing `tag` into the parent's constructor. Is there some sort of inheritance trick I can apply simultaneously to all such classes to get them all to behave like B and C's constructors?

View 2 Replies View Related

C++ :: Constructors In A Singleton

Apr 6, 2013

I need to implement a singleton, so I've been reading about it online and I'm still not quite sure about all the types of constructors I need to declare:

Code:
class my_singleton {
private:
my_singleton();
my_singleton(my_singleton & X);
my_singleton(const my_singleton & X);

[Code] ....

Is this OK?

View 8 Replies View Related

C++ :: Creating Binary Tree Program - Allow User To Input Data Types

Apr 23, 2013

In class we were asked to create a C++ BTree program that would allow a user to input the following data types and then store said data in a .txt file:

0. ID 8 bytes

1. First name 30 char

2. Last Name 30 char

3. Street Address one 30 char

4. Street Adress two 30 char

5. City 30 char

6. State 20 char

7. Zip 10 char

8. Country 30 char

(I'm not particularly asking for full code, pseudo code would also be great). I had a great deal of my work done, unfortunately, the computer I was working on crashed, corrupting my files.

View 4 Replies View Related

C++ :: Array Of Classes With Classes Inside

Oct 5, 2013

I have an array of (Student)classes created in Manager.h, which contains a new instance of class Name (name),(in Student.h)How would I go about accessing the SetFirstName method in Name.cpp if I was in a class Manager.cpp? I have tried using Students[i].name.SetFirstName("name");

// In Manager.h
#include"Student.h"
class Manager
{

[Code]....

View 2 Replies View Related

C++ :: Multiple Default Constructors Specified

Dec 16, 2014

I have an inherited class that essentially manages a Qt Window.

It is as follows (prototype below):

class QTMyOpenGLWindow : public QWindow, protected QOpenGLFunctions {
Q_OBJECT

[Code] ....

Now, I can understand the confusion of the compiler, but the functionality as I laid it out works for me (I can create the class with just specifying the parent and also have the option of preventing auto-initialization when creating). But, is there a better approach?

View 3 Replies View Related

C++ :: Copy Constructors In Inheritance

May 16, 2013

#include <iostream>
using std::cout;
using std::endl;
class CBox // Base class definition
{
public:
// Base class constructor
explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv)

[Code] .....

This example produces the following output:

// Derived class copy constructor
CCandyBox(const CCandyBox& initCB): CBox(initCB) {
std::cout << std::endl << "CCandyBox copy constructor called";
// Get new memory
m_Contents = new char[ strlen(initCB.m_Contents) + 1 ];
// Copy string
strcpy_s(m_Contents, strlen(initCB.m_Contents) + 1, initCB.m_Contents);
}

It will work right? Cause when I do "CBox(initCB)" it only sends the BASE part of the derived object to the base class copy constructor, is it copy or default?

View 6 Replies View Related

C++ :: Using Constructors In Template Class?

Sep 13, 2014

The code below references to a header file and implementation .cpp file, which are not important. My question is what is the proper way to use a constructor in a main file. I have been getting "invalid use of" errors when using letters.Pair(a,b), where Pair(T a, T b) is a constructor that accepts arbitrary type T of variables 'a' and 'b'. So I played around a bit and suddenly found a syntax that works. I need verification for the syntax below:

#include <iostream>
#include "pair.h"
#include "pair.cpp"

[Code].....

Are the comments with the asterisks correct? As in this is always the way you initialize and assign? So letters.Pair(a, b) is not the right way to use constructors?

View 2 Replies View Related

C/C++ :: Calling Constructors With Parameters

May 28, 2014

I created 3 Rectangle pointers. And later in the program, I would like to modify these existing Rectangles by calling constructors with parameters. Is this possible? I have a sense that it involves creating overload operators, but I am not sure how to do it, or if that's the correct path.

#include <iostream>
#include <string>
using namespace std;
// Base class
class Shape {
protected:
int width;
int height;

[code]....

View 6 Replies View Related

C++ :: Link Error Although All Constructors Appear To Be There

Feb 16, 2015

I'm getting a massive 1300 char link error with VC10. It appears to complain that it can't see the constructor although the constructor is definitely there.

Error:
test_GatewayNS.obj : error LNK2019: unresolved external symbol "public: void __thiscall std::allocator<class BinarySearchVector::ElementTemplate<class Gateway,unsigned __int64> *>::construct(class BinarySearchVector::ElementTemplate<class Gateway,unsigned __int64> * *,class BinarySearchVector::ElementTemplate<class Gateway,unsigned __int64> * const &)"

[Code] ....

However, the constructors seem to be there and if I copy them into my program just to make sure - the compiler complains that they are already defined:

namespace BinarySearchVector {
template <class ElementType, class IdType> class ElementTemplate //allows comparison functions to be redefined {
public:
ElementTemplate(IdType myId) : id(myId), tickCount(0), requestingDeletion(false) {};

[Code] ....

Any clues as to what I'm missing ?

View 2 Replies View Related

C++ :: Copy Constructors For A Dynamic Array

Dec 13, 2013

I am trying to figure out copy constructors for a dynamic array and I am definitely missing something. If I go into the copy constructor routine during debug, the values appear to be correct but they don't percolate up to the newly created object. I'll post a portion of the code below:

Code:

// include header files for the classes that are being used
#include "stdafx.h" //

NOTE: THis reference must be added to all cpp files in Visual Studio Express 2013

#include <iostream>
#include <string>
#include <cstdlib>
#include <map>
using namespace std;
const int ARRAY_SIZE_DEFAULT = 32;
class vectorOfInt {
public:

[code]....

The size of c is 0. Values of a were not copied to c, although they appear to do so within the copy constructor routine.

View 7 Replies View Related

C++ :: Default Constructors And Header Files?

Jun 8, 2013

I'm working on trying to figure out constructors and header files. Can ya'll help me out with this? I'm sure my code looks like a mess as I tried to piece together different solutions I've found. There's also an attempted copy constructor and operator function. Basically my problem is my source file says there is no default constructor for my class type. Here's my header code:

#include <iostream>
#ifndef _car
#define _car

[Code].....

View 8 Replies View Related

C++ :: Writing Test Programs Involving Constructors

Jan 30, 2014

I am currently trying to write a test program involving constructors. I know what I am about to ask is pretty basic stuff but Visual Studio is not recognizing when I declare strings for some reason. My code isn't completed yet but as I am typing string in to declare the variable in the class Visual Studio is not recognizing string as a usable value.

Code below:

#include <cassert>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;
class college {

[Code] .....

Like I said... this is completely unfinished I just need to understand why my strings aren't being recognized.

View 2 Replies View Related







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