Thread: Inheritance :Getting back to the basics

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Inheritance :Getting back to the basics

    Hi, please help

    Code:
    #include <iostream> 
    
    class Base {
    
    public:
    	Base ( int d ) {data = d;}
    	void Show() { std::cout << "I'm showig off " << data << " in base"; }
    private :
    	int data;
    };
    
    class Derived : public Base {
    
    public:
    	Derived() {}
    	void Showd() {
    		Show();
    	}
    };
    
    void main () {
    	Base b(5);
    	Derived	d;
    	d.Show();
    }
    : error C2512: 'Base' : no appropriate default constructor available ?????

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, since Base has a user declared constructor, the compiler will not provide it a default constructor. Now, the Derived class default constructor implicitly invokes Base's default constructor, hence the error. You can either explicitly initialise Base in the Derived default constructor's initialisation list, or you can provide Base with a default constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by laserlight View Post
    Yes, since Base has a user declared constructor, the compiler will not provide it a default constructor. Now, the Derived class default constructor implicitly invokes Base's default constructor, hence the error. You can either explicitly initialise Base in the Derived default constructor's initialisation list, or you can provide Base with a default constructor.
    like this

    Code:
    Derived() : Base() {}

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by csonx_p View Post
    like this

    Code:
    Derived() : Base() {}
    k ...

    Code:
    Derived::Derived() : Base (5) {
    }

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Abstract class

    Code:
    #include <iostream> 
    
    class Base {
    
    public:
    	Base ( int d ) { data = d;}
    	void setData( int d ) { data = d;} 
    	virtual void Show() = 0;
    protected :
    	int data;
    };
    
    class Derived : private Base {
    
    public:
    	Derived();
    	void setData( int d ) { data = d; }
    	void Show() { std::cout << "I'm showig off " << data << " in base"; }
    };
    
    Derived::Derived() : Base (5) {
    }
    
    void main () 
    {
    	Derived	d;	
    	Base * b = &d;
    
    	b->Show();
    	d.setData(10);
    	std::cout << "\n";
    	b->Show();
    }
    error C2243: 'type cast' : conversion from 'Derived *__w64 ' to 'Base *' exists, but is inaccessible ???

    thnx

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are using private inheritance.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by laserlight View Post
    You are using private inheritance.
    lol, forgot to remove tht... But gave me more knowledge .. thanx

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are using void main. Why?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    You are using void main. Why?
    just for testing purposes? But now that you've asked, why not?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by csonx_p
    just for testing purposes? But now that you've asked, why not?
    Because it is non-standard, and you have nothing to gain since changing void to int is enough.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    just for testing purposes? But now that you've asked, why not?
    As a member of this board, you should be fully aware that void main is frowned upon here (just see Salem's avatar).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    just see Salem's avatar.
    ...

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  2. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. Some woman back ended my car today, and back hurts
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-20-2003, 12:42 AM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM