C++ ::  Inheritance Using Base Method With Other Name?

Feb 18, 2015

I have 2 classes with a Function with the same definition (both inherited from the same base class) and in my derived class I derive from both of those 2. Is it possible to use the Methods of both classes? for example with an other name?

class A {
protected:
int print(int a) { std::cout << "int A: " << a << std::endl; }
};
class B : A

[Code] ....

is there something like using C::print as printc;?

The Problem, I have a Sprite class that derives from a Rectangle with properties Position, Size, Origin and Angle and a Text class that derives from Rectangle. Now i have a Button class deriving from both Sprite and Text.

- The Position, when moving the Button i have to change the position of both so i Implemented a new Method which calls SetPosition from the Sprite and the Text.
- The SetSize just affects the Button so i just did using Sprite::SetSize;
- The angle affects both so i just implemented a new Method and hide the other two

The problem is here:
- The Origin: writing button.SetOrigin(-1,0) should set the Origin of the Button and writing button.SetTextOrigin should set the Origin of the text.

Should i just reimplement a Mehtod named SetTextOrigin and call Text::SetOrigin from there and hide the button.Text::SetOrigin or is there something like using Text::SetOrigin as SetTextOrigin?

View 6 Replies


ADVERTISEMENT

C# :: Inheritance Or Base Class / Polymorphism?

Sep 16, 2014

I have couple of objects which are using some amount of methods. Right now my application is not OOP and i am going to transfer it to OOP. So i would create each class for my each object. Almost all methods are in use for each object, the only one thing which is changing some of those objects passing not all parameters inside specific method. I can go two ways one is prepare interface for all methods i got and each of my classes could implement its own definition for it but other way almost all would implement exactly the same methods but with different parameters so maybe its better to create base class then for each object do inheritance from base class (polymorphism). inside base class i can prepare base methods and classes which will inherit from that class would override those methods for requirements they want.

So imagine: You got:

Memory
CPU
Latency

and all of them using mostly all of those same methods (only arguments for one of them could be use different way):

ExecuteQuery()
ExportToExcel()
PopulateDataTable()
PutValueToReport()

Now its better to do like this e.g:

Base class: Stuff
prop: name, id, date ...
methods to ovveride: ExecuteQuery(), ExportToExcel() ...

classes: CPU, Memory, Latency (inheriting from Stuff)
ovveride methods and align its definition for specific class use (remember mostly only passing args are used or not by specific class)

or go with inheritance

Interface SomeMethods
ExecuteQuery()
ExportToExcel()
PopulateDataTable()
PutValueToReport()

and each class would implemet its own definition.

The most important thing is that those every objects mostly using all of those methods and what is diffrence that one object can use all of available parameters inside this method and other one no. What i should do? Go with interface or inheritance and polymporfizm inside base class?

View 2 Replies View Related

C++ :: Inheritance - Class Base Has No Member Function

Jan 16, 2014

I have a simple question about inheritance. Consider the following code:

Code:
Class Base {
int type;
Base(){};
};
Class Derived1 : public Base

[Code] ....

I get the following error: Class "Base" has no member "Function1";

That makes sense - as Base has not declared Function1. But how can I loop through a vector of Bases, and then if the object is of type Derived1, call the function Function1?

View 11 Replies View Related

C++ :: Inheritance From Abstract Base Class For Decision Tree

Feb 27, 2014

I'm trying to implement a decision tree that gets doubles as input (in this sample code just random numbers) and returns a boolean value. At the nodes, I'd like to use various operators that can have different input and return types. I've implemented an abstract base class for the nodes and I'm deriving the specific nodes from it. In the code below, I've included only a few derived classes. Building the tree (randomly) is no problem. However, I'm looking for a clever way to evaluate the tree. I think that uncommenting the lines in bold print would in principle do it. However, this is not possible because "value" is not a member of the base class. The type of "value" is different in the derived classes, so I cannot simply declare it in the base class.

"Node.h"
#pragma once
class NodeBase{
public:
NodeBase* Child_1;
NodeBase* Child_2;
virtual void evaluate() = 0;

[Code] ....

View 4 Replies View Related

C++ :: Inheritance - Using Base Class Constructor (LNK2019 Error)

Sep 16, 2014

I am trying to create a few subclasses which all use the base class constructor, according to my book this is all fine and dandy by using the "using Baseclass::Baseclass", this doesnt work for me.

class Monster {
public:
Monster(char[], char[], char[], int); //The constructor, and its implemented.
etc..
};

[Code] ....

Yet, i recieve this error:
error LNK2019: unresolved external symbol "public: __thiscall Human::Human(char * const,char * const,char * const,int)"

Why is this? Does doing it this way create const pointers somehow? The constructor works fine with Monster, i am using the same calls, just changed to create Human instead of Monster. All files are included where they should, monster and human are declared in the same header.

View 18 Replies View Related

C++ :: Pass Method Of Derived Class As Parameter To Another Method In Base Class?

Feb 2, 2015

I have a question similar to the one here: [URL] .....

The main difference is I would like to pass a method of derived class as a parameter to some other method in its template base class.

template <typename BaseType>
class Base {
public:
typedef void (Base::*Callback)(int A);

[Code] .....

The above is an example which does not compile. My compiler complains that the two BaseMethod() calls in DerivedMethod() are invalid uses of non-static member function.

Is this not possible to do, or is my syntax simply wrong? All I want is to be able to pass as an an argument to a method in the base class from the derived class some callback as a variable for the base class to invoke later.

View 2 Replies View Related

C++ :: How To Access Base Method Via Derived Pointer

Oct 1, 2013

I would like to access (use) a (virtual) method declared in a base class via a pointer to an object belonging to a derived class, which overrides the base method. Is it possible? Up to now I have not been successful. I am including a program with a more detailed description.

// declare a base class T0 with virtual function P, and a derived
// class T1 which overrides P; declare two ptrs, to T0 and T1,
// and generate corresponding objects; calling P via the two
// ptrs gives expected result; however I would like to take
// ptr to T1 object & execute the base function (declared
// in T0); is it possible? I tried all possible casts but nothing
// works; no matter how I cast the pt1 ptr, I always end up executing
// the overriding function (declared in T1)
// actual output (debian 7, gcc 4.7.2-5):

[code]....

View 5 Replies View Related

C++ :: Overriding Base Class Template Method

Jan 22, 2015

PHP Code:

class B {
public:
     template<class T>
     T foo(){}
};
class D1:public B {

[Code] ....

I have that code piece and would want it to work but it doesn't.

Error "Error2error C2993: 'double' : illegal type for non-type template parameter '__formal' ....

I have no choice and have to use double and float for template typename when specializing. I tried to wrap it up like this

PHP Code:

typedef struct DOUBLE {
     DOUBLE(double d){ val = d; }
     double val;
}; 

but it's of no use.

View 6 Replies View Related

C++ :: Calling Method Of Specialized Template Base Class In Subclass

Feb 10, 2013

class IFoo {
virtual void Bar() = 0;
};

class FooAbstract {
virtual void Bar() {}

[Code] .....

How to call the Bar() method from FooTemplate in FooDerived::Bar()?

View 5 Replies View Related

C++ :: Friendship From Derived Class Method To Base Class Members

Jul 15, 2014

I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:

class Derived;
class Base {
int i, j;
friend void Derived::f();
protected:
Base();

[Code] ......

View 3 Replies View Related

C/C++ :: Adding Base-N Through Strings (between Base-2 And Base-36)

Feb 27, 2015

I just wanted to add strings in any base form (example 1101+100 = 10001 in base-2 but it could be added using any base-form like in base-3 to base-36) and I'm having a big trouble with my code because it gave me incorrect results.

addition(char st[], char st2[], int base){
int i, j, carry = 0, ans, len, o=0, z=1, l=0;
char final[50];
if(strlen(st)>=strlen(st2))
len = strlen(st);
else
len = strlen(st2);

[Code] ....

View 1 Replies View Related

C++ :: Convert Base 10 To Base X Number Within String Of Characters

Apr 15, 2013

How would you convert say "238273615237287352536266362524382737272" base 10 to a base x number contained within a string of characters?

View 2 Replies View Related

C++ :: Undefined Reference To (method Name) When Accessing Method In Static Library

Jan 17, 2013

I've been trying for more than one month to access a method found in a library called libcocosnas_static.a. I'm using Cocos2d-X version 2.0.4. The library has been used many times by my company to make games using cocos2d-1.0.1-x-0.12.0 without any problem.

This is what I've done:
1- I added the include paths of the library to both eclipse and Android.mk
2- Included the .h file using #include "NASPlatformUtil.h"
3- Added the libcocosnas_static.a file to the proj.androidobjlocalarmeabi folder
4- Added "LOCAL_WHOLE_STATIC_LIBRARIES += cocosnas_static" to the Android.mk file
5- Called the function using: NASPlatformUtil:: openUrl("http://xxx.xxx.com/");

I can right click on the function, click Open Declaration and get it without any problem, but the compiler keeps on giving me that dreaded error...

View 3 Replies View Related

C :: Algorithm To Convert From Any Base To Base 10 Decimal

Mar 25, 2013

I got this algorithm of conversion and now I'm stuck at how to code it.

"Algorithm to Convert From any Base to Base 10 Decimal."

Let 'n' be the number of digits in the number. For example, 104 has 3 digits, so 'n'=3.
Let 'b' be the base of the number. For example, 104 is decimal so 'b' = 10.
Let 's' be a running total, initially 0.

For each digit in the number, working left to right do:

Subtract 1 from 'n'.
Multiply the digit times b^n and add it to 's'.

When done with all the digits in the number, the decimal value should be 's' .

View 6 Replies View Related

C :: Need To Convert Base 10 Integer To Base 16

Mar 20, 2014

I need to convert an integer, for example 10, to its base 16 equivalent, 16. I found this code that converts a base 16 number to base 10, but I need the opposite. Plus, this code doesn't seem to work properly with input values under 32.

Code:

uint32_t input = 16;
uint8_t temp;
temp = ((input>>8)*100)|((input>>4)*10)|(input&0x0f);

View 13 Replies View Related

C++ :: Converting From Base 10 To Base 4

May 7, 2013

i am converting a base 10 number to base 4.

View 1 Replies View Related

C++ :: Converting Base N To Base M

Feb 8, 2013

I was wondering if there is a standard for converting something like this:

HELLO 27 to ?3

View 5 Replies View Related

C++ :: Inheritance Slows Down Process

Sep 5, 2013

i have read that inheritance slows down the process.because a_class -> b_class -> c_class it goes thorough many classes to find a function or a value. people advices not to do it more than a few times. what if instead of inheritance we are using headers, would it slow down the process also??i mean which one is faster? and arent both the same when we use headers or used inheritance.

View 3 Replies View Related

C++ :: Object Inheritance And Encapsulation

Jan 17, 2014

I want to define a base class A which allows different type of logging (file stream, console, etc). In the meantime, I want a class B to to be able to call A's print function (i.e. derived class C's print function which defines the logging method). However, I don't want to make A's print function to be public because it will only be called by B.

class A {
protected:
virtual void print(void);
};

[Code] .....

View 1 Replies View Related

C++ :: Virtual Keyword In Inheritance?

Feb 19, 2014

How to put virtual keyword in the function of the base class. Will the function still be over-written properly? What will happen if I

(1) call function from base class pointer
(2) call function from derived class pointer
(3) call function from derived class object

If the virtual keyword is present, the over-written version will be called in all cases, am I mistaken?

View 1 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++ :: Array Of Objects And Inheritance

Dec 20, 2013

I have Class A as a base class , and Class B , C derived classes from A and there's class D who have a data member (pointer to Array) of type A (Composition)

class D{
A **a;
int size;
.......
a = new A*[size];
......
};

And i have Print method , in its body i have to specific element (if it from class B or C ) with a given ID(both B and C have a data member ID ) there should be 2 options in print function .. printing elements for class B , or printing elements for class C ? how can i specific the elements ?

View 5 Replies View Related

C# :: How To Use Encapsulation / Polymorphism And Inheritance In WPF

Oct 20, 2014

I have been reading up about object oriented programming recently and have come across 'Encapsulation, Polymorphism and Inheritance' as i understand it so far all OOP programs should use these three concepts. So i started thinking how do i get these concepts into my program as i am using WPF C# and i could not really find much good info about how these concepts apply to WPF programs.

Or do these concepts just not work with WPF programs?

View 5 Replies View Related

C# :: Inheritance In WinForm Design

Feb 23, 2014

I have two Form Employee and Instructor, here Employee is super class of instructor.

public class Instructor: Employee
{
//Instructor class
}

I am able to access all property of Employee inside instructor class but with these some textbox and button design inside Employee design is also getting inherited inside Instructor design and i don want this and i want to maintain the parent-child relationship between Employee and Instructor.

View 11 Replies View Related

C++ :: Self-Deleting Objects With Inheritance

May 27, 2013

I'm writing a bunch of classes like Form, Button, etc. All these are derived from a base Object class. I want to make these objects auto-delete, so I only have to declare the new keyword, and the program will clean them when necessary. I understand the implications of dereferencing, I just have a couple of questions about my code.

I through this run-down example together:

Code:
#include <Windows.h>
#include <map>
class Object {
public:
Object() {};
~Object() {};
void *operator new(size_t size);
void operator delete(void *ptr);

[Code] .....

I have added a static variable of std::map type called m_Dynamic. I have overloaded the new and delete keywords (within the Object class) to keep the m_Dynamic map up-to-date. This prevents objects created on the stack from being deleted.

The object itself can then be deleted via the base Dispose() method. Or methods such as Destroy() can be added in derived classes and call upon Object::Dispose() themselves. This can be overloaded, etc, but eventually will be removed from the public view. I only have it here for testing.

From what I can tell everything "works", though I'm uncertain that the object is being correctly deleted.

Though, my main concern is that when a derived class such as Circle calls Dispose() which in turns fires delete this. Does it free() the sizeof(Object) or does it correctly free() the sizeof(Circle)?

Are there any other things I should be vary of? I only just started playing with new/delete overloading yesterday.

View 14 Replies View Related

C++ :: Array Of Class With Inheritance?

Jun 6, 2012

Shape base class, line and Point derived classes. What should I declare in .h files and implement in .cpp files that this is array will be work.

My major concern refer to operator [] and assign (=) operator. As far as I understand I should overload ([]) and (=) three times for classes shape , line and point or not... or is it possible made through virtual function? How will be code looks like ?

Code:
// part of main.cpp
Shape* shapes[3]; // Array of pointers to Shape
shapes[0] = new Shape();
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
shapes[2] = new Point(11,44);
cout << "using ToString function" << endl;
for(int i=0; i < 3; i++)
cout << s[i]->ToString();
for(i=0; i < 3; i++)
delete s[i];

View 2 Replies View Related







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