Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 563801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:41:54+00:00 2026-05-13T12:41:54+00:00

suppose i have a class engin and i inherit a class car from engin

  • 0

suppose i have a class engin and i inherit a class car from engin class

class engin
{
    public:
        engin(int nobofcylinders);
        void start();
};
class car:private engin
{
    public:
        car():e(8){}
        void start()
        {
            e.start();
        }
    private:
        engin e;
};

now the same can be done by the composition, the question is which approch would be best and is mostly used in programming, and why???????

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:41:54+00:00Added an answer on May 13, 2026 at 12:41 pm

    I prefer to think of inheritance as derived is a kind of base, that basically means public inheritance. In case of private inheritance it more like derived has a base, which IMHO doesn’t sound right, because that’s IMHO the work for composition not inheritance of any kind. So, since private inheritance and composition essentially mean same thing logically, which to choose? With the example you posted, I’d most certainly go for composition. Why? I tend to think of all kinds of inheritance as a kind of relationship, and with the example you posted, I can’t think of a situation where I could say a car is kind of an engine, it simply isn’t. It’s indeed like a car has an engine, so why would a car inherit from an engine? I see no reason.

    Now, indeed there are cases where it’s good to have private inheritance, namely boost::noncopyable, with it’s ctor/dtor being protected, you’d have hard time instantiating it, and indeed since we want our class to have a noncopyable part, that’s the only way to go.

    Some style guides (e.g. google c++ style guide) even recommend to never use private inheritance, for reasons similar to what I already written – private inheritance is just a bit confusing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have class A { public: void print(){cout<<A; }}; class B: public A
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose I have a class that processes some data: class SomeClass { public: void
I have the regex: (?ms)(?<attribute>\[.+?\]|public|private|\s)+?class and I have the text: [attribute] public int a;
Suppose I have: class Foo { public int Bar { get; set; } }
Suppose I have the following classes: public class Test { public static void main(String[]
Suppose I have class A public class A { public void method() { //do
Suppose I have this class: public class DispatcherService<T> { private static Action<T> Dispatcher; public
Suppose I have: class A { public: A(HWND hwnd, HWND hwnd2); ~A(); int i;
Suppose I have: class Foo { ... }; class Bar : public Foo {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.