My question is while I'm reading this book step by step line by line, I reached this example. Building classes then derived classes. How can I add derived class from a class which is already existed. In other words, I have Person Class (which is my base class) and after implementing this class, I would like to create Student Class ( which is my derived class). How can I do that from using Solution Explorer window? I can add base class and derived class together by right click on my project and then add new item then adding class + base class I know that but what if my class in this case existed and I want only to add base class? I know how to add cpp. file and h. file but I want to take advantage of using the formal style if it exists in this case.
Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:
//function fight() fight(Player p1, Player p2) { p1.func2(); } //using function fight() fight(human, computer);
When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).
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> #include "curve1.h" #include "curve2.h" using namespace std; int main() { Curve1 curve1Obj; Curve2 curve2Obj;
[Code]...
Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.
My base class header score.h looks like this:
#ifndef SCORE_H #define SCORE_H class Score { private: int *ipScore; float fAverage; int iSize;
[Code]...
You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously). .....
The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....
How can I cause it to return the old values that are set in curve1.cpp?
Here is my curve2.cpp
#include <iostream> #include "curve2.h" using namespace std; void Curve2::curve() { cout << "getSize() returns: " << getSize() << endl; // out comes the garbage }
Can I use a function to simply put values from old to new variables? If yes then how?
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code: class B { public: B():_y(1){} virtual ~B() { cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
#include <iostream> using namespace std; class superclass; class subclass1; class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
So I have a base class, lets call it base. In base I have a virtual function called update(), update just couts "base" then I have a class derived from base called derived;
it has a function called update(), update just couts "derived" then I create a vector called Vec it's initialised like this:
std::vector<base> Vec;
then I add an element into it like this
Derived DerElement; Vec.push_back(DerElement);
then when I type:
for (int i=0; i<Vec.size(); i++) { Vec.at(i).Update(); }
It outputs:
Derived DerElement2; DerElement2.Update();
and it outputs this:
#include <iostream> #include <vector> class Base { public: virtual void Update() {
I have an example where I have a variable belonging to a base class, but I would like to tell the compiler that it actually belongs to a derived class. How can I do this?
// base class: R0 // derived class: R1 // see function SetR1 for the problem class R0 { public: int a;
I just wondering if a base class can call the overridden function from a Derived class?
Here's an example:
//Base Class H class BaseClass { public: BaseClass(); virtual ~BaseClass(); virtual void functionA();
[Code] ....
So basically, when I am creating a new object of Derived class, it will initialize BaseClass and the BaseClass will call functionA but I want it to call the function overridden by Derived class.
I know that if I call newObj->functionA it will call the overridden function. Right now I want the base class to call the overridden function "this->functionA(); in BaseClass" during its initialization. Is it possible to do that?
Basically, I have a base class called MainShop and it has 3 derived classes which are SwordShop, SpellBookShop and BowShop. I want the base class to be able to call a function from one of the derived classes but no matter what i do, it doesn't seem to work!
I have two other derived classes, but its basically the same concept. I have a function in one of the derived classes and i would like to call it from the base class. This is one my derived classes:
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.
I create an instance of a base class (not derived class) and assign it to base class pointer. Then, I convert it to a pointer to a derived class and call methods on it.
why does it work, if there is a virtual table?
when will it fail?
// TestCastWin.cpp : Defines the entry point for the console application.//
Constructor of the Base Class Person::Person(char* n="", char* nat="U.S.A", int s=1) { name = n; nationality = nat; sex = s; }
Constructor of the Derived Class (inherited from the base class)
Student(char* n, int s=0, char* i=""): Person(n, s)
Why the initialized list of the base class constructor doesn't match the initialized list of the derived class constructor? I know this book is a little bit old, I'm not sure if this wrong in VC++ 2010?
I'm trying to call a function on a derived class that's in a vector of it's base class. I've made the code really simple for illustration purposes:
class Sprite { virtual void update(); } class Enemy : public Sprite { virtual void update();
[Code] ....
I want to be able to just call update() on the items in the vector and the derived class update() functions be called. Currently, it always calls the Sprite update, which makes sense, but it's not what I want. Is there a way to call the derived update function without knowing the type of the derived class?
I have a big problem with searching a solution for getting access on getters and setters of the derived classes of an interface.
Interface:
class IfParam { public: IfParam(); };
Now I have a derived class for each needed datatype. E.g.
class Int32Param: public IfParam { public: Int32Param(); Int32Param(IfParam* l_param); int getValue(); void setValue(int l_value); private:
[Code]...
My Problem now ist getting access to the getters/setters over the interface. I wish I could initialize the Params like this:
IfParam* param = new Int32Param(); param.setValue(12);
IfParam* param = new StringParam(); param.setValue("String");
But to have access to the getter/setter I have to declaire the functions in the interface as generic functions. But how? I tried to use temlates, but then i have to declaire IfParam as IfParam<int>. Thats a problem because in my original program I do not know which TypeParam the IfParam interface will be initialized with when I create the pointer.
I don't understand why my compiler gives me this error when I'm trying to run this code:
Code:
#include <iostream> #include <cmath> using namespace std; class Airplane { public: Airplane(); ~Airplane();
[Code]...
The variable is protected. Yeah, that's right. But shouldn't a derived class be able to reach it? Or is it only in a function that the derived class is able to reach protected variables and isn't able to reach protected variables in the constructor?
Is is possible to force derived classed to overload certain operators, like we do with pure virtual functions?
Is this possible to dynamically bind objects to their respective overloaded operators?
I am getting errors with undefined references to my base class vtable when I hackly try to overload: Code: operator+ I am not sure whether this is possible.