C++ :: Pointer Usage To Base Class Object In Vector Int Main Function

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


ADVERTISEMENT

C++ :: Copy Derived Class Object Through A Pointer To Base

Jan 14, 2015

Is there a way to copy a derived class object thru a pointer to base?

For example:

class Base { public: Base( int x ) : x( x ) {}
private: int x; };
class Derived1 : public Base { public: Derived( int z, float f ) : Base( z ), f( f ) {}
private: float f;};
class Derived2 : public Base { public: Derived( int z, string f ) : Base( z ), f( f ) {}

[Code] ....

The question is whether *B[0] would be a Derived1 object and *B[1] a Derived2 object?If not, how could I copy a derived class thru a pointer to the base class?

View 1 Replies View Related

C++ :: Using Member Function Of A Class In Another Class And Relate It To Object In Int Main

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

C++ :: Calling Defined Function Pointer From Another Pointer To Class Object?

Aug 19, 2014

I am attempting to implement function pointers and I am having a bit of a problem.

See the code example below; what I want to be able to do is call a function pointer from another pointer.

I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.

class MainObject;
class SecondaryObject;
class SecondaryObject {
public:

[Code]....

View 10 Replies View Related

C++ :: Cast Base Class Pointer To Derived Class Pointer

Feb 6, 2015

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.//

#include "stdafx.h"
#include <iostream>
class B
{
public:
B(double x, double y) : x_(x), y_(y) {}
double x() const { return x_; }

[Code] ....

View 14 Replies View Related

C++ :: Transfer Values Set In Privates Of Base Class By Object Of One Derived Class To Another

Apr 26, 2014

I have my main.cpp like this:

#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?

View 3 Replies View Related

C++ :: Deleting Array Of Derived Class Objects Through Base Class Pointer

Mar 21, 2015

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?

View 1 Replies View Related

C++ :: A Pointer To Base Class

May 18, 2013

A pointer to base class, if assigned to a derived class, only points to the base part right? So you can only use the base part of the derived class with that pointer and no methods from the derived class?

View 8 Replies View Related

C++ :: Base Class Pointer Destructor

Aug 29, 2014

I have a query regarding virtual destructor functionality. So below is sample code:

Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Base

Code: Base *b = d;
Here b and d now pointing to same memory location.

Now below statement:
Code: delete (b);
Here since my destructor is virtual it will call derived class and base class destructor.

Now my question is, if I use this:
Code: delete (d); // And without virtual keyword in ~Base() {} This call both derived and base class destructor.

So which one is correct form to call and why? Is delete(b) is standard in virtual function mechanism.

Output is:

D1 :: function1()
Base :: function2()
INSIDE D1 DES
INSIDE BASE DES

View 2 Replies View Related

C++ :: Pimpl Class With Pointer To Base Class?

Jun 28, 2012

I'm trying to implement a class hierarchy and a wrapper class with a pointer to the base class. The base class has operator< overloaded and the implementation makes use of virtual functions as some of the logic for sorting is in the derived classes. Unfortunately, when trying to use the base class operator< from the wrapper, I get a "pure virtual method called".

Below code is meant to illustrate my problem. Unfortunately it crashes on me (upon destruction of vec) and I cannot quite see, why. So two questions:

1. spot the error I made in the code below (having lived in Java-land for the last 5 years, I'm sure I just did some stupid error)?

2. How can I implement Wrapper::operator< to use Base::operator<? I know I could write a function and pass it to sort but I'm interessted if there is a way to actually use Base::operator<.

Code:
#include <vector>
#include <algorithm>
#include <cmath>

[Code].....

View 3 Replies View Related

C++ :: Initializing Base Class Part From Derived Object Using Copy Constructor

Feb 20, 2012

class Base
{
char * ptr;
public:
Base(){}
Base(char * str)

[code].....

Obj1 is a derived class object where base class char pointer is initialized with "singh" and derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1.

Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in
both the classes are private.

I tried using initializer list but could not succeed.

Is there some proper way to do this?

View 4 Replies View Related

C++ :: Member Function In Derived Class Call Same Function From Its Base Class?

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

C++ :: Calling Derived Class Functions In A Function With Parameter Of Base Class

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

C++ :: Derived Class Not Overwriting Base Class Function - Using Vectors

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

C++ :: Can Base Class Call Overridden Function From Derived Class?

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

C++ :: Pointer Function - Getting Information Back To Main

Feb 6, 2014

I am trying to make a information to pointer function but the problem is how/what number am i putting in for *a...

And can i get that information back to main? and is that right way to split the array in to ? because i have will 200 random number and I need to find the smallest and second smallest element in array.

two_smallest (print, size3,size3,size3,size3);
} void two_smallest (int find_array[] , int size3, int *a, int *a2, int *b,int *b2) {
for (int index = 0; index <= 100; index ++) {
if (find_array[index] < find_array [0]) {
find_array[0] = find_array [index];
*a = find_array[0];

[Code] ....

View 1 Replies View Related

C/C++ :: Converting Int To Double Pointer Outside Main Function

Dec 22, 2014

I have int pointer called p and i want to calculate average.Since average involves using double or float so i am trying to convert this in the function averagetemp. It still gives me an error saying "cannot be converted"...

#include <iostream>
using namespace std;
int* createArray(int n);
int lowesttemp(int *p,int f);
int highesttemp(int *p,int f);
double averagetemp(int *k,double f);
void print(int *p,int lowest_temp,int highesttemp,int average_temp);

[Code] ....

View 8 Replies View Related

C/C++ :: Assigning Pointer (int) Located In Main With Another Function

Aug 2, 2014

I have the following:

int Allocate(int, int *);
main() {
int *Pointer;
int Elements = 25;
// this works just fine - as expected.
Pointer = (int *) malloc(Elements, sizeof(int));
// This DOES NOT - The value of Pointer never changes.....

[code]....

View 2 Replies View Related

C++ :: How To Call Function From Derived Class In Base Class

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

C/C++ :: How To Get Formation From Pointer Function To Main Function

Feb 13, 2014

int example (int [], int, *int,*int,*int,*int);
int main () {
My code will be here
example (int array[], int size, &a,&b,&c,&d); // Like this??? I try it didnt work

[Code] ....

View 2 Replies View Related

C :: File Opened In Main And Passing Pointer To A Function

Sep 14, 2013

I am getting a few compile errors for what might be a simple thing to do. I am opening a file in main, passing that pointer to a function and checking the contents of that file with a regex before I pass it on to build a BST. I am getting the following compile errors, what is wrong. Here are the errors:

Code:
gcc main.c fileCheck.c -o tree
main.c: In function `fileCheck':
main.c:19: error: syntax error before "FILE"
fileCheck.c: In function `fileCheck':

[Code] .....

Fatal error: Command failed for target `tree' Here is the two files and header that seem to be causing me the problems.

main.c

Code:
#include "main.h"
//#include "node.h"
int main(int argc, char *argv[])
FILE *fp;
if (argc > 2)

[Code] ....

And the header file.
main.h

Code:
#ifndef MAIN_H
#define MAIN_H
#ifdef __cplusplus
extern "C" {
#endif

[Code] .....

View 2 Replies View Related

C :: Correct Way To Call A Function Within Main That Has File Pointer Parameters?

Mar 16, 2013

What would be the correct way to call a function within main that has file pointer parameters?

function prototype: Code: void CalculateBoth(int num1, int num2, int*sumPtr, int *diffPtr);

View 2 Replies View Related

C++ :: How To Initialize Vector In A Class Object

Mar 6, 2013

I want to make a class, named generation, having a vector<chromosome> type data member.

chromosome is also class name.

chromosome has a 'route' as data member, which type is class TArrayI defined in ROOT package.

the size of route is to be fixed according to parameters of generation class object.

class chromosome{
public: chromosome(){;}
....
protected:
TArrayI route;
};
class generation{
public: generation(int PopSize=300, int numCity = 36){
.........
}
vector<chromosome> Population;
};

I want each element, chromosome of Population, to have a numCity size of route.

View 2 Replies View Related

C/C++ :: Function Within A Base Class

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

C++ :: Pointer Class Object Vectors

Aug 4, 2013

#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
struct bop {
string realname; //real name

[Code] ....

Okay, so first thing's first. The program will not compile due to lines 39-45. If I were to change those pointers into regular objects, it will not change the values of my class object. So what is the right way to do this?

I want the user to be able to input the # of employers/programmers into the system. But I cannot do that with an array of classes because when declaring an array; the array size must be constant.

View 6 Replies View Related

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 View Related







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