C++ :: Typical Implementation Of Multiple Inheritance
Jun 20, 2013
Here is the code:
class Foo {
public:
Foo(){cout<<"Foo"<<endl;}
};
class Bar : public virtual Foo {
[Code] ....
This is a typical implementation of multiple inheritance. If define class FooTooBar as,
Code:
class FooTooBar : public virtual FooToo, public virtual Bar {
public:
FooTooBar(){cout<<"FooTooBar"<<endl;}
};
I know why we use virtual inheritance in the class Bar and class FooToo. But if we use virtual inheritance in the class FooTooBar, does it make any sense?
Write a program using inheritance allow user to enter grades of his students 5~8 students as a base class and compute the sums for each students in derived class and compute the average of sums in another derived class. I created 3 classes in 1 header file and 1 cpp file how ever i cant seem to get the sum or the average to show up on execution time
I've been working on a program that uses a reference counting class that I've written which works fine for objects that inherit from it however I now need to have the following setup:
class SBComponent : private Refcounted { /*stuff*/} class ProxiedComponent : public SBComponent, private Refcounted {/*stuff*/}
The compiler gives the following warnings
warning: direct base ‘Refcounted’ inaccessible in ‘ProxiedComponent’ due to ambiguity
And then several repeats of the error:
error: request for member ‘bk’ is ambiguous Back *b = bk<ProxiedComponent>();
bk is a templated function that's defined in Refcounted and returns a pointer of type <template arg>::Back (ProxiedComponent::Back in this case).
I don't understand why the call to bk is ambiguous as although there's two instances of Refcounted (there needs to be with the way I've designed it so I can't make it virtual) it's inheritance is private in both cases so there should only be one instance of it visible in ProxiedComponent.
I have attached my code below and I am stuck in what to do next to make an instance of the dateCls so I can use the instance to assign the open date. By instance I mean like create an instance of the class, like this: dateCls myFirstInstance; And everything in the dateCls I can access through the . operator. So far my code looks like this..what I should do? Lastly, I am using derived data from I think the bankAccountCls.
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.
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.
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?
#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?
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 ?
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?
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.
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 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.
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];
Code: #ifndef DUCK_H_ #define DUCK_H_ #include<iostream> using namespace std; class Duck
[Code].....
When I compile, it says: "error: 'virtual void Duck::display()' is protected" how come I can't gain access to MallardDuck's display(), which is public?
I am having some serious issues with class inheritance. I am trying to make a MoneyBag class inherit from a class called bag. This will not work. I get an error complaining: error: expected class-name before '{' token. And yes I have googleing it and tried several of the various solutions offered with no avail.
The MoneyBag is pretty simple right now as I wanted to get it connected to bag before I tried to do anything with it.
//MoneyBag.h//
#ifndef MONEYBAG_H #define MONEYBAG_H #include <bag.h> class MoneyBag : public bag{ ////<<------ Error appears on this line.
[Code] ....
So based on everything I have seen on line the statement: class MoneyBag : public bag{ is legal. As it is done this way on this very site's tutorial: class Rectangle: public Shape, public PaintCost{
#include <list> #include <iostream> using namespace std;
[Code]....
I added an object of class myderiv to the container, but when I retrieve it back and try to manipulate it, it's actually of class mybase. Is there any way to keep the inheritance information in the container?
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):
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)
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?
I have the following classes and 'dreaded diamond':
A / / B C / / D | | E
Classes B & C both inherit from A using public virtual A.
E is the only concrete class. None of the classes are totally abstract.
Every class has a copy constructor.
All of the copy constructors are chained together through the initialization lists.
E correctly calls D's copy constructor.
D correctly calls B and C's copy constructors.
But neither B nor C call A's copy constructor, although A's default constructor is called. To reiterate B and C have a call to A's copy constructor in their initialization lists.
I guess A's default constructor is being called is because of virtual inheritence, but why isn't its copy constructor called (too)?
A's copy constructor includes some very important code and I could do with calling it. Should I call it from the concrete class' initialization list or is that considered bad form?
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?
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;
I have a class which dynamically allocates memory for three data arrays, and as such in the destructor I told it to delete those data arrays.
However, when I've created a new class, and inherited the previous class - it will always crash AFTER running the program, unless I don't have the previous destructor present.
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.