C++ :: Constructor That Initializes A New Inventory Object With Values Passed As Arguments

Feb 23, 2014

Write a constructor that initializes a new inventory object with the values passed as arguments, but which also includes a reasonable default value for each parameter.

#include "stdafx.h"
#include <iostream.h>
#include <stdio.h>
#include <string.h>
class inventory {

[Code] ....

I am not trying to get my homewotk done, just to understand my errors. It complies without problem. But doesn't run.

View 2 Replies


ADVERTISEMENT

C++ :: Constructor That Initializes A Pointer To Point To The Object

Aug 27, 2013

I have a program that has a base class 'control' and there are 2 dervied classes 'button' and 'textbox'. How do i make a constructor in the 'button' or 'textbox' that initializes a pointer of the data type 'control' to point to the object that invokes the constructor. the code should look like this

class control {
//data
} class button:public control {
buton() {
//code for the constructor
}
}

actually i have an array of pointers of the type 'control' and as soon as any instance of a control like button or textbox is created the constructor should make an element of the array to point to the instance

View 7 Replies View Related

C++ :: Returning Values Or Using Pointers To Redefine Variables Passed As Arguments?

Feb 28, 2014

Which is more efficient in functions? Returning values or using pointers to redefine variables passed as arguments?

I mean either using:

void ptr_Func(int *x)
{
*x = *x+1
}

or

int ptr_Func(int x)
{
return x + 1;
}

In terms of speed, memory use etc.I want to know general efficiency, I know it will obviously vary with different uses and circumstances.

View 7 Replies View Related

C++ :: Object That Is Passed To Function Is Changed Although No Pointer Is Passed

Mar 22, 2013

I am posting this simplified piece of code that is a bit confusing for me. There are two functions that I call. One shows the expected results but the result of the other one rather puzzles me.

//#define defineVecTyp Vec3f
#define defineVecTyp float
template <typename vecTyp>
vecTyp buildLaplacianPyramid(cv::Mat inputmat) {
vecTyp lapPyr;

[Code].....

Calling the function sum1 does not change the values stored in the variables val1 and val2. The output of the program is as follows:

val1= 1 ## val2= 10 // before the call of function sum1
val1= 1 ## val2= 10 // after the call of function sum1
sumOfVals= 22

This is quite obvious and as expected and I just pasted this piece of code as an example for better clarification.

However, if I call the function buildLaplacianPyramid and apply a function for Gaussian Blurring, this also effects the cv::Mat passed to the function. The line imshow("M1, after buildLaplacianPyramid",M1); therefore shows an image that is blurred. Since I am not passing a pointer to the cv::Mat I do not understand why this should be happening. I was assuming that there would be a copy of the cv::Mat M1 to be used within the function. Therefore I was expecting the cv::Mat M1 to retain its original value. I was expecting that all changes applied to cv::Mat inputmat within the function would not have any influence on the cv::Mat M1. Just like in my other example with the sum.

View 3 Replies View Related

C :: Preventing A Buffer Overflow When Strings Being Passed As Arguments

Sep 21, 2013

preventing a buffer overflow when dealing with strings being passed as arguments.

If I have a function prototype such as:

Code:

void foobar(char *bar);

That argument bar - is intended to take a pointer to a buffer of x characters in length. Inside that function, I can't get the size of that buffer, as bar is now just a pointer to a char. I COULD just make the user of this function pass a length parameter, but there is no guarantee that would be correct. Is there a bullet proof way of detecting that the user has provided a buffer that is too small?

View 11 Replies View Related

C# :: Update Initial Values Once User Has Added To Inventory

Apr 10, 2015

I am having some trouble creating a listbox in visual studio. I declared an array, set initial values, and now I would like to be able to update those values. It is a pizza inventory app, so I need to update the initial values once the user has added inventory. This is the code I have for the "Update Inventory" button:

// read in value and convert to double
double dblInput = Convert.ToDouble(txtInput.Text);
// loop through ingredients and add inventory to selected ingredient
for (int i = 0; i < dblInventory.Length; i++) {
// is item at i checked for update?
if(lstInventory.GetItemCheckState(i) == CheckState.Checked)

[Code]....

View 11 Replies View Related

C++ :: Display Fraction In Proper From Based On 2 Arguments Passed To Class Member Function

Mar 15, 2015

We're assigned a project working with classes and fractions. My goal is to display a fraction in proper from based on 2 arguments passed to a class member function proper();

My strategy was to utilize the greatest common factor between the 2 arguements, then divide both the numerator and denominator by that number and then it would display.

The program actually runs, but only seems to divide the numerator and not the denominator. This in return makes my other class member functions have incorrect comparisons and sums.

Code:
#include<iostream>
#include<conio.h>
class Fraction {
friend void compare(Fraction a, Fraction b);
friend void sum(Fraction a, Fraction b);

[Code] ....

View 14 Replies View Related

C++ :: Will Copy Constructor Does Object Initialization Using Another Already Created Object

Mar 16, 2013

will copy constructor does object initialization using another already created object? I understand that it can be applied for object initialization and not for assignment.Is it correct?

View 10 Replies View Related

C++ :: Cloning Object Passed By Function?

May 21, 2014

Is it correct for me to make a clone of testobj in function AddTest below, before i add it to my map? What i want is an user pass testobj to me though AddTest, and after i add it into my map, i do not want to have anything to do with the original testobj anymore. I.e, they are two copies, one belong to Device, one belong to the caller, both has no link to each other.

Also regarding the GetTest method, i prefer to return a raw pointer, as i do not want to force the caller to use smart pointer. And i also do not want the caller to be able to do anything that may change testobj in the map. So i am not sure to return as const reference or make a clone of testobj on the map and return it to the user (but caller need to delete the testobj when it is not used).

Code:

class Device {
public:
void AddTest(const Test* const testobj) {
_pTest.insert("ss", QSharedPointer<Test>(test->clone()));
} const Test* const GetTest(QString str) {
return _pTest[str].data();

[code].....

View 6 Replies View Related

C# :: Constructor Object Reference Not To Set Instance Of Object

Mar 28, 2014

I am trying to use web api in order to return custom json response keys. For this i have return a custom class to return json response

Custom Class:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

[Code].....

View 2 Replies View Related

C# :: User Class Does Not Contain Constructor That Takes 2 Arguments

Feb 10, 2014

I am having trouble creating a new User from my User Class. The error I am recieving:

Error3'HospitalSystemBL.User' does not contain a constructor that takes 2 argumentsC:UsersStudentdocumentsvisual studio 2010ProjectsHospitalSystemHospitalSystemLogin.aspx.cs2131HospitalSystem

In my User class, I have two constructors. Default and an overloaded constructor.

//default constructor
public User() {
}
//overloaded
public User(long UserName, string Password)

[Code] ....

I am trying to create my user in my Login class. Why is the error saying "does not contain a constructor that takes 2 arguments" when I have a constructor (above), User passing two arguments: UserName and Password? Here is my code for the login:

protected void btnLogin_Click(object sender, EventArgs e) {
try {
int Userid = Convert.ToInt32(txtUser.Text);
string password = txtPassword.Text;

[Code] ....

View 11 Replies View Related

C/C++ :: Values Passed To Put Function But Not Having Any Effect

Feb 9, 2015

I have this code:

tgaManager.fileWriter.put((unsigned char)min(blue*255.0f,255.0f)).put((unsigned char)min(green*255.0f, 255.0f)).put((unsigned char)min(red*255.0f, 255.0f));

that should pass the value decided by the min function to an ofstream object, filewriter, that call the put method to print chars in a tga image file. When I open the file, all I see it is a huge black screen. You may be thinking that the values of blue,green and red are all zero but it is not the case.

With this code:

if (x==50 && y==50) {
cout << "Min BGR: " << endl;
cout << min (blue*255.0f,255.0f) << ' ' << min (green*255.0f,255.0f) << ' ' << min (red*255.0f,255.0f) << ' ' << x << ' ' << y << endl;

I get: Min BGR: 9.54167 29.9188 47.8701 50 50

View 3 Replies View Related

C++ :: Using Static Method To Check Arguments Before Calling Constructor?

Mar 1, 2013

I would like to avoid throwing things in constructors as much as possible.

Is this good design to have a static class method that checks arguments the caller will give to the constructor. The documentation of the class will say, thou shall call this method to validate thine arguments before calling the constructor, or else segfault may befall thoust.

View 14 Replies View Related

C++ :: Correct Values Passed To The Pointers - Memory Corruption

Apr 26, 2012

My project is say that it has memory leaks. When I view it in the debugger, it shows the correct values being passed to the pointers.

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
bool ConfigGetEntry( char *pcFile, char *pcNameMust, char* &pcResult ) {
char pcBuffer[ 512 ];

[Code] ....

Its saying that i have memory corrupting at..

Code:
if ( !ConfigGetEntry( pcFile, pcName, pcRet ) ) return _strdup( pcDefault );

Also is it normal to have a bad pointer then point data into it to then clear that of the bad pointer. To have valid data now in that pointer.

View 3 Replies View Related

C++ :: How To Get Object Name In The Constructor

Mar 11, 2013

is it possible to get object name in the constructor? I would like to initialize an object of circle class without any arguments and put some pretty lines in constructor to get and save as table of chars the name of creating object. Is it possible? I work with MSVS2012.

View 4 Replies View Related

C++ :: Initializing Object Through Constructor

Oct 15, 2013

sth is in my mind.

fast way of initializing an object through constructor is

Fred::Fred() : x_(whatever) { }

slow way
Fred::Fred() { x_ = whatever; }

ok. i accept it is faster but why?? what is the difference compiler does.

View 6 Replies View Related

C++ :: Creating Object Without Default Constructor

Jun 24, 2013

I have a question about the default constructor.

To my best understanding, the compiler will provide me with a deafult constructor only if there are no any user defined constructors, at all. Now consider the following code:

Code: class MyClass
{
private:
int m_data;
public:
MyClass(int init):m_data(init){cout<<"Ctr called"<<endl;}

[Code] ....

How is it that suddenly, there is a default constructor?

View 3 Replies View Related

C++ :: Passing Object To Inherited Class By Constructor

May 1, 2013

I am trying to pass an object to an inherited clas by a constructor. I am having Cboard my board and i need to pass it to the object Cpawn.

Here under is my code:

main
Code:
#include<iostream>#include "Cboard.h"
#include "Cpawn.h"
#include "Cpiece.h"

void main(){
char location[50];

[Code] ....

View 3 Replies View Related

C++ :: CString Program - Function Should Accept String Object Arguments

Feb 23, 2015

Write a function named replaceSubstring. The function should accept three C-string or string object arguments.

Let's call them string1, string2, and string3. It should search string1 for all occurrences of string2. When it finds an occurrence of string2, it should replace it with string3.

For example, suppose the three arguments have the following values:

string1: "the dog jumped over the fence"
string2: "the"
string3: "that"

With these three arguments, the function would return a string object with the value "that dog jumped over that fence." Demonstrate the function in a complete program.

View 1 Replies View Related

C++ :: Discrepancy Between Constructor Output And Following Print Call To Object?

Jul 5, 2013

I am creating a Matrix class, and one of the constructors parses a string into a matrix. However, printing the result of the constructor (this->Print()) prints what I expect, and an <object_just_created>.Print() call returns bogus data. How is this even possible?

Snippets below:

Matrix::Matrix(const string &str) {
// Parse a new matrix from the given string
Matrix r = Matrix::Parse(str);
nRows= r.nRows;
nCols= r.nCols;

[Code] ....

in the driver program, here are the two successive calls

Matrix mm6("[1 2 3.8 4 5; 6 7 8 9 10; 20.4 68.2 1341.2 -15135 -80.9999]");
mm6.Print();
// mm6.Print() calls bogus data, -2.65698e+303 at each location. The matrix's
// underlying array is valid, because printing the addresses yields a block
// of memory 8 bits apart for each location

View 4 Replies View Related

C/C++ :: Declare Parent Object Inside Class Constructor

Mar 24, 2014

This keeps giving me the error

ecg.h:18:11: error: field "next" has incomplete type

How do I do what I need? It does the same thing whether I use a class or a struct. This is C++ code

struct ECG_node {
double voltage;
clock_t time;
ECG_node next;

[Code] .....

View 3 Replies View Related

C++ :: Unable To Pass Reference Of Object Ifstream To Constructor

Jan 30, 2013

I'm trying to pass an reference of the object ifstream to my constructor as such:

// myClass.cpp
int main(){
ifstream file;
myClass classobj = myClass(file);}

I am defining the constructor as follows, in myClass.cpp:

myClass::myClass(ifstream &file){
// do stuff
}

I get the error of "No overloaded function of myClass::myClass matches the specified type."

Also, when I build the program, it tells me "syntax error: identifier 'ifstream'"

Also, I made sure I included this:
#include <iostream>
#include <fstream>
#include <sstream>

What am I doing wrong? How can I pass an ifstream reference to my constructor?

View 3 Replies View Related

C++ :: Initializing Base Class Part From Derived Object Using Copy Constructor

Feb 20, 2012

class Base
{
char * ptr;
public:
Base(){}
Base(char * str)

[code].....

Obj1 is a derived class object where base class char pointer is initialized with "singh" and derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1.

Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in
both the classes are private.

I tried using initializer list but could not succeed.

Is there some proper way to do this?

View 4 Replies View Related

C++ :: Why Overload Constructor Not Put Values Into Arrays

Jan 7, 2014

I'm still pretty new to classes so what am i doing in this code that is wrong.

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
class BunnyInfo{

[Code]...

View 6 Replies View Related

C :: Return 2 Values From A Called Function - Arguments And Parameters

Feb 2, 2013

Is there anyway we can return 2 values from a called function. Example

Code:
float xxxx(float a, float b, float c, float d)
{///
///
///
}

void xxx() {
int e,f,g,h;
////
////
xxx(e,f,g,h);
}

So if I want for example a+b and c+d, can i return those 2 answer? I don't think its possible since I am new into C programming.

View 5 Replies View Related

C# :: Object Instance Not Retaining Field Values

Jun 13, 2014

I have this class:

class Excuse
{...
public string Description { get; set; }
public string Results { get; set; }
public DateTime LastUsed { get; set; }
public string ExcusePath { get; set; }
....}

[Code] ...

When I run it, I do get the messagebox popping up with the correct description, but then nothing happens. When I debug, I see the fields under the UpdateForm() method are set to null.

I'm guessing this happening because it is looking at the 'currentExcuse' that was declared in the form body. I thought however that redeclaring 'currentExcuse' in the method would overwrite this instance or am I wrong?

Do I need an array to make this work?

View 3 Replies View Related







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