inheritance - c++ create an instance of the class as a reference type -


create bond class , munibond class inherits bond. create simple program , create instance of munibond class reference type.

don't know how create instance of munibond class reference type.

bond.h

#pragma once  class bond  { private:     int myvalue; public:     int a; }; 

bond.cpp

#pragma once #include "bond.h"   class munibond : public bond  { private:     int myvalue;  public:     munibond(int);     ~munibond(void);     void set_value(int);     int get_value(); }; 

main.cpp

#include "stdafx.h" #include "bond.h" #include "munibond.h" #include <iostream> using namespace std;  int _main() {      munibond *m_obj = new munibond (5);       cout << m_obj->get_value()<<endl;     delete m_obj;      return 0; } 

"reference type" strange choice of word teacher. has specific meaning, isn't quite related question asking (using usual meanings of word, can't create "instance" reference type... have create "variable" reference type). should review class notes determine teacher means.

if had venture guess, guess want create instance of munibond, , create variable type "reference type" refers it. reference types notated using & symbol, int& "reference int"

if correct interpretation of teacher's word choice, there! created instance of munibond already, , you've stored pointer (m_obj). need create variable "reference munibond," or munibond& , have refer object m_obj pointing at. easy "dereferencing" m_obj using *. dereferencing c++ operator takes pointer instance object (which have), , produces reference object (which want). given have pointers in code, you've used * operator already. if not, recommend reading on it, because powerful, , i'm not focusing on right now.

the final line need (assuming translate teacher's wording properly), along lines of

munibond& m_reftoobj = *m_obj; 

now m_reftoobj reference same instance m_obj points at.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -