C++ :: Changing Virtual Function Output Without Using Any Data Member

May 10, 2014

Instead of this:

#include <iostream>
struct Object {
int size; // Want to avoid this because size is (almost always) constant
Object (int s): size(s) {} // for every Object subtype.

[Code] ....

I want this:

#include <iostream>
struct Object {
virtual int getSize() const = 0;
};
struct Block: Object {
int getSize() const {return 5;} // always 5, except once in a blue moon it may change

[Code] ....

The Decorator Pattern works (getSize() can then return 6) but it is a poor choice for my program because it will cause more problems (due to many containers holding its old address, among other things. Any way to achieve this without any change of address, and without creating new storage somewhere, either inside the class or outside the class (too much responsibility to follow that stored value for the rest of the program just for this rare change, and creating a data member uses up too much memory for the many, many Block instances)?

View 5 Replies


ADVERTISEMENT

C++ :: Overload Virtual Member Function In Polymorphism?

Nov 30, 2013

I defined a virtual class and three other classes based on it. I want to use them like this:

int main() {
Dirichlet_t D;
Neumann_t N;
Cauchy_t C;

PDEBoundary_t * B1=& D;
PDEBoundary_t * B2=& N;
PDEBoundary_t * B3=& C;

[Code] .....

but I got two major errors
1: "object f abstract type is not allowed" error.-----why not?
2: "the derived class must implement the inherited pure virtual method"-----Did't I?

View 15 Replies View Related

C++ :: Virtual Can Only Exist In Non-static Member Function - Field Has Incomplete Type Void

Dec 5, 2014

I'm writing a class "Property" for a program that manages different types of properties. This is my .h for y base class. I was trying to write a virtual void function to convert different children classes to strings that can be displayed, but Xcode is freaking out.

I had it as:

virtual void toString()= 0;

and it gave me an error message: "Virtual can only exist in non-static member functions" and "field has incomplete type 'void'"

I changed it to:

virtual string toString() = 0;

and the error message didn't change.

Is this an issue with Xcode or did I do something wrong? Even after changing it to string it told me that it "has incomplete type 'void'"....

View 1 Replies View Related

C/C++ :: Use Virtual Function In Class In Which Virtual Function Is Defined?

Dec 27, 2012

class Parent{
  public:
virtual int width();
    virtual int height();
    int area(){return width()*height();};

[Code] ....

View 10 Replies View Related

C++ :: Overriding Of Overloaded Virtual Member

Feb 5, 2013

I've got the following code with output. I can't figure out myself why it's what printed out there. I believe, it has something to deal with overloading/overriding/virtual functions implementations in C++:

class Base{
public: virtual void f(int);
virtual void f(double);
}

[Code].....

Thus here're my conclusions:
1) in line
d.f(1.0);
for some reason compiler preferred casting double->int of the argument and then call to 'Derived::f(int)'.

2)in line
pb->f(1.0);
for some reason compiler preferred call to 'Base::f(double);'. 'Base' is static type of pb, but the dynamic type is 'Derived'.

I believe the answer has to deal with the fact whether virtual table contains in addition to functions' names also the types of arguments they accept. AFAIK, vTable doesn't include such info.

View 7 Replies View Related

C++ :: Pointer-to-member Array Crashes With Virtual Inheritance?

Sep 30, 2014

I've got the following code which demonstrates a problem :

Code:
struct A {
double x[2];
double i;
};
struct B : virtual public A {
double y[2];

[code]....

I'm wondering if this is a compiler bug. Why doesn't the pointer-to-(derived)-member work for the array if it works for the non-array?

View 4 Replies View Related

C++ :: Demonstrate Usage Of Each Member Function Using Test Data (width And Height)

Feb 11, 2013

Provide the definitions for the following class declaration and then write a program that demonstrates usage of each member function using the test data of 10 for width and 8 for height. Remember to put your main program, class header and class source into separate files. This is an easier version of the exercise than the one you have been asked to practice. Only provide code for the methods specified in the class declaration. Note that the displayRectangle() method should output an appropriate ‘box’, its dimensions, area and perimeter to the screen.

class Rectangle {
public:
Rectangle(int height, int width);
~Rectangle(void);

[Code] .....

View 1 Replies View Related

C++ :: Overloading Output Stream And Pure Virtual Functions?

Aug 7, 2013

I'm working with inheritance and pure virtual functions, and I want to overload an output stream operator. However, every time I run the program I get this: 0x7fff00ee98c0.

I'll include a base class and a derived class so you can see what I'm talking about.

Base:

#include <iostream>
using namespace std;
#ifndef _Insurance_h_
#define _Insurance_h_

[Code]....

The application is something like this (I'm assuming the user has already inputted the name, salesperson, make, model, etc):

#include "Auto.h"
#include <iostream>
using namespace std;
#include <vector>
vector<Insurance *> sales;

[Code] .....

View 4 Replies View Related

C++ :: Call Function To Output Data?

Dec 10, 2014

I am having an issue with my Call Function to output data. The compiler doesn't like the Call function to output data.

#pragma once
namespace WindowsFormsApplication1 {
using namespace System;

[Code].....

View 1 Replies View Related

Visual C++ :: Access Member Function From Non-member Function In Same CPP File?

Dec 16, 2012

In my MFC, CMyPorpertyPageDlg is derived from CPropertyPage. How to access its member function from a nonmember function in the same CPP file?.

void Non_Member_Get_PorpertyPage()
{
CMyPorpertyPageDlg* pPageDlg = ....
}

View 4 Replies View Related

C++ :: Call To Member Function X Is Ambiguous - Overloaded Member From Header File

Feb 23, 2014

I get the following error in XCode whenever I try to access the member I created 'randomGen' in a separate class in a different header file. I have made sure to include the header file and have tried to access it through an object.

This is the code I enter when trying to access the method from randomiser.h in main.cpp. It is also an overloaded function with doubles and integers:

RandomG randomiser;
randomiser.randomGen(); // 'Call to member function 'randomGen' is ambiguous'

This is the code inside randomiser.h:

#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class RandomG {

[Code] ....

This is the error inside xcode: [URL] ....

I have tried seperating the code for the functions in another class (main.cpp) and then running and it seems to works, so I'm not sure why I can't put everything in the .h file and then access it?

I would like it in a seperate file so it doesn't clutter my main. I am writing a game with SDL so that might be confusing and I would like the window to have a random title and other random properties, so it would be easier to use a function.

View 3 Replies View Related

C++ :: Call Member Function Inside Another Member Function?

Mar 21, 2013

If I wanted to call a member function inside another member function, how would I do that, if it's possible?

For example, if I have Find(int key) defined already and wanted to call it while i was overloading operator+.

View 2 Replies View Related

C# :: Viewing And Changing Data In DGV With SQL Database File

Mar 4, 2014

Background: I've created a projected and added a mdf database file to it.

I am attempting to fill my datagridview with data from a table that I've added to my mdf file.

I am having a hell of a time figuring out the correct connection string and datasource file path for it.

I've decided on using OleDB because that's what I am most familiar with.

I clearly see the rule "Don't tell us to give you code!" but I cannot find a good tutorial from START to FINISH on how to do what I am trying to do.

As stated, I think my connection string is wrong. Even if I change it to

OleDB (string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database.mdf"

I get a new error saying it cannot recognize the database format.

public partial class DataViewer : Form {
string conStr = "Provider=SQLOLEDB;Data Source=Database.mdf";
DataSet dS = new DataSet();
DataSet tdS = new DataSet();
OleDbConnection con = null;

[Code] ....

View 2 Replies View Related

C++ :: How To Convert String To Double Without Changing Actual Data

Jun 3, 2013

I have to convert string to double. i'm using "atof" function to achieve same.

I have string as "0.0409434228722337" and i'm converting with "atof" But i'm getting double value as "0.040943422872233702". Why it adds 02 additionally at the end?

More example :

"0.0409434228722337" converts to "0.040943422872233702"
"0.067187778121134" converts to "0.067187778121133995"

Is there any other possibility to convert string to double without changing data ?

View 5 Replies View Related

C++ :: Alternative Of Virtual Function?

Sep 25, 2013

have a look-

class Base
{
private:
{

[Code].....

my question is why we can't directly call the member function of the desired class instead of using virtual function.

***********same program using virtual keyword*******************

class Base
{
private:
{

[Code].....

Why we generally prefer the 2nd one i.e with virtual keyword. why we can't directly call the member function of the desired class instead of using virtual function...make me understand this point..

View 5 Replies View Related

C++ :: Multiple Of CSV File Used As Input / Extracting Data To A Output File - Getline Function

Jun 4, 2013

I have written a C++ program I have multiple of CSV file used as input, which I open one at a time and close it after extracting data to a output file which is the only file.

I run getline(inFile,line);
outFile << line << endl;

I run this code, and only part of it is goes to the output file I got, also have spacing randomly to specific file and inconsistent

But when I slower the code, like system("Pause") in the loop, I can get extract what I want perfectly....

Is my program running to fast, why getline would be skipping part of what things I want?

View 11 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++ :: Define To Overload Virtual Function

Jul 11, 2014

I want to overload pure virtual function from 3rd party SDK class to put my debug messages like that:

errorStatus aXsaction(){printf(_T("
abort transaction"));transactionManager->abortTransaction();}

#define transactionManager->abortTransaction() aXsaction()

But compiler complains on the minus sign:
error C2008: '-' : unexpected in macro definition

Is it possible to trick the compiler?

View 4 Replies View Related

C++ :: Changing Recursive Function Into Iterator Function?

Nov 29, 2014

I had the following question in my exam and received 3 out of a possible 4 marks

Here is the question:

Assume the following recursive function:
int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

Translate this function into an iterative version:

Here is my solution:

int f(int i)
{
int prev,next,final;
prev = 0;

[Code]....

View 9 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i)
{
int prev,next,final;
prev = 0;
next = 1;
if(i==1)
final = i;

[Code] .....

View 1 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function?

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i) {
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i) {
int prev,next,final;
prev = 0;
next = 1;

[Code] ....

View 1 Replies View Related

C++ :: Virtual Function Defined / How To Define A Destructor

Aug 22, 2014

I wrote the following program, it can be compiled and run, but there is warning saying that if virtual function is defined, there should be a destructor. How to do that I tried many different ways I can thought of, but none of them works.

#include <iostream>
using namespace std;
class cell_c {
public:
double p;
cell_c() {p=1;}
virtual void print() {cout<<p<<endl;}

[code]....

View 1 Replies View Related

C++ :: Pure Virtual Function Called At Runtime

Jul 12, 2014

I'm currently making a game and what happens is that during runtime, it suddenly closes and a message is shown in the console saying "Pure virtual function called at runtime".

Here is the code: [URL]

The problem seems to occur somewhere between lines 662 - 695. And it seems to only happen when the size of the vector reaches 1.

View 2 Replies View Related

C++ :: Non Constant Member Function Being Called Inside Constant Member Function?

Dec 28, 2012

#include <iostream>
class Hello {
public:
void Test() {

[Code].....

As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .

View 5 Replies View Related

C :: Changing The Value Of Something From A Function

Oct 30, 2013

I was talking to someone earlier about how to change the value of something from a function, and they said what was needed was to use a ** to change something, and was wondering if I could get a walk - through of what happens. I understand a single pointer well enough, but a pointer through a pointer is kind of confusing to me. Here is a simple example.

Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int add(int ** TOP, int * stack);

int *stack = NULL;

[Code] ....

Why is it that when the program prints the address of TOP in main, it is different than the address of TOP in the function? Is it because it is a different instance of TOP because it is in the function? When I put the number on *TOP, and come out of the function back to main, it then says the address of TOP is the number entered into *TOP, and am not sure why. And the **TOP ++ at the end I am thinking it increments malloc by 1, therefore bringing the pointer TOP up to point at the next element, or am I completely off base there?

View 7 Replies View Related

C++ :: Define Virtual Function Compare In Subclass Score?

Mar 26, 2014

I am trying to bend my head around polymorphism but I can't figure out how to use the virtual method properly. I have the following superclass:

Code: class comparable
{
public:
comparable();
virtual bool compare(comparable &other)=0;
}; and the subclass:
Code: class score : public comparable
{
public:
score(string player,int highscore);
bool compare(comparable &other);
string name;
int highscore;
};

My problem is that I do not know how to define the virtual function compare in the subclass score:

Code: bool score::compare(comparable &other){
if(this->highscore > other.???){
...
}
}

How do I tell the compiler that the type is of score?

View 7 Replies View Related







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