C++ :: Instance Of Class A Vs Class B Derived From Class A

Jul 3, 2014

There are two ways to access the members of class A inside class B:

1) Making an instance of class A in class B
2) Deriving class B from class A

So what is the basic difference in both ways as we can do same kind of work with both ways?

View 1 Replies


ADVERTISEMENT

Visual C++ :: Creating Instance Of MFC Derived Class

May 23, 2013

I need to create an object of a mfc derived CFormView class that's not in the doc/template (a second view class). But it was generated with a protected ctor. Here's the code explanation with comments.

I'm thinking all the normal classes of the Doc/View template are created starting with this code, but within the template code base.

Code:

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,
RUNTIME_CLASS(CViewSwitchDoc), //<-expands to-> ((CRuntimeClass*)(&CViewSwitchDoc::classCViewSwitchDoc)),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CViewSwitchView));

But I have generated "another view" using the "Add Class" Wizard, it's a derived class of mfc CFormView which I named ViewForm. However I'm having a problem creating an instance of it because of the generated protected ctor and pulls a compile error of not being able to access ctor. Below are the header and implementation files of this said ViewForm class. How to create an object of this view ? Did I go about it all the wrong way since it's not in the doc/template group ?

// ViewForm.h file
#pragma once
// ViewForm form view
class ViewForm : public CFormView {
DECLARE_DYNCREATE(ViewForm)

[Code] ....

View 5 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++ :: Size Of Derived Class With Overriding Virtual Functions From Base Class?

Jan 21, 2014

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>
class A{
public:

[Code].....

View 1 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++ :: Creating Array Of Pointers To Base Class To Point To Derived Class Objects Dynamically

Jan 16, 2013

Please consider the following code :

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

View 2 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++ :: 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++ :: Variable Belonging To Base Class - Tell Compiler Consider This To Be Derived Class?

Oct 12, 2013

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;

[Code] .....

View 5 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++ :: Class Function That Uses Instance Of Its Child Class As Argument

Mar 1, 2013

I am facing a real-life problem, it can be simplified as below:

#include <iostream>
using namespace std;
class B;
class A {
public:
void f1(A a) {}
void f2(B b) {}

[Code]...

There is no problem at all with the f1(), it compiles and executes without any problem. But f2() gives compilation error. How to solve this?

The error message is: error: 'b' has incomplete type This is just to define the function f2() in a class, that uses an instance of its child class as one of its arguments.

View 11 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++ :: Exchange Array Between Derived Class And Base Class?

Apr 8, 2014

Base class has an array, whose size is controlled by the derived class.

I can't use the STL and use a 2003 compiler, so things like std::vector and std::array are out. I also can't use dynamic memory allocation.

So I thought of a few options:

1.
template <int N> class myBaseClass { ... int array[N]; ... }

then class MyClass: public myBaseClass<8> ... etc ...

2.
have a int **array in the base and assign in the derived class.

3.
give the base some virtual methods such as int *getArray or even int &getInt for more safety.

View 6 Replies View Related

C++ :: Invoking Base Class Constructor From Derived Class?

May 15, 2013

I understand it is done like this

// Calling the base class constructor
explicit CCandyBox(double lv, double wv, double hv, const char* str="Candy"): CBox(lv, wv, hv)
{
...
}

But how does the compiler know that you are initializing the base "part" of the object?

Or is this the entire reason initialization lists exist, to separate this confusion?

View 4 Replies View Related

C++ :: Calling Base Class Constructors From Derived Class

Mar 30, 2013

I'm having some difficulties in understanding the topic which I stated above.

View 5 Replies View Related

C++ :: Derived Class Inherit Operator Of Base Class

Jan 17, 2012

If Yes, then why this syntax does not works :

class Derived : public Base {
public:
Derived& operator=(const Derived &rhs) {
operator =(static_cast<const Base&>(rhs));

[Code] ....

View 2 Replies View Related

C++ :: Building Derived Class On Existing Class

Jan 1, 2013

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.

View 4 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++ :: 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++ :: 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++ :: Derived Class Constructor Using Base Class Constructor?

Jan 1, 2013

Is this example correct? This example from a book

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?

View 5 Replies View Related

C++ :: Why Would A Class Allocate An Instance Of Itself

Oct 4, 2014

I'm currently learning the Qt framework and doing my first tutorial. Straight away I saw something that baffled me:

notepad.h

Code: namespace Ui {
class Notepad;
}
class Notepad : public QMainWindow

[Code] ....

Note the ui pointer and the heap allocation in the class constructor; I can't wrap my head around why one would do this. What's going on here?

View 5 Replies View Related

C++ :: Declaring New Instance Of A Class

Nov 6, 2014

I am putting a instance o the Vehicle Class inside the constructor of the Calculate Class then calling it later. I get a warning saying the variable is not used and a error when I try to used the functions from the vehicle class saying use of undeclared identifier.

Code:
#include <iostream>
#include "Calculate.h"
#include "Vehicle.h"
#include <fstream>
Calculate::Calculate(){

[Code] ....

View 9 Replies View Related

C++ :: Vector Is Different For Every Class Instance

Feb 12, 2014

Okay so I have a class Student, which takes a number and a vector as a parameter for the constructor. Everything works well, until I output the values of the vector for every instance. The problem is that the same vector is being shared with EVERY instance I create, but I want it to be unique for every single one!

//Student.h
#ifndef __Grade_calculator__Student__
#define __Grade_calculator__Student__
#include <iostream>

[Code].....

View 1 Replies View Related

C++ ::  How To Have A Class Return Instance Of Itself

Nov 24, 2012

I was wondering if (in C++) you can instantiate a class (class foo) then have said class return the already instantiated object. (foo::instance())

In other words, can I have a class return it's-self via it's own methods? I want to be able to create a class (i.e. class foo) early in my program so it is already setup and ready to go. Then, farther down the line, I want to be able to call functions from that class without having to pass that object as an argument to my calling function. Can I do something like so:

MyClass::ReturnSelf()->foo();
or
MyClass::ReturnSelf().foo();

View 13 Replies View Related







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