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


ADVERTISEMENT

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++ :: Using Virtual Functions In Base Classes

Oct 19, 2014

I recall when I first started playing with C++ I was told that you should never use virtual functions unless you absolutely cannot think of a better way to do whatever you are attempting. This is something I have tried to stick to over the years - and indeed is probably why I have never used inheritance or polymorphism much in my own programmes.

However, I notice through a great deal of the code examples offered to questions here and even over on StackOverflow that commentators show no hesitation to recommend code that involves virtual functions. More so, I have even seen several instances here where - what I was taught as, but they may well have a different official name - 'pure virtual functions' (those with definitions inside a class of something like virtual int function_name(void)=0) are demonstrated and I was very clearly taught to avoid those like the plague.

I was wondering therefore has the official thinking changed since the middle nineties on when - and even whether - to use virtual functions in your programmes?

View 9 Replies View Related

C++ :: Virtual Functions From 2 Separate Base Classes?

Sep 11, 2014

So I have 2 seperate base classes, (note that I removed the variables and functions that do not relate to the topic) Object.h

class Object{
public:
Object();
~Object();

[Code].....

The error I get is saying I am calling a function declared with one calling convention with a function pointer declared with a different calling convention and this makes perfect sense because for some reason, the function pointer is pointed at the virtual function Object::update but I can't figure out why and how to make it point at the virtual function Drawable::getImage.

Also, the virtual update function is called in a different place just before this and works correctly.

View 6 Replies View Related

C++ :: Pure Virtual And Construction Order

Jan 24, 2014

I have an AbstractAgent base class that manages a background thread. The actual work done in the background thread is accomplished through a pure virtual function call.

Here's the problem: because the base class is initialized prior to the derived class, there is a race condition in which the pure virtual call might occur before the derived class is initialized. Likewise, on teardown the derived class might deconstruct before the base class destructor has a chance to stop the thread.

I'd like to know if there are any well-known patterns for dealing with this problem. All I can think of is providing start() and stop() methods which can be called from the most-derived class's constructor/destructor, but that strikes me as inelegant.

View 4 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++ :: Pure Virtual Methods And Interface Class

Jul 11, 2012

I develop add-ons for MS Flight Simulator and I use the poorly documented SDK for this. The SDK provides a .h file in which an interface class is defined (with pure virtual methods only), something like this:

Code:
class IPanelCCallback {
public:
virtual IPanelCCallback* QueryInterface (PCSTRINGZ pszInterface) = 0;
virtual bool ConvertStringToProperty (PCSTRINGZ keyword, SINT32* pID) = 0;
virtual bool ConvertPropertyToString (SINT32 id, PPCSTRINGZ pKeyword) = 0;
};

In my code, I use this interface like this:
Code:
IPanelCCallback* pCallBack = panel_get_registered_c_callback("fs9gps");
...
SINT32 id;
pCallBack->ConvertStringToProperty(propertyName, &id);

Everything works fine, but I don't understand why... I thought the linker would stop with an "undefined symbol" error because the IPanelCCallback methods, such as ConvertStringToProperty, are declared as pure virtual but defined nowhere, and I don't use any library for linking. With such an interface class, I thought I would have to defined a subclass of IPanelCCallback and define the ConvertStringToProperty method.

View 6 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# :: 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# :: 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 View Related

C++ :: Why CRTP Used Instead Of Virtual Functions

Jan 31, 2014

I was reading about the CRTP, and I can't for the life of me understand it's advantages over virtual functions.

Unless you're coding embedded systems, and can't afford the few extra bytes for the vptr, or coding something requiring high-performance, where every nanosecond counts, I just don't see why the CRTP is so attractive. It just adds more text and forces every user class that wants to use the CRTP'd hierarchy to become a template class.

I tried implementing my Functor hierarchy with the CRTP instead of virtual functions...All it did was clutter my files with angle brackets and made the whole thing look very ugly.

View 14 Replies View Related

C++ :: Virtual Functions And Polymorphism

May 2, 2014

if we do a virtual functions(polymorphism) why we need re-declare the functions(when we create a new class derived from other)?

View 10 Replies View Related

C++ :: Cannot Change Virtual Functions From Object?

Oct 14, 2013

I try the functions pointers too, but without success. I understand the objects are the way for work with class's. until here fine. But why i can't change the virtual functions from an object? is there anyway for do it? Ican't do, outside of classfunctions, these code:

#include <iostream>
using namespace std;
class test {
public:
virtual void created(){};

[Code] ....

How i can validate these line:

void a::created()
???

View 2 Replies View Related

C++ :: Overriding Inherited Virtual Functions

Feb 15, 2013

Is it possible to do something like this:

class A //parent {
public:
virtual void DoSomething() = 0;
};

class B : public A //child {
public:
void DoSomething(string s) override;
}

Where the child member function overrides and changes the parents member function.

I need to pass an array of key states to the Controller class' Update() function but don't want to send it to every class derived from Object (like Controller).

Is this possible or do I have to overload the original Update() member function (but I would need to define the method in Object then (i.e remove the pure virtual function (=0)))

View 4 Replies View Related

C++ :: Virtual Functions - Calculate Carbon Footprint For Car / Building And Bicycle

Apr 13, 2014

I'm writing a program that calculate the carbon footprint for car, building, and bicycle. i have three classes building, car, bicycle. class called carbonfootprint have the pure virtual and should have the formula, but i didn't find it. having a little bit hard understanding some requests. like,

• Write an abstract class CarbonFootprint with only a pure virtual getCarbonFootprint method. Have each of your classes inherit from that abstract class and implement the getCarbonFootprint method to calculate an appropriate carbon footprint for that class.

• The main() function in the given program creates objects of each of the three classes, places pointers to those objects in a vector of CarbonFootprint pointers. You need to iterate through the vector, polymorphically invoking each object’s getCarbonFootprint method.

// Test program for CarbonFootprint and implementing classes.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector< CarbonFootprint* > list;
// add elements to list

[Code] ....

View 4 Replies View Related

C++ :: Including Outside Functions Into Classes?

Mar 6, 2015

I have a hpp file with a list of inline finctions like this:

Code:
inline int check() {
return 1;
}
inline int check_1() {
return 1;
}

... What I would like to do is to include them into several unrelated classes. How can I do this. Can I just add the hpp inline functions in headers of my class containing files or not. I mean if they are not defined as class functions how can they be called. I don't understan the logic.

View 2 Replies View Related

C++ :: Friend Functions And Classes Not Permitted To Data

Dec 29, 2013

I have a situation: I have a class character

class Character;
class Village;
class Character {
public:
//Functions
void charGen(); //Character creation

[Code] .....

According to the Friendship and Inheritance tutorial [URL] ...., that code should work, but it doesn't. I am given an error: undefined reference to questsCompleted

The compiler I am using is Code::Blocks ....

View 4 Replies View Related

C/C++ :: Access Derived Classes Functions On A Vector

Jan 19, 2014

I need to access the functions of the derived classes from a vector of objects of base classes (can't believe I wrote it). Here a Diagram for you to understand:

So as you see, I need the function Use() from the Usable class, to be able to be called from the vector like:

inventory.at(x)->Use()

View 14 Replies View Related

C++ :: Linked List - Friendship Of Functions Related To Classes

Jul 5, 2013

I'm developing a stack using a linked list. I have to develop two friend functions to do something with the list. But I've tried to understand the friendship, without any consistent result.

Extract of code:

Code:
class Element {
public:
int Data;
Element *nextElement;
Element(int Element) {Data = Element; nextElement = NULL;}
~Element() { }

[Code] ....

I have no errors, but I cannot access the private properties of the class.

The thing I would like to do is: sum the elements of the stack with a friend function.

View 7 Replies View Related

C++ :: Size Of Derived Class With Overriding Virtual Functions From Base Class?

Jan 21, 2014

The compiler creates virtual table for the base class and also for the derived class whether we override it or not.

That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1

The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.

But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.

#include<iostream>
class A{
public:

[Code].....

View 1 Replies View Related

C/C++ :: Print Single Linked List Backward With Functions / Classes - Error Message Regarding Header Files

Oct 11, 2014

I'm trying to print a single linked list backward with functions/classes. I have created the main file and 3 header files. But I'm getting an error on one of the header files, linkedListIterator after adding #include "linkedListType.h". It says that "linkedLlistType.h" is calling itself. And when I try to run it, I get an error of "too many header files." I have tried changing the headers many times, but nothing seems to work.

.cpp file:

/*(Printing a single linked list backward) Include the functions reversePrint and recursiveReversePrint, as discussed in this chapter, in the class linkedListType. Also, write a program function to print a (single) linked list backward. (Use either the class unorderedLinkedList or the class orderedLinkedList to test your function.)*/

#include "linkedListType.h"
#include "orderedLinkedList.h"
#include "linkedListIterator.h"
#include <iostream>
using namespace std;
struct nodeType

[Code] ....

header files:
[URL] .... (error in this header file)
[URL] ....

View 9 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++ :: 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++ :: 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++ :: Using Typedefs To Abstract Multiple Implementations

Jan 12, 2014

I am writing a program that aims to be cross-platform, so it supports multiple low-level implementations for certain operations. My initial approach is to write an abstract class to use as an interface, and inherit from that when writing specific implementations.

However, I realized that the specific implementation that I will be using is known at compile-time, and I to not need any polymorphic behaviour. I realized that I could just write this:

Code:
class Impl1 {};
#ifdef USING_IMPL1
typedef Impl1 Interf;
#endif

Clients would then go on using the Interf type, regardless of the underlying implementation. The only drawback that I can see is that my Implementation classes are not forced to conform to an interface, but the compiler will tell me anyway if I try to use, in my client code, a method that is not defined.

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







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