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 8383887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:16:46+00:00 2026-06-09T17:16:46+00:00

I want to know which is better way for this problem: A teacher can

  • 0

I want to know which is better way for this problem:

A teacher can teach more than one subject or can take more than on lab or may be both in a institute. Lab,Subject,Teacher also have some other variables. I did not write them all here.
New lab,subject and teacher can be added individually. When a teacher is added I only input all variables of Teacher class and labCode and subCode. There are some Labs and Subjects which are predefined and others which are added later. I can view record of a teacher which shows his Lab and Subject details also if available.

My concepts in c++ are not too strong and I don’t know which one is better for this. Should I give a pointer in Teacher class to Lab and Subject class or should I inherent them, or is there any better way to do this.

class Lab{
    char labCode[20];
    char labName[40];
        int labYear;
    //here default constructor 
};
class Subject{

    char subCode[20];
    char subName[20];
        int subYear;
    //here default constructor 
};
//-------------------------------------
class Teacher{
    char facName[30];
    int teacherSubCount;
        int teacherLabCount;
    Subject *subject;
        Lab *lab;
    //here default constructor 

};
//------------or should i do like this--------------
class Teacher:public Lab,public Subject{
   char teacherName[30];
    int teacherSubCount;
        int teacherLabCount; 
   //here default constructor 

};

If you can suggest any tutorials so I can learn the power of a pointer as my book states “pointers are very powerful c++”, but they only give some simple examples 🙂

  • 1 1 Answer
  • 0 Views
  • 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-06-09T17:16:48+00:00Added an answer on June 9, 2026 at 5:16 pm

    “Favor ‘object composition’ over ‘class inheritance’.” – Gang of Four

    More specific to your classes, though, first ask yourself… “Is a Teacher a type of Lab or a type of Subject?” Clearly, the answer is “no.”

    In an inheritance model, the child class should itself be an instance of the parent class. Considering the Liskov Substitution Principle, that child object should be treatable as that parent object. So if there’s code that’s looking for a Subject you would be able to pass it a Teacher. Intuitively, that doesn’t make much sense and is not an accurate representation of the real-world domain entities being modeled.

    In this case you definitely want object composition instead of class inheritance. The Teacher has a Lab and has a Subject, not is a Lab or is a Subject. Indeed, if the Teacher can have multiple Labs or Subjects then the composition would simply change from a single instance class member to an array or list of some kind. Whereas the inheritance approach would break entirely in that case.

    When modeling objects that represent real-world concepts like this, consider intuition of those real world concepts foremost and the technical shortcuts to implementation secondary. If it doesn’t make sense in terms of what’s being modeled, then it won’t make sense in the code either.

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

Sidebar

Related Questions

I am new to PayPal and i want to know which is batter way
I want to know which database is more secure: mysql or pgSQL. Which ones
I want to know which one is stable, fast and easy to use?
I Want to know which one is preferred while coding to use Static Methods
I have jboss EAP 5.1 .I want to know which eclipse works with this
I want to know which FamilyName was used by Windows to create a font
i want to know which parsing method is best to use among Simple XML
I want to know which component is best to display multiple line of text's(
I have to do a comparison and I want to know which will be
I want to know in which format the contact details are stored in android

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.