Derive function acess

Why cant it access derive class function isn't pointer pointer pointing at adreess of derived class and should access all it's functions it does access virtual function of derive class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  class Base
{
    
      public:
      virtual void show(){ cout<<" Base Class Show ….."<<endl;}
      void functionA() {cout<<" Base Class Function A…"<<endl;}  
   
}; 
    
class Derv :  public Base 
  {
       public:
       void show(){cout<<" Derv  Class Show….."<<endl;} 
     void functionB() {cout<<" Derv Class Function B…"<<endl;}  
   
    }; 
 void main()
{
   Derv dv;
   
   Base * ptr;	

   ptr = &dv;

   ptr->show();

   ptr->functionA();

   ptr->functionB();		
   getch();
	
}
 
closed account (j3Rz8vqX)
First off, prt is of type Base.

 
Base * ptr;

You assigned it the address of dv.
 
ptr = &dv;

Which is of type Derv.

But because ptr is of class type Base, it will only associate with the members it can access; scope narrowing scheme.

A is the base class
B is the derived class

B has an A object within it.

So when an A object points to a B object.
It will have a scope of type A, even though it is addressing a type B address.

Image, casting a double to int.
Or better yet, pointing an int pointer at a double address.

Hope this helps.

Edited:
Just to point out, some compilers will allow void main()
Mines does not, if you aren't getting any issue - no concerns.
If you are, use int main(). The "return 0;" is passively invoked even if you don't call it - hence why successfully completed programs return 0 upon completion.
Last edited on
Topic archived. No new replies allowed.