how to refer to base objects created during inheritance??

This is a discussion on how to refer to base objects created during inheritance?? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; class base{ protected: int a; public: base(){ cout<<"\nconstructing default base object";}; base(int x){ a=x; ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    how to refer to base objects created during inheritance??

    Code:
    #include <iostream>
    using namespace std;
    
    
    class base{
    protected:
    	int a;
    public:
    	base(){ cout<<"\nconstructing default base object";};
    	base(int x){ a=x;	cout<<"\nconstructing base object"; };
    };
    
    class inherit:public base{
    private:
    	int c;
    public:
    	inherit(){ cout<<"\n constructing default inherited object"; }
    	inherit(int x,int y):base(x),c(y){ 		cout<<"\n constructing inherited object";
    	}
    };
    
    int main(void){
    	inherit obj(1,2);	
    }
    the above code has a base class and a derieved class. when obj is created,a base object is first created,followed by the inherited object.does any one has idea how to refer to / or make use of that base object created ?

    Thanx alot if anyone could guide.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i know alot more of java than c++, regarding OOP. but the derived class is an object of the base class as well. it can be used in any case where a base class object can be used, and can perform in every way the base class can.

    does this answer your question a little? im not sure i fully understand.. can you give an example of what you want to do?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    You can access any public base class functions or variables like you would public derived class functions and variables (there are none in your example). You can also access protected member variables and functions from within the inherited class's functions, so in this case you can access a from inside inherit's functions.

    If two functions or variables have the same name and one is in the base class and one in the derived class, you can access the base class version by prefixing the base class name (e.g. base::func()).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mulitple Inheritance
    By DMJ in forum C++ Programming
    Replies: 1
    Last Post: 08-09-2004, 11:48 AM
  2. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  3. Back with some inheritance fun
    By hpy_gilmore8 in forum C++ Programming
    Replies: 11
    Last Post: 01-16-2004, 11:15 PM
  4. Problems w.r.t Virtual Base Class (inheritance)
    By pankajdynamic in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 10:28 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21