Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ object oriented programming question.

C++ object oriented programming question.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++oop
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    trustno1
    wrote on last edited by
    #1

    i was studying c++ object oriented programming notes today and have some questions regarding to this problem.. any suggestion is welcome.. thank you! for this question, i'm supposed to find the implementation error and fix it without changing the interface. i read thru the codes and realized that the method B::Init() was trying to initialize the private variable, pNum, that belongs to class A(Class B's super class).. I was wondering is there anyway to access to class A's private variable from class B? and also have i missed any other implementation error? class A { public: A() { Init(); } virtual ~A() { Destroy(); } virtual void Init(); virtual void Destroy(); long Get() { return *pNum; } void Set(long num) { *pNum = num; } private: long *pNum; }; class B : public A { public: B() { Init(); } virtual ~B() { Destroy(); } virtual void Init(); virtual void Destroy(); long Get2() { return *pNum; } void Set2(long num) { *pNum = num; } private: long *pNum2; }; void A::Init() { pNum = new long; } void A::Destroy() { delete pNum; } void B::Init() { pNum = new long; pNum2 = new long; } void B::Destroy() { delete pNum2; }

    C 1 Reply Last reply
    0
    • T trustno1

      i was studying c++ object oriented programming notes today and have some questions regarding to this problem.. any suggestion is welcome.. thank you! for this question, i'm supposed to find the implementation error and fix it without changing the interface. i read thru the codes and realized that the method B::Init() was trying to initialize the private variable, pNum, that belongs to class A(Class B's super class).. I was wondering is there anyway to access to class A's private variable from class B? and also have i missed any other implementation error? class A { public: A() { Init(); } virtual ~A() { Destroy(); } virtual void Init(); virtual void Destroy(); long Get() { return *pNum; } void Set(long num) { *pNum = num; } private: long *pNum; }; class B : public A { public: B() { Init(); } virtual ~B() { Destroy(); } virtual void Init(); virtual void Destroy(); long Get2() { return *pNum; } void Set2(long num) { *pNum = num; } private: long *pNum2; }; void A::Init() { pNum = new long; } void A::Destroy() { delete pNum; } void B::Init() { pNum = new long; pNum2 = new long; } void B::Destroy() { delete pNum2; }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      B cannot see A's privates. If you made the variable protected, it would be visible to B. Otherwise you can make B a friend class of A, but in this case that would be kind of redundant, unless A has other classes derived from it which should be disallowed from seeing A's private stuff. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups