C++ :: Base Class Member Function
Apr 9, 2014
I have encountered following lines in base class and I do not comprehend its meaning of "= 0" at the end of the member functions;
distance_list intersect(ray & r) = 0;
appearance get_appearance(vector & pt) = 0;
where distance_list is a list of doubles and appearance is properties.
In general, what does this "equal sign and 0 " mean for the member functions in the base class?
View 3 Replies
ADVERTISEMENT
Sep 18, 2013
How can a member function in my derived class call the same function from its base class?
View 1 Replies
View Related
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
Jul 25, 2012
Let's take a look at the code first,
Code:
class B
{
public:
int data;
};
class D : public B
{
public:
int data;
};
Both B and D defines data. I wonder if there is any difference between them?
View 2 Replies
View Related
Sep 9, 2013
Basically I want to create a base class which defines a static data member so that its automatically redeclared as the same static data member in the derived class.
class A{
protected:
static derivable int val;
// A::val
}
class B : public A{
// static derivable int val is already here
// A::val AND B::val
}
This seems impossible to me but I'm wondering if perhaps there's a way to add modifiers to the compiler to do this (or preferably something MUCH simpler)...
View 11 Replies
View Related
Mar 30, 2013
Say I have 3 classes:
class Player {
public:
virtual func1();
[code]....
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).
View 6 Replies
View Related
Feb 4, 2014
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() {
[Code] .....
and this is it's output:
Base
Derived
Press any key to continue . . .
View 1 Replies
View Related
Aug 28, 2013
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?
View 9 Replies
View Related
Dec 24, 2013
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!
Here is my code:
#include "MainShop.h"
//BaseClass cpp
void MainShop::EnterShop(Hero& hero)
[Code]....
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:
//SwordShop derived cpp
#include "SwordShop.h"
void SwordShop::soldierShop(Hero& hero)
{
/* some code here*/
}
View 4 Replies
View Related
Mar 1, 2012
I am working on a program for school and I have managed to get everything to work, except the last portion of the program. It seems that the program is not calling the last function EmployeeSummary. The program will compile, and will accept data and compute overtime pay, etc. Just won't go on to the last function.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class CPayroll {
[Code] ...
View 3 Replies
View Related
Oct 26, 2012
Here are the classes:
BaseClass.h
Code:
class BaseClass {
public:
BaseClass();
virtual ~BaseClass();
virtual void printStuff() const;
[Code] ....
When I call printStuff, the DerivedClass's function gets called. Now, if I remove the const part from the DerivedClass's printStuff function, we call the BaseClass's printStuff function.
View 4 Replies
View Related
Aug 20, 2014
I'm playing with the idea of a singleton base class, but I'm having an issue with how to implement the GetInstance() function in the base class. Since I'm trying to make this ridiculously simple for the child, I'd like to handle that in the base.
class Singleton {
private:
static Singleton* instance;
Singleton() { Construct(); } // Private to avoid other instances
[Code] .....
It would be easy to use like so:
class Hello : public Singleton {
private:
std::string hello;
void Construct() { hello = "hello"; }
public:
std::string GetHello() const { return hello; }
};
Then the instance would be handled like so:
std::cout << Hello::GetInstance()->GetHello();
View 12 Replies
View Related
Aug 21, 2013
I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.
I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.
I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.
Lets have it as a sample code:
class Timer {
private:
int x;
public:
Timer();
get_X();
start_X();
[Code] ....
View 4 Replies
View Related
Aug 12, 2013
I know what are pointer's and how to use them but there is one point i am not able to understand. Below is the example code
I understand everything in the below code except 1 thing why i am using pointer to base class object in vector int the main() Function?
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// base class
[Code] ...
Here is the lines of code i want to understand.
Code:
vector<Employee*> employees;
employees.push_back(&emp1);
employees.push_back(&mgr1);
I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.
View 3 Replies
View Related
Oct 21, 2013
I mount a function (parameter - numeric vector; returns a string). However, this same function is used in several classes. To avoid that I keep duplicating the same code within these classes there is a way to do that as the code below?
std::string func( const vector<int> vec ) {
//processamento
return result;
} class A {
[Code] ....
View 6 Replies
View Related
Jun 16, 2013
whether i can define a member function inside a class or not in C++. Is the following code is legal?
#include<iostream> using namespace std;
class adder {
private:
int a;
int b;
int c;
int answer;
public:
[code]....
View 6 Replies
View Related
Apr 15, 2014
I want to have a template function that is a member of a class. Is this possible? This code snippet is how I would think the syntax would go, although it doesn't compile. How would I achieve the same effect?
Code:
class myclass {
public:
int member ;
} ;
template <typename T> void myclass::func( T& arg )
[Code] .....
View 4 Replies
View Related
May 19, 2012
I keep getting an error saying ui.h:30: error: 'class BTree<Word>' has no member named 'prntInOrder'
I have no line 30 in my ui.h but if i count the lines from the .cpp as if they were attached to the .h i find the call to the BTree printInOrder()
here is my ui.h
Code:
#pragma once
#include "btree.h"
#include <fstream>
#include <iostream>
using namespace std;
[Code].....
As you can see the printInOrder() function is there so would it not see it?
Error:
Code:
ui.h: In member function 'void UI::go(std::string)':
ui.h:30: error: 'class BTree<Word>' has no member named 'printInOrder'
View 6 Replies
View Related
Feb 10, 2015
I have a class I am building called date and I've built all my functions to run and done all the necessary error checking in each function. However, my last and final function I have to write is where the # of days passed in as a parameter and then I have to increment the days by that many. And if the user does not increment any days at all in the parameter and leaves it blank, then it automatically increments by one day. I am having trouble with this. So for example if the user was to do this:
Date d1(10, 31, 1998); // Oct 31, 1998
Date d2(6, 29, 1950);// June 29, 1950
d1.Increment(); // d1 is now Nov 1, 1998
d2.Increment(5);// d2 is now July 4, 1950
The function starts out looking like this
void Date::Increment(int numDays = 1) {
}
I know I have to use a for loop to accomplish this. I just don't know how to get it to where the days passed in will go to the next month and then days passed in would go to the next year.
View 2 Replies
View Related
Aug 19, 2014
I have the following problem: I am using NLOpt for optimization. The API provides functions to set the objective. This is done as follows:
double objective(const vector<double> &x, vector<double> &grad, void *data)
{
return x[1]*x[0];
}
int main(){
nlopt::opt opti(nlopt::LD_MMA,2);
opti.set_min_objective(objective,NULL);
vector<double> x(2);
[Code]....
Now I want to make the function objective a member of a class:
class Foo {
public:
double objective(...){..}
};
How can I give this method to opti.optimize? If I make objective static I can use
opti.optimize(Foo::objective,NULL);
but I do not want to have a static member. Is it possible to create an object of type Foo and give it to opti.optimize?
View 1 Replies
View Related
Mar 26, 2014
Some background: I have a class, A, with members, B and C and D; I also have an array of A objects; I want to be able to have a function which takes said array and performs a certain calculation on either the B, C, or D members of each of the A objects, depending upon certain circumstances; I want to perform the same calculation regardless of which member is to be used in said calculation, such as always assigning the value 3 or multiplying the member's value by a cofactor of some sort.
My question, therefore, is: how I might do this using only one function be it a template or not?
View 1 Replies
View Related
Apr 13, 2012
In C++, how do i call a method member of class A from a class B, using a pointer. By the way Class A and B are of different types.
I read that when a pointer is pointing to member function it can only point member functions within the class. But how can i point to a member function outside the class.?????
for example
class A {
public:
int add(int x) {
return x+x;
[Code] .....
View 1 Replies
View Related
Oct 2, 2012
Usually we use the following statements to export a class or member functions,
Code:
#ifdef DLLMICRO
#define DLLIO __declspec(dllexport)
#else
#define DLLIO __declspec(dllimport)
#endif
I understand that the files using the exported class or function need to call this class or function with dllimport and the file containing the exported class or function needs to call this class or function with dllexport. But I tried to use __declspec(dllexport) only instead of the statements above. It still works. Is there anything I am missing?Why'd we have to switch between dllexport and dllimport?
View 12 Replies
View Related
Jan 6, 2013
I'm currently programming a server which uses multiple threads- I have a class for one map in the game. Each map has a thread for timed events(tile regeneration, NPC regeneration, etc.), and a thread for handling NPCs(movement, combat, etc.). A basic structure of the class looks like this:
class Region {
public:
/* game values are here, they are public so
they can be accessed from outside of the class
inside of packet-handling functions and such */
int value;
void *Function();
[Code] ....
The program crashes when I use a member of the same class the function is located in- in the context I have shown about it would crash on "value++".
View 11 Replies
View Related
Apr 30, 2012
When I do this:
// header file:
#include <list>
using namespace std;
class myClass {
list<int> myMethod();
};
// cpp file:
list<int> myClass::myMethod() {
}
In the cpp file, 'myMethod' is underlined in red and when I hover over it, it says:
"std::list<int, std::allocator<int>> myClass::myMethod()
Error: declaration is incompatible with "<error-type> myClass::myMethod()""
But when I make it as a standalone function, outside a class, with no pre-declaration, there is no problem.
View 8 Replies
View Related
Aug 17, 2013
I am supposed to implement the member functions of class Person.
class Person {
public:
Person();
Person(string pname, int page);
void get_name() const;
void get_age() const;
[Code] ....
The code I wrote is below. Where I am struggling is the program does not allow me to input age. Therefore, I cannot test if my temp for age works. It automatically defaults to 0 because it hasn't taken input. Here is my code:
// Program Title: Person function
// Program Description: The program prompts the user for first and last name and age.
// It then prints the output that was provided by the user.
#include<iostream>
#include<string>
using namespace std;
class Person {
[Code] .....
View 13 Replies
View Related